1

I am creating virtual machine in azure using terraform in which i am passing custom data parameter which should actually trigger a yaml file which does few installations. But it's not being triggered. Could you please advise on this issue.enter code here

Controller VM

resource "azurerm_virtual_machine" "controller-vm" {
name                  = "controller-vm"`enter code here`
location              = var.region

resource_group_name   = azurerm_resource_group.resourcegroup.name
network_interface_ids = [azurerm_network_interface.controllernic.id]
vm_size               = var.ctr_instance_type

storage_os_disk {
    name              = "controller-os-disk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    disk_size_gb      = "400"
    managed_disk_type = "Standard_LRS"
}

storage_data_disk {
    name              = "controller-data-disk0"
    caching           = "ReadWrite"
    create_option     = "Empty"
    managed_disk_type = "Standard_LRS"
    disk_size_gb      = "512"
    lun               = 1
}

storage_data_disk {
    name              = "controller-data-disk1"
    caching           = "ReadWrite"
    create_option     = "Empty"
    managed_disk_type = "Standard_LRS"
    disk_size_gb      = "512"
    lun               = 2
}

storage_image_reference {
    publisher = "OpenLogic"
    offer     = "CentOS-CI"
    sku       = "7-CI"
    version   = "latest"
}

os_profile {
    computer_name  = "controller.${var.project_id}.local"
    admin_username = var.user
    custom_data = file(pathexpand(var.ctr_cloud_init_file))
}

os_profile_linux_config {
    disable_password_authentication = true
    ssh_keys {
        path     = "/home/${var.user}/.ssh/authorized_keys"
        key_data = file(pathexpand(var.ssh_pub_key_path))
    }
}

boot_diagnostics {
    enabled     = "true"
    storage_uri = azurerm_storage_account.storageaccount.primary_blob_endpoint
}

tags = {
    environment = var.project_id,
    user = var.user
}

}

The above code has a custom_data = file(pathexpand(var.ctr_cloud_init_file)) which should actually call the yaml file. which is not happened here.

Please find the yaml file i am using. ctr_cloud_init_file

users:
- name: bluedata
  groups: [sudo, wheel]
  shell: /bin/bash
  sudo: ['ALL=(ALL) NOPASSWD:ALL']
  ssh-authorized-keys: 
  ### SSH Public Key Here
  - ssh-rsa ...

package_upgrade: true
packages: 
  - epel-release
  - firewalld
repo_update: true
repo_upgrade: all
1
  • 1
    The handling of custom_data is mostly done by cloud-init software installed in the VM image, and all Terraform is doing is making that raw data available for cloud-init to fetch and process. cloud-init usually generates logs about what it's doing that can be helpful in debugging problems like this. Where you'd find them will depend on which distribution you are using but hopefully you can find a file with a name containing "cloud-init" in /var/log, or maybe run journalctl /usr/bin/cloud-init on a systemd system. Commented May 11, 2020 at 22:01

1 Answer 1

1

To provision the Azure VM with the Cloud-init file, it's not a good way to load the file directly. For the Cloud-init, I would recommend you use the template_cloudinit_config, this is the special the provider for Cloud-init and its featureset is specialized for the features of cloud-init. And here is the example code:

data "template_file" "script" {
  template = file("cloud-init")
}

# Render a multi-part cloud-init config making use of the part
# above, and other source files
data "template_cloudinit_config" "config" {
  gzip          = true
  base64_encode = true

  # Main cloud-config configuration file.
  part {
    filename     = "cloud-init"
    content_type = "text/cloud-config"
    content      = data.template_file.script.rendered
  }
}

resource "azurerm_virtual_machine" "controller-vm" {
...
os_profile {
    computer_name  = "controller.${var.project_id}.local"
    admin_username = var.user
    custom_data = data.template_cloudinit_config.config.rendered
}
...
}
Sign up to request clarification or add additional context in comments.

19 Comments

I have made the changes based on your suggestion, but the clod inti file is not getting triggered. Please find the below changes made. data "template_file" "script" { template = file("cloud-init-ctr.yaml") } data "template_cloudinit_config" "config" { gzip = true base64_encode = true # Main cloud-config configuration file. part { filename = "init.cfg" content_type = "text/cloud-config" content = data.template_file.script.rendered } }
@BabaDudekula It's my fault. The filename in the part should be the cloud-init name that you want to load.
Updated the script with file name. Still it's not being called. The file i am trying to call is a Yaml file. I have also changed the content from text to yaml in code. Please advise..data "template_file" "script" { template = file("cloud-init-ctr.yaml") } data "template_cloudinit_config" "config" { gzip = true base64_encode = true # Main cloud-config configuration file. part { filename = "cloud-init-ctr.yaml" content_type = "yaml/cloud-config" content = data.template_file.script.rendered } }
@BabaDudekula Where do you put the file? In the same folder that the Terraform file you would deploy?
Yes indeed. Its in the same directory where the terraform is applied
|

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.