gpt4 book ai didi

azure - 使用 Terraform 创建 Azure IaaS 域 Controller

转载 作者:行者123 更新时间:2023-12-03 03:46:28 27 4
gpt4 key购买 nike

我正在尝试使用 terraform(在 Azure 中)将域 Controller 添加到现有域。我在服务器的主 terraform 文件中声明了一些本地值,如下所示:

locals {
username_command = "$username = ${var.domainAdminUsername}"
password_command = "$password = ConvertTo-SecureString ${var.domainAdminPassword} -AsPlainText -Force"
credentials_command = "$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($username,$password)"
install_ad_command = "Add-WindowsFeature -name ad-domain-services -IncludeManagementTools"
configure_ad_command = "Install-ADDSDomainController -DomainName ${var.domainName} -InstallDns -Credential $credentials -SafeModeAdministratorPassword $password -Force:$true"
shutdown_command = "shutdown -r -t 10"
exit_code_hack = "exit 0"
powershell_command = "${local.username_command}; ${local.password_command}; ${local.credentials_command}; ${local.install_ad_command}; ${local.configure_ad_command}; ${local.shutdown_command}; ${local.exit_code_hack}"


}

然后,我在虚拟机上应用自定义脚本扩展资源,该资源运行由本地值构建的 powershell cmdlet:

resource "azurerm_virtual_machine_extension" "promote-to-domain-controller" {
count = 2
depends_on = [
azurerm_windows_virtual_machine.vm
]

name = "promote-to-domain-controller"
virtual_machine_id = azurerm_windows_virtual_machine.vm[count.index].id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"

settings = <<SETTINGS
{
"commandToExecute": "powershell.exe -Command \"${local.powershell_command}\""
}
SETTINGS
}

一切似乎都正常,但虚拟机扩展除外。 [ { "code": "ComponentStatus/StdOut/succeeded", "level": "Info", "displayStatus": "Provisioning succeeded", "message": "" }, { "code": "ComponentStatus/StdErr/succeeded", "level": "Info", "displayStatus": "Provisioning succeeded", "message": "azadmin : The term 'azadmin' is not recognized as the name of a cmdlet, function, script file, or operable program. \r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\r\nAt line:1 char:13\r\n+ $username = azadmin; $password = ConvertTo-SecureString <redacted>...\r\n+ ~~~~~~~\r\n + CategoryInfo : ObjectNotFound: (azadmin:String) [], CommandNotFoundException\r\n + FullyQualifiedErrorId : CommandNotFoundException\r\n \r\nNew-Object : Exception calling \".ctor\" with \"2\" argument(s): \"Cannot process argument because the value of argument \r\n\"userName\" is not valid. Change the value of the \"userName\" argument and run the operation again.\"\r\nAt line:1 char:118\r\n+ ... edentials = New-Object -TypeName System.Management.Automation.PSCrede ...\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException\r\n + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand\r\n \r\n" } ]

var.domainAdminUsername 设置为“azadmin”。在第一个屏幕截图中,第 11 行,您可以看到我将此值 ($username) 传递给 PSCredential 构造函数。该构造函数需要两个重载:一个字符串和一个安全字符串。就像传递给构造函数的值不是字符串,尽管它应该是。

最佳答案

您可以使用以下方法将您的虚拟机提升为现有林的域 Controller 。

主.tf文件:

   provider "azurerm" {
features {}
}
locals {
password_command = "$password = ConvertTo-SecureString ${var.admin_password} -AsPlainText -Force"

credentials_command = "$credentials = Get-Credential ${var.domainAdminUsername}"
install_ad_command = "Add-WindowsFeature -name ad-domain-services -IncludeManagementTools"
configure_ad_command = "Install-ADDSDomainController -DomainName ${var.active_directory_domain} -InstallDns -Credential $credentials -SafeModeAdministratorPassword $password -Force:$true"
shutdown_command = "shutdown -r -t 10"
exit_code_hack = "exit 0"
powershell_command = " ${local.password_command};${local.credentials_command}; ${local.install_ad_command}; ${local.configure_ad_command}; ${local.shutdown_command}; ${local.exit_code_hack}"

}

data "azurerm_virtual_machine" "example" {
name = "${var.vmname}"
resource_group_name = "${var.resource_group_name}"
}

resource "azurerm_virtual_machine_extension" "promote-to-domain-controller" {
name = "promote-to-domain-controller"
virtual_machine_id = data.azurerm_virtual_machine.example.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"

settings = <<SETTINGS
{
"commandToExecute": "powershell.exe -Command \"${local.powershell_command}\""
}
SETTINGS
}

变量.tf 文件:

variable resource_group_name {
description = "The name of the Resource Group where the VM is"
default = "resourcegroup"
}
variable location {
description = "The Azure Region in which the Resource Group exists"
default = "resourcegrouplocation"
}

# Active Directory & Domain Controller
variable vmname {
description = "The Virtual Machine name that you wish to join to the domain"
default = "vmname"
}

variable "active_directory_domain" {
description = "The name of the Active Directory domain, for example `consoto.local`"
default = "domainname"
}
variable "domainAdminUsername" {
description = "The local administrator account on the Domain"
default = "Domain\admin or <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caabaea7a3a48aaea5a7aba3a4e4a9a5a7" rel="noreferrer noopener nofollow">[email protected]</a>"
}

variable "admin_password" {
description = "The password associated with the local administrator account on the virtual machine"
default = "password"
}

输出:(地形规划)

enter image description here

将虚拟机添加到现有域:

Main.tf

resource "azurerm_virtual_machine_extension" "join-domain" {
name = "join-domain"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
virtual_machine_name = "${var.vmname}"
publisher = "Microsoft.Compute"
type = "JsonADDomainExtension"
type_handler_version = "1.3"



settings = <<SETTINGS
{
"Name": "${var.active_directory_domain}",
"OUPath": "",
"User": "${var.active_directory_domain}\\${var.active_directory_username}",
"Restart": "true",
"Options": "3"
}
SETTINGS

protected_settings = <<SETTINGS
{
"Password": "${var.active_directory_password}"
}
SETTINGS
}

变量.tf

variable resource_group_name {
description = "The name of the Resource Group where the VM is"
}
variable location {
description = "The Azure Region in which the Resource Group exists"
}

# Active Directory & Domain Controller
variable vmname {
description = "The Virtual Machine name that you wish to join to the domain"
}

variable "active_directory_domain" {
description = "The name of the Active Directory domain, for example `consoto.local`"
}

variable "active_directory_username" {
description = "The username of an account with permissions to bind machines to the Active Directory Domain"
}

variable "active_directory_password" {
description = "The password of the account with permissions to bind machines to the Active Directory Domain"
}

注意:请确保使用用户名作为域\管理员用户名,以便代码运行时其预期用户名是域的用户名。

关于azure - 使用 Terraform 创建 Azure IaaS 域 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68990098/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com