使用 terraform 在 Azure VM 中运行自定义 shell 脚本

jag*_*oti 3 shell azure azure-virtual-machine terraform

长期以来,我一直在努力在 Azure VM 中运行自定义 shell 脚本。Shell 命令工作正常,但是当我将它们捆绑到 shell 脚本时,它失败了。我已经在 节中定义了 shell 脚本settings

地形代码:

resource "azurerm_resource_group" "test" {
  name     = "acctestrg"
  location = "West US"
}

resource "azurerm_virtual_network" "test" {
  name                = "acctvn"
  address_space       = ["10.0.0.0/16"]
  location            = "West US"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "test" {
  name                 = "acctsub"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_network_name = "${azurerm_virtual_network.test.name}"
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_public_ip" "pubip" {
  name                         = "tom-pip"
  location                     = "${azurerm_resource_group.test.location}"
  resource_group_name          = "${azurerm_resource_group.test.name}"
  public_ip_address_allocation = "Dynamic"
  idle_timeout_in_minutes      = 30

  tags {
    environment = "test"
  }
}

resource "azurerm_network_interface" "test" {
  name                = "acctni"
  location            = "West US"
  resource_group_name = "${azurerm_resource_group.test.name}"

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = "${azurerm_subnet.test.id}"
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = "${azurerm_public_ip.pubip.id}"
  }
}

resource "azurerm_storage_account" "test" {
  name                     = "mostor"
  resource_group_name      = "${azurerm_resource_group.test.name}"
  location                 = "westus"
  account_tier             = "Standard"
  account_replication_type = "LRS"

  tags {
    environment = "staging"
  }
}

resource "azurerm_storage_container" "test" {
  name                  = "vhds"
  resource_group_name   = "${azurerm_resource_group.test.name}"
  storage_account_name  = "${azurerm_storage_account.test.name}"
  container_access_type = "private"
}

resource "azurerm_virtual_machine" "test" {
  name                  = "acctvm"
  location              = "West US"
  resource_group_name   = "${azurerm_resource_group.test.name}"
  network_interface_ids = ["${azurerm_network_interface.test.id}"]
  vm_size               = "Standard_A0"

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

  storage_os_disk {
    name          = "myosdisk1"
    vhd_uri       = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
    caching       = "ReadWrite"
    create_option = "FromImage"
  }

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

  os_profile_linux_config {
    disable_password_authentication = false
  }

  tags {
    environment = "staging"
  }
}

resource "azurerm_virtual_machine_extension" "test" {
  name                 = "hostname"
  location             = "West US"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_machine_name = "${azurerm_virtual_machine.test.name}"
  publisher            = "Microsoft.OSTCExtensions"
  type                 = "CustomScriptForLinux"
  type_handler_version = "1.2"

  settings = <<SETTINGS
  {
  "fileUris": ["https://sag.blob.core.windows.net/sagcont/install_nginx_ubuntu.sh"],
    "commandToExecute": "sh install_nginx_ubuntu.sh"
  }
SETTINGS

  tags {
    environment = "Production"
  }
}
Run Code Online (Sandbox Code Playgroud)

我已从脚本中的命令中删除了所有 sudo,因为 Azure 以 root 身份运行所有命令。FYR,shell 脚本如下:

外壳代码:

#!/bin/bash

echo "Running apt update"
apt-get update
echo "Installing nginx"
apt-get install nginx
Run Code Online (Sandbox Code Playgroud)

我面临的错误只不过是一条超时消息,如下所示:

错误:

azurerm_virtual_machine.test: Creation complete after 3m21s (ID: /subscriptions/b017dff9-5685-4a83-80d3-...crosoft.Compute/virtualMachines/acctvm)
azurerm_virtual_machine_extension.test: Creating...
  location:             "" => "westus"
  name:                 "" => "hostname"
  publisher:            "" => "Microsoft.OSTCExtensions"
  resource_group_name:  "" => "acctestrg"
  settings:             "" => "  {\n  \"fileUris\": [\"https://sag.blob.core.windows.net/sagcont/install_nginx_ubuntu.sh\"],\n\t\"commandToExecute\": \"sh install_nginx_ubuntu.sh\"\n  }\n"
  tags.%:               "" => "1"
  tags.environment:     "" => "Production"
  type:                 "" => "CustomScriptForLinux"
  type_handler_version: "" => "1.2"
  virtual_machine_name: "" => "acctvm"
azurerm_virtual_machine_extension.test: Still creating... (10s elapsed)
azurerm_virtual_machine_extension.test: Still creating... (20s elapsed)
azurerm_virtual_machine_extension.test: Still creating... (30s elapsed)
azurerm_virtual_machine_extension.test: Still creating... (40s elapsed)
azurerm_virtual_machine_extension.test: Still creating... (50s elapsed)
azurerm_virtual_machine_extension.test: Still creating... (1m0s elapsed)

Error: Error applying plan:

1 error(s) occurred:

* azurerm_virtual_machine_extension.test: 1 error(s) occurred:

* azurerm_virtual_machine_extension.test: compute.VirtualMachineExtensionsClient#CreateOrUpdate: Failure sending request: StatusCode=200 -- Original Error: Long running operation terminated with status 'Failed': Code="VMExtensionProvisioningError" Message="VM has reported a failure when processing extension 'hostname'. Error message: \"Malformed status file [ExtensionError] Invalid status/status: failed\"."

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.
Run Code Online (Sandbox Code Playgroud)

我可以确认每个人都可以访问该脚本,因为我可以使用 wget 下载它。不知道出了什么问题。在网上进行了很多挖掘,但最终我到处都发现了一个未解决的错误或问题。此外,适用于 Azure with Terraform 的内容并不多。任何帮助表示赞赏!

Shu*_*bao 5

-y是的,你的脚本中需要。

apt-get install nginx -y

当执行Azure自定义脚本扩展时,脚本应该是自动的,不能等待手动输入参数。

在您的脚本中,如果您不添加-y,脚本将挂起并等待您的输入yes。Azure 自定义脚本扩展等待几分钟,然后出现超时错误。

评论更新:

我无法找到下载 tar/脚本的位置。请你在这里提供一些线索。

脚本的所有执行输出和错误都会记录到脚本的下载目录/var/lib/waagent//download//中,输出的尾部会记录到HandlerEnvironment.json指定的日志目录中并返回到蔚蓝

扩展的操作日志是/var/log/azure///extension.log 文件。

有关此的更多信息请参阅此链接