地形azurerm_virtual_machine_extension,使用带参数的CustomScriptExtension运行本地PowerShell脚本

jgovgodb  于 2022-12-23  发布在  Shell
关注(0)|答案(1)|浏览(132)

下面是一段tf代码:

resource "azurerm_virtual_machine_scale_set_extension" "runscript" {
    virtual_machine_scale_set_id = subid

    auto_upgrade_minor_version = true
    name                       = "RunCustomScript"
    publisher                  = "Microsoft.Compute"
    type                       = "CustomScriptExtension"
    type_handler_version       = "1.8"
    protected_settings         = <<PROTECTED_SETTINGS
    {
       "commandToExecute" : "powershell -encodedCommand ${textencodebase64(file("${path.module}/scripts/myscript.ps1"), "UTF-16LE") }"
    }
    PROTECTED_SETTINGS
}

这是基于这个answer。我想使用的脚本需要两个参数。我已经能够让一个参数工作,但正在努力使它与两个参数一起工作。
我已经尝试过使用一个参数:

resource "azurerm_virtual_machine_scale_set_extension" "runscipt" {
    virtual_machine_scale_set_id = subid

    auto_upgrade_minor_version = true
    name                       = "RunCustomScript"
    publisher                  = "Microsoft.Compute"
    type                       = "CustomScriptExtension"
    type_handler_version       = "1.8"
    protected_settings         = <<PROTECTED_SETTINGS
    {
       "commandToExecute" : "echo param1 | powershell -encodedCommand ${textencodebase64(file("${path.module}/scripts/myscript.ps1"), "UTF-16LE") }"
    }
    PROTECTED_SETTINGS
}

我试过把两个参数都放在那里,但那不起作用。如果有人能帮我的话,我将非常感激。

rkkpypqq

rkkpypqq1#

我们可以使用操作符执行多个命令。试试这个方法。

  • *&&**-如果第一个命令成功,则执行第二个命令。
    *|| *-仅在第一个命令失败时执行第二个命令。

下面是使用Terraform运行多个命令的示例代码。

commandToExecute : "echo param1 && apt-get -y update"
    • 包含azurerm虚拟机规模集扩展名的示例代码段**
    • Step1:**将以下代码放入主tf文件
resource "azurerm_resource_group" "example" {
  name     = "rg-swarnademonew"
  location = "East US"
}
locals {
  first_public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+wWK73dCr+jgQOAxNsHAnNNNMEFki5p2ZRJqcbCiF4pJrxUQR/RXqVFQdbRLZgYfJ8xGB878RENq3yQ39d8dVOkq4edbkzwcUmwwwkYVPIoDGsYLaRHnG+To7FvMeyO7xDVQkMKzopTQV8AuKpyvpqu0a9pWOMaiCyDytO7GGN you@me.com"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_linux_virtual_machine_scale_set" "example" {
  name                = "example-vmss"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  sku                 = "Standard_F2"
  instances           = 1
  admin_username      = "adminuser"

  admin_ssh_key {
    username   = "adminuser"
    public_key = local.first_public_key
  }

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

  os_disk {
    storage_account_type = "Standard_LRS"
    caching              = "ReadWrite"
  }

  network_interface {
    name    = "example"
    primary = true

    ip_configuration {
      name      = "internal"
      primary   = true
      subnet_id = azurerm_subnet.internal.id
    }
  }
}
resource "azurerm_virtual_machine_scale_set_extension" "example" {
  name                         = "example"
  virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.example.id
  publisher                    = "Microsoft.Azure.Extensions"
  type                         = "CustomScript"
  type_handler_version         = "2.0"
   protected_settings         = <<PROTECTED_SETTINGS
    {
        "commandToExecute" : "echo param1 && apt-get -y update " 
    }
    PROTECTED_SETTINGS
}

将tf文件提供为

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.0"
    }
  }

}

provider "azurerm" {
  features {}
}

步骤2:在命令下方运行

Terraform plan

输出如下:

terraform apply -auto-approve

从门户验证

相关问题