0

I am creating multiple VMs in Azure using cloud-init, they are created in parallel and when any of them fails, I can see in the logs:

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

But I have no way to figure out which VM is failing, I need to ssh each of them and check The script path seems to be defined for provisioning Terraform

Is there a way to override it also for cloud-init to something like: /tmp/terraform_vmName_876543210.sh ?

I am not using provisioner but cloud-init, any idea how I can force terraform to override the terraform sh file?

Below my script:

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"
    })
  )
}

And my cloud-init script:

## template: jinja
#cloud-config

runcmd:
- sudo /tmp/bootstrap.sh

write_files:

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

1 Answer 1

1

From the code you found for the Terraform, it shows:

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

It's the configuration for the remote execution. And for the remote execution of the SSH, you can set the source and the destination for the copy file in the provisioner "file".

But it's used to set the path in the remote VM, not the local machine that you execute the Terraform. And you can overwrite the file name like this:

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

  ...
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your response, but as I said, I am not using provisioner but cloud-init, I updated my question to make more clearer.
Also, the /tmp/terraform_876543210.sh sh file is created by terraform itself, I have no control over it
@proximator Can you share the my-cloud-init.tmpl file?
@proximator Actually, if you use the cloud-init to configure the VM, it won't create a file in the path /tmp/.
@proximator What you found is to set the default file path and it's for the resource that doesn't configure the provisioner. You can get more details here. I didn't find a certain document, but I guess if the cloud-init script doesn't and you didn't configure the provisioner, then it will copy the script in that path and name the file in that format. No matter you use the VM extension or the cloud-init, the purpose is to run the script, there is no difference.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.