运行 Linux VM 上使用 Terraform 的脚本扩展

Run script extension on Linux VM using Terraform

我运行正在尝试使用自定义脚本扩展在 Azure Linux VM 规模集上 运行 一个 bash 脚本,我已将脚本上传到已有 Azure 存储帐户。 bash 脚本用于在 VM 规模集上安装 ngix。脚本 运行s 没有任何错误,但是如果我登录任何 VMScaleset 实例进行验证,我看不到 NGIX 运行ning。 Bash 脚本在这里

#!/bin/bash
apt-get update 
apt-get install -y nginx

此处为 Terraform 文件

data "azurerm_subnet" "refdata" {
  name                 = var.subnetName1
  virtual_network_name = var.vnetName
  resource_group_name  = var.resourceGroupName
}
resource "azurerm_windows_virtual_machine_scale_set" "res-vmscaleset" {
  name                = var.vmScaleSetName
  resource_group_name = azurerm_resource_group.DevRG.name
  location            = azurerm_resource_group.DevRG.location
  sku                 = "Standard_F2"
  instances           = 1
  admin_password      = "xxxxxx"
  admin_username      = "adminuser"

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter-Server-Core"
    version   = "latest"
  }

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

  network_interface {
    name    = "vmscaleset-nic"
    primary = true

    ip_configuration {
      name      = "internal"
      primary   = true
       subnet_id=data.azurerm_subnet.test.id
    }
  }
}

resource "azurerm_linux_virtual_machine_scale_set" "res-linuxscale" {
  name                = "linuxvmss"
  resource_group_name = azurerm_resource_group.DevRG.name
  location            = azurerm_resource_group.DevRG.location
  sku                 = "Standard_F2"
  instances           = 2
  admin_password = "Password1234!"
  disable_password_authentication = false
  admin_username      = "adminuser"

  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    = "lvmscaleset-nic"
    primary = true

    ip_configuration {
      name      = "internal"
      primary   = true
      subnet_id=data.azurerm_subnet.test.id
      
    }
  }
}

resource "azurerm_virtual_machine_scale_set_extension" "res-extension" {
  name                         = "example"
  virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.res-linuxscale.id
  publisher                    = "Microsoft.OSTCExtensions"
  type                         = "CustomScriptForLinux"
  type_handler_version         = "1.0"
  settings = <<SETTINGS
{
    
    "fileUris": ["https://xxxxxxxxxxx.blob.core.windows.net/shellscript11/post-deploy.sh"],
    "commandToExecute": "sh post-deploy.sh"
    }
 SETTINGS

}

参考 this document,您可以像这样为您的自定义脚本使用发布者和类型。

resource "azurerm_virtual_machine_scale_set_extension" "res-extension" {
  name                         = "nnn-extension"
  virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.example.id
  publisher                    = "Microsoft.Azure.Extensions"
  type                         = "CustomScript"
  type_handler_version         = "2.0"
  settings = jsonencode({ 
    "fileUris" = ["https://xxxx.blob.core.windows.net/shscripts/aptupdate.sh"],
    "commandToExecute" = "sh aptupdate.sh"
    }
  )
}

应用以上配置后,你可以upgrade每个vmss实例,那么Nginx就会运行。

结果