skip to Main Content

I’m trying to run a bash script on an Azure VM after deploying it with Terraform. I’ve tried different approaches but none of them have worked. With “custom_data”, I assumed that the file will be uploaded and executed, however I’m not even seeing the file inside the VM.

I’ve also looked at “azurerm_virtual_machine_extension”, but this does not give me the option to upload the file, only to execute commands or download from remote location (can’t use fileUris due to requirements):

resource "azurerm_virtual_machine_extension" "test" {
  name                 = "hostname"
  location             = "${azurerm_resource_group.test.location}"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_machine_name = "${azurerm_virtual_machine.test.name}"
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"

  settings = <<SETTINGS
    {
        "commandToExecute": "sh my_script.sh"
    }
SETTINGS

  tags = {
    environment = "Production"
  }
}
resource "azurerm_virtual_machine" "middleware_vm" {
    name                  = "${var.middleware_vm}"
    location              = "${var.location}"
    resource_group_name   = "${azurerm_resource_group.middleware.name}"
    network_interface_ids = ["${azurerm_network_interface.middleware.id}"]
    vm_size               = "Standard_DS4_v2"        

    storage_os_disk {
        name              = "centos_os_disk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Premium_LRS"
    }

    storage_data_disk {
        name                 = "managed_backup_disk"
        create_option        = "Empty"
        caching              = "ReadWrite"
        disk_size_gb         = "256"  
        managed_disk_type    = "Premium_LRS"
        lun                  = 0
    }

    storage_image_reference {
        publisher = "OpenLogic"
        offer     = "CentOS"
        sku       = "7.5"
        version   = "latest"
    }

    os_profile {
        computer_name  = "${var.middleware_vm}"
        admin_username = "middlewareadmin"
        custom_data    = "${file("scripts/middleware_disk.sh")}"
  }

2

Answers


  1. First, the VM extension will just execute the script and do not copy the file to the VM. If you want to copy the script into the VM and then execute it. I will suggest you the Terraform provisioner file and remote-exec.

    Here is the example that copies the file into the existing VM and executes the script:

    resource "null_resource" "example" {
    
        connection {
            type = "ssh"
            user = "azureuser"
            password = "azureuser@2018"
            host = "13.92.255.50"
            port = 22
        }
        provisioner "file" {
            source = "script.sh"
            destination = "/tmp/script.sh"
        }
    
        provisioner "remote-exec" {
            inline = [
                "/bin/bash /tmp/script.sh"
            ]
        }
    }
    

    Note: the script should be created in the current directory.

    Login or Signup to reply.
  2. In azurerm_virtual_machine_extension, you can use:

    protected_settings = <<PROTECTED_SETTINGS
    {
        "script": "${base64encode(file(var.scfile))}"
    }
    PROTECTED_SETTINGS
    

    Please refer to my answer

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search