Terraform azurerm_虚拟机_扩展,使用CustomScriptExtension运行本地PowerShell脚本

Terraform azurerm_虚拟机_扩展,使用CustomScriptExtension运行本地PowerShell脚本,powershell,terraform,Powershell,Terraform,如何在terraform azurerm_virtual_machine_扩展中运行本地而不存储到blob存储帐户PowerShell脚本 文件夹具有 main.tf 安装.ps1 资源azurerm\u虚拟机\u扩展软件{ name=安装软件 resource\u group\u name=azurerm\u resource\u group.azrg.name virtual\u machine\u id=azurerm\u virtual\u machine.vm.id publisher

如何在terraform azurerm_virtual_machine_扩展中运行本地而不存储到blob存储帐户PowerShell脚本

文件夹具有

main.tf 安装.ps1

资源azurerm\u虚拟机\u扩展软件{ name=安装软件 resource\u group\u name=azurerm\u resource\u group.azrg.name virtual\u machine\u id=azurerm\u virtual\u machine.vm.id publisher=Microsoft.Compute 类型=CustomScriptExtension 类型\处理程序\版本=1.9

  settings = <<SETTINGS
    { 
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File \"install.ps1\""
    } 
    SETTINGS
} 
任何线索

谢谢

这对我很有效

resource "azurerm_virtual_machine_extension" "software" {
  name                 = "install-software"
  resource_group_name  = azurerm_resource_group.azrg.name
  virtual_machine_id   = azurerm_virtual_machine.vm.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"

  protected_settings = <<SETTINGS
  {
    "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.tf.rendered)}')) | Out-File -filepath install.ps1\" && powershell -ExecutionPolicy Unrestricted -File install.ps1"
  }
  SETTINGS
}

data "template_file" "tf" {
    template = "${file("install.ps1")}"
} 
这对我有用

resource "azurerm_virtual_machine_extension" "software" {
  name                 = "install-software"
  resource_group_name  = azurerm_resource_group.azrg.name
  virtual_machine_id   = azurerm_virtual_machine.vm.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"

  protected_settings = <<SETTINGS
  {
    "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.tf.rendered)}')) | Out-File -filepath install.ps1\" && powershell -ExecutionPolicy Unrestricted -File install.ps1"
  }
  SETTINGS
}

data "template_file" "tf" {
    template = "${file("install.ps1")}"
} 
感谢您的工作指令

下面介绍如何使用函数来支持数据源

locals {
  scriptName     = "install.ps1"
  scriptRendered = filebase64("${path.module}/${local.scriptName}")
  # use templatefile() to parse script parameters
  ifTemplateFile = base64encode(templatefile("${path.module}/${local.scriptName}", {}))
  commandToExecute = jsonencode({
    commandToExecute = "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${local.scriptRendered}')) | Out-File -filepath ${local.scriptName}\" && powershell -ExecutionPolicy Unrestricted -File ${local.scriptName}"
  })
}

  # settings block will look like
  protected_settings = local.commandToExecute
module假定脚本与地形代码位于同一目录中

感谢您的工作指令

下面介绍如何使用函数来支持数据源

locals {
  scriptName     = "install.ps1"
  scriptRendered = filebase64("${path.module}/${local.scriptName}")
  # use templatefile() to parse script parameters
  ifTemplateFile = base64encode(templatefile("${path.module}/${local.scriptName}", {}))
  commandToExecute = jsonencode({
    commandToExecute = "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${local.scriptRendered}')) | Out-File -filepath ${local.scriptName}\" && powershell -ExecutionPolicy Unrestricted -File ${local.scriptName}"
  })
}

  # settings block will look like
  protected_settings = local.commandToExecute
module假定脚本与地形代码位于同一目录中


这里有一个更漂亮的解决方案,基于。 注意,我们应该使用Terraform>=v0.14,因为它允许指定Unicode编码,这是 powershell-编码的命令


这里有一个更漂亮的解决方案,基于。 注意,我们应该使用Terraform>=v0.14,因为它允许指定Unicode编码,这是 powershell-编码的命令