azure 如何在powershell脚本中部署microsoft自定义脚本扩展terraform变量

s5a0g9ez  于 2023-05-18  发布在  Shell
关注(0)|答案(2)|浏览(172)

我需要部署Azure自定义脚本扩展。我的脚本是PowerShell,我想在PowerShell脚本中使用terraform变量。当我使用空资源时,我的脚本工作得很好,但我可以在Azure自定义脚本扩展中发送值。
我的地形脚本在下面

resource "azurerm_virtual_machine_extension" "example_extension" {
  name                 = "exampleExtension"
  virtual_machine_id   = azurerm_virtual_machine.example_vm.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.10"

  settings = <<SETTINGS
    {
      "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(file("testing.ps1"))}')) | Out-File -filepath testing.ps1\" && powershell -ExecutionPolicy Unrestricted -File testing.ps1 -example_variable '${var.example_variable}'"
     
    }
SETTINGS
}

Powershell脚本

Param(
    [Parameter(Mandatory=$true)]
    [string]$example_variable
)

Write-Host "The value of example_variable is: $example_variable"

扩展时出错

[
    {
        "code": "ComponentStatus/StdOut/succeeded",
        "level": "Info",
        "displayStatus": "Provisioning succeeded",
        "message": "The value of example_variable is: \r\n\r\n\r\n    Directory: C:\\Packages\\Plugins\\Microsoft.Compute.CustomScriptExtension\\1.10.15\\Downloads\\0\r\n\r\n\r\nMode                LastWriteTime         Length Name                                                                  \r\n----                -------------         ------ ----                                                                  \r\n-a----         5/8/2023   1:10 AM              0 .txt                                                                  \r\n\r\n\r\n"
    },
    {
        "code": "ComponentStatus/StdErr/succeeded",
        "level": "Info",
        "displayStatus": "Provisioning succeeded",
        "message": "?Param : The term '?Param' 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 C:\\Packages\\Plugins\\Microsoft.Compute.CustomScriptExtension\\1.10.15\\Downloads\\0\\testing.ps1:1 char:1\r\n+ ?Param(\r\n+ ~~~~~~\r\n    + CategoryInfo          : ObjectNotFound: (?Param:String) [], CommandNotFoundException\r\n    + FullyQualifiedErrorId : CommandNotFoundException\r\n \r\n"
    }
]

我没有使用?因为它正在向我展示。有没有任何解决方案,部署terraform变量在powershell脚本使用微软扩展。

oewdyzsn

oewdyzsn1#

检查powershell执行后的文件内容是否正确获取。
用途

data "template_file" "tf" {
    template = "${file("mypscript.ps1")}"
}

获取powershell文件。
验证码:

resource "azurerm_virtual_machine" "example" {
  name                  = "kaaaacctvm"
  location              = data.azurerm_resource_group.example.location
  resource_group_name   = data.azurerm_resource_group.example.name
 ....

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }

  ...
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }

  ...

}

resource "azurerm_virtual_machine_extension" "example" {
  name                 = "vmextnname"
  virtual_machine_id   = azurerm_virtual_machine.example.id
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"
settings = <<SETTINGS
    {
      "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.tf.rendered)}'))  | Out-File -filepath mypscript.ps1\" && powershell -ExecutionPolicy Unrestricted -File mypscript.ps1 -example_variable '${var.example_variable}'"
     
    }
SETTINGS

 
}

data "template_file" "tempfile" {
    template = "${file("mypscript.ps1")}"
}

参考:Terraform azurerm_virtual_machine_extension, run local PowerShell Script using CustomScriptExtension - Stack Overflow

mwkjh3gx

mwkjh3gx2#

在这里,我将所需的文件存储在存储帐户中,并从keyvault(在我的情况下)获取所需的秘密。

# In my data.tf file
data "azurerm_key_vault" "commonkv" {
  name                = var.common_kv_name
  resource_group_name = var.common_rg_name
}

# Non-Domain admin user name
data "azurerm_key_vault_secret" "vmusername" {
  name         = "vm-admin-user-name"
  key_vault_id = data.azurerm_key_vault.commonkv.id
}

# Non-Domain admin user Password
data "azurerm_key_vault_secret" "vmuserpwd" {
  name         = "vm-admin-user-pwd"
  key_vault_id = data.azurerm_key_vault.commonkv.id
}

# In my main.tf file
resource "azurerm_virtual_machine_extension" "winvmcse" {
  name                       = "wincse"
  virtual_machine_id         = azurerm_windows_virtual_machine.winvm.id
  publisher                  = "Microsoft.Compute"
  type                       = "CustomScriptExtension"
  type_handler_version       = "1.9"
  auto_upgrade_minor_version = true
  automatic_upgrade_enabled  = false
  settings                   = <<SETTINGS
  {
  "fileUris": ["https://mystorageaccount.blob.core.windows.net/packages/file1.zip","https://mystorageaccount.blob.core.windows.net/packages/file2.ps1"],
  "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File ./file2.ps1 ${data.azurerm_key_vault_secret.vmusername.value} ${data.azurerm_key_vault_secret.vmuserpwd.value}",
  "timestamp" : "2"
  }
SETTINGS
  timeouts {
    create = "1h30m"
    delete = "1h15m"
  }
  tags = var.tags
}

在我的file2.ps1中,我通过以下方式获得这些值

$username = $args[0]
$password = $args[1]

这个方法适用于linux和windows,根据操作系统,唯一的变化是你必须修改你的执行脚本。希望这有帮助!

相关问题