无法使用 Terraform 将 powershell 脚本作为自定义扩展执行

问题描述 投票:0回答:1

我正在尝试使用 terraform 在 Azure Windows VM 上执行自定义扩展脚本。这是一个简单的 PS1 脚本,用于执行已下载并在 Azure VM 中可用的 msi 文件。 PS1 和 MSI 文件都可以在虚拟机中提到的位置 (c: emp)

这是我执行的高级脚本

locals {
  shir_authorization_key = "asdkjfbdkjfb"
  powershell_script = "c:\\temp\\installGatewayOnLocalMachine.ps1"
  msi_file_location = "C:\\temp\\IntegrationRuntime_5.33.8649.1.msi"
  vm_name = "sdjkfgbsgb"
  vm_custom_extension_name = "somename01"
}


resource "azurerm_virtual_machine_extension" "shir-vm-exclusive" {
  for_each = local.shir_exclusive_vm_ids
  name                             = local.vm_custom_extension_name
  virtual_machine_id               = each.value
  publisher                        = "Microsoft.Compute"
  type                             = "CustomScriptExtension"
  type_handler_version             = "1.9"
  settings                         = <<SETTINGS
  {   
    "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File ${local.powershell_script} -path \"${local.msi_file_location}\" -authKey ${local.shir_authorization_key}"
  }
  SETTINGS
}

我无法让这段代码工作。我不断收到 Bad JSON 错误或类似下面的内容

CI-terraform-plan FAILURE terraform plan failed due to     Error: "settings" contains an invalid JSON: invalid character 'I' in string escape code
    
      with module.dataFactory_instance01.azurerm_virtual_machine_extension.shir-vm-exclusive["/subscriptions/0c535c03-373b-4ce5-a538-ab1832d91e49/resourceGroups/rg-cus-gtmddp-dev-01/providers/Microsoft.Compute/virtualMachines/VCAUSC117DEV01P"],
      on .terraform/modules/dataFactory_instance01/vmshirexclusive.tf line 61, in resource "azurerm_virtual_machine_extension" "shir-vm-exclusive":
      61:   settings                         = <<SETTINGS
      62:   {   
      63:     "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File ${local.powershell_script} -path \"${local.msi_file_location}\" -authKey ${local.shir_authorization_key}"
      64:   }
      65:   SETTINGS

如果我传递实际值,而不是将路径参数作为变量传递,一切都会正常工作

"commandToExecute": "powershell ${local.powershell_script} -path \"C:\\temp\\IntegrationRuntime_5.33.8649.1.msi\" -authKey ${local.shir_authorization_key}"

当我将变量传递给 terraform 脚本时,不确定是否需要在文件路径中进行任何额外的转义。有人可以建议我缺少什么吗?

powershell terraform azure-virtual-machine terraform-provider-azure
1个回答
1
投票

我尝试使用 Terraform 将 PowerShell 脚本作为自定义扩展执行,并且能够成功配置需求

您的 Terraform 脚本遇到问题的原因是 Azure VM 自定义扩展的 JSON 配置中字符串插值和转义的工作方式。您需要确保在使用变量时正确转义

/
字符串中的斜杠 (
commandToExecute
)(Windows 文件路径中经常使用的斜杠)。

JSON 中的反斜杠表示转义字符。在原始脚本中,变量

local.msi_file_location
(
C:/temp/...
) 的文件路径中的反斜杠是转义字符,因此 JSON 格式错误。

您可以通过根据路径在每个文件后面提供正确的路径来解决此问题。

我的地形配置:

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}

# Assuming you have a resource group already created
data "azurerm_resource_group" "example" {
  name = "Venkat"
}

# Assuming you have a virtual machine already created
data "azurerm_virtual_machine" "example" {
  name                = "Venkat-Windows-VM"
  resource_group_name = data.azurerm_resource_group.example.name
}

# Locals for the script and extension details
locals {
  shir_authorization_key = "Welcoe@123$"
  powershell_script      = "C:/Installer/Powershellscript.ps1"
  msi_file_location      = "C:/Installer/7z2301-x64.msi"
  vm_custom_extension_name = "demovk"
}

# Custom script extension to run on the existing VM
resource "azurerm_virtual_machine_extension" "shir_vm_extension" {
  name                 = local.vm_custom_extension_name
  virtual_machine_id   = data.azurerm_virtual_machine.example.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"

  settings = <<SETTINGS
  {
    "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File ${replace(local.powershell_script, "\\", "\\\\")} -path \"${replace(local.msi_file_location, "\\", "\\\\")}\" -authKey ${local.shir_authorization_key}"
  }
  SETTINGS
}

输出:

enter image description here

enter image description here

现在打开

run
并发出命令
appwiz.cpl
并检查进度

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.