英文:
Add safemodeadminpassword to locals in terraform
问题
尝试创建一个具有一个域控制器的实验室,我试图将该虚拟机加入到一个新的AD林,但我在添加SafeModeAdministratorPassword时遇到了问题,而不是将密码添加为明文。
密码是由random_password提供程序生成的:
resource "random_password" "rndm-pass-vm" {
length = 12
special = true
}
resource "azurerm_key_vault_secret" "kv-sec-vm-pass" {
name = "kv-sec-vm-pass"
value = random_password.rndm-pass-vm.result
key_vault_id = azurerm_key_vault.kvne01.id
depends_on = [azurerm_key_vault.kvne01]
}
resource "azurerm_virtual_machine_extension" "dc01-ad" {
name = "dc01-ad-ps1"
virtual_machine_id = azurerm_windows_virtual_machine.rgne1-vm01.id
depends_on = [azurerm_managed_disk.dc01-ntds]
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"commandToExecute": "powershell.exe -Command \"${local.powershell}\""
}
SETTINGS
}
locals {
cmd01 = "Install-WindowsFeature AD-Domain-Services -IncludeAllSubFeature -IncludeManagementTools"
cmd02 = "Install-WindowsFeature DNS -IncludeAllSubFeature -IncludeManagementTools"
cmd03 = "Import-Module ADDSDeployment, DnsServer"
cmd04 = "Install-ADDSForest -DomainName ${var.domain_name} -DomainNetbiosName ${var.domain_netbios_name} -DomainMode ${var.domain_mode} -ForestMode ${var.domain_mode} -DatabasePath ${var.database_path} -SysvolPath ${var.sysvol_path} -LogPath ${var.log_path} -NoRebootOnCompletion:$false -Force:$true -SafeModeAdministratorPassword (ConvertTo-SecureString ${var.safe_mode_administrator_password} -AsPlainText -Force)"
powershell = "${local.cmd01}; ${local.cmd02}; ${local.cmd03}; ${local.cmd04}"
}
英文:
Trying to create a lab with one domain controller and I'm trying to join that VM to a new ad forest but I'm having trouble adding the SafeModeAdministratorPassword without adding the password as plaintext
The password is generated by random_password provider:
resource "random_password" "rndm-pass-vm" {
length = 12
special = true
}
resource "azurerm_key_vault_secret" "kv-sec-vm-pass" {
name = "kv-sec-vm-pass"
value = random_password.rndm-pass-vm.result
key_vault_id = azurerm_key_vault.kvne01.id
depends_on = [azurerm_key_vault.kvne01]
}
resource "azurerm_virtual_machine_extension" "dc01-ad" {
name = "dc01-ad-ps1"
virtual_machine_id = azurerm_windows_virtual_machine.rgne1-vm01.id
depends_on = [azurerm_managed_disk.dc01-ntds]
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"commandToExecute": "powershell.exe -Command \"${local.powershell}\""
}
SETTINGS
}
locals {
cmd01 = "Install-WindowsFeature AD-Domain-Services -IncludeAllSubFeature -IncludeManagementTools"
cmd02 = "Install-WindowsFeature DNS -IncludeAllSubFeature -IncludeManagementTools"
cmd03 = "Import-Module ADDSDeployment, DnsServer"
cmd04 = "Install-ADDSForest -DomainName ${var.domain_name} -DomainNetbiosName ${var.domain_netbios_name} -DomainMode ${var.domain_mode} -ForestMode ${var.domain_mode} -DatabasePath ${var.database_path} -SysvolPath ${var.sysvol_path} -LogPath ${var.log_path} -NoRebootOnCompletion:$false -Force:$true -SafeModeAdministratorPassword (ConvertTo-SecureString ${var.safe_mode_administrator_password} -AsPlainText -Force)"
powershell = "${local.cmd01}; ${local.cmd02}; ${local.cmd03}; ${local.cmd04}"
}
答案1
得分: 1
如果您的资源声明正确,您需要创建一个名为 variables.tf
的文件,并插入以下代码:
variable "safe_mode_administrator_password" {
type = string
description = "The password for the Safe Mode Administrator account."
sensitive = true
}
将变量声明为 sensitive 是为了建议保护秘密:https://developer.hashicorp.com/terraform/tutorials/configuration-language/sensitive-variables#sensitive-variables
您可以运行以下命令进行检查:
terraform plan
这将显示下一个屏幕:
然后,您可以继续执行 terraform apply
命令。
更新:
要生成密码并在命令中使用它,您可以在本地块中声明:
resource "random_password" "rndm-pass-vm" {
length = 12
special = true
}
locals {
generated_password = random_password.rndm-pass-vm.result
cmd01 = "Install-WindowsFeature AD-Domain-Services -IncludeAllSubFeature -IncludeManagementTools"
cmd02 = "Install-WindowsFeature DNS -IncludeAllSubFeature -IncludeManagementTools"
cmd03 = "Import-Module ADDSDeployment, DnsServer"
cmd04 = "Install-ADDSForest -DomainName 'test.domain' -DomainNetbiosName 'test' -NoRebootOnCompletion:$false -Force:$true -SafeModeAdministratorPassword (ConvertTo-SecureString ${local.generated_password} -AsPlainText -Force)"
powershell = "${local.cmd01}; ${local.cmd02}; ${local.cmd03}; ${local.cmd04}"
}
然后更新命令以使用它:
... -SafeModeAdministratorPassword (ConvertTo-SecureString ${local.generated_password} -AsPlainText -Force) ...
英文:
If your resources declaration it's OK, you need to create a file named variables.tf
and insert the following code:
variable "safe_mode_administrator_password" {
type = string
description = "The password for the Safe Mode Administrator account."
sensitive = true
}
The declaration of the variable as sensitive it's a recommendation to protect the secret: https://developer.hashicorp.com/terraform/tutorials/configuration-language/sensitive-variables#sensitive-variables
And you can check running:
terraform plan
That will show you the next screen:
And you can continue to execute the terraform apply
command.
Update:
To generate the password and use it inside de command, you can declare on the locals block:
resource "random_password" "rndm-pass-vm" {
length = 12
special = true
}
locals {
generated_password = random_password.rndm-pass-vm.result
cmd01 = "Install-WindowsFeature AD-Domain-Services -IncludeAllSubFeature -IncludeManagementTools"
cmd02 = "Install-WindowsFeature DNS -IncludeAllSubFeature -IncludeManagementTools"
cmd03 = "Import-Module ADDSDeployment, DnsServer"
cmd04 = "Install-ADDSForest -DomainName 'test.domain' -DomainNetbiosName 'test' -NoRebootOnCompletion:$false -Force:$true -SafeModeAdministratorPassword (ConvertTo-SecureString ${local.generated_password} -AsPlainText -Force)"
powershell = "${local.cmd01}; ${local.cmd02}; ${local.cmd03}; ${local.cmd04}"
}
And update the command to use it:
... -SafeModeAdministratorPassword (ConvertTo-SecureString ${local.generated_password} -AsPlainText -Force)" ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论