Terraform 默认脚本路径

Terraform default script path

我正在使用 cloud-init 在 Azure 中创建多个虚拟机,它们是并行创建的,当其中任何一个失败时,我可以在日志中看到:

Error: error executing "/tmp/terraform_876543210.sh": Process exited with status 1

但我无法找出哪个 VM 出现故障,我需要对每个 VM 进行 ssh 并检查 脚本路径似乎是为供应定义的 Terraform

有没有一种方法可以将它也覆盖到 cloud-init 中:/tmp/terraform_vmName_876543210.sh ?

我没有使用 provisioner,而是 cloud-init,知道如何强制 terraform 覆盖 terraform sh 文件吗?

下面是我的脚本:

resource "azurerm_linux_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = "Standard_F2"
  admin_username      = "adminuser"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

  admin_ssh_key {
    username   = "adminuser"
    public_key = file("~/.ssh/id_rsa.pub")
  }

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

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }
  
    custom_data = base64encode(templatefile(
    "my-cloud-init.tmpl", {
      var1 = "value1"
      var2 = "value2"
    })
  )
}

还有我的云初始化脚本:

## template: jinja
#cloud-config

runcmd:
- sudo /tmp/bootstrap.sh

write_files:

- path: /tmp/bootstrap.sh
  permissions: 00700
  content: |
    #!/bin/sh -e
    echo hello

根据您为 Terraform 找到的代码,它显示:

DefaultUnixScriptPath is used as the path to copy the file to for remote execution on Unix if not provided otherwise.

这是远程执行的配置。而对于SSH的远程执行,你可以在provisioner "file".

中为复制文件设置sourcedestination

但它是用来在远程VM中设置路径的,而不是你执行Terraform的本地机器。您可以像这样覆盖文件名:

provisioner "file" {
  source      = "conf/myapp.conf"
  destination = "/etc/terraform_$(var.vmName).conf"

  ...
}