linuxunixterraformterraform-provider-azurecloud-init

Cloudinit file inside terraform config file not working


I'm trying to run a cloudinit file by passing it in the terraform config file. The terraform apply command creates all the resources. But when i spin up the VM, none of the changes from the cloudinit are seen in the VM.

Here is the Cloudinit file with .tpl extension:

users:
- name: ansible
  gecos: Ansible
  sudo: ALL=(ALL) NOPASSWD:ALL
  groups: [users, admin]
  shell: /bin/bash
  ssh_authorized_keys:
    - ssh-rsa AAAAB3NzaC1.......

And here is the main.tf file:

data "template_file" "users_data" {
  template = file("./sshPass.tpl")
}

data "template_cloudinit_config" "config" {
  gzip          = true
  base64_encode = true

  part {
  content_type = "text/cloud-config"
  content      = data.template_file.users_data.rendered
}

  resource "azurerm_linux_virtual_machine" "poc-vm" {


    name                  = var.vm_name
    resource_group_name   = azurerm_resource_group.poc_rg.name
    location              = azurerm_resource_group.poc_rg.location
    size                  = var.virtual_machine_size
    admin_username        = var.vm_username
    network_interface_ids = [azurerm_network_interface.poc_nic_1.id]

    admin_ssh_key {
      username   = var.vm_username
      public_key = tls_private_key.poc_key.public_key_openssh
    }

    os_disk {
      caching              = var.disk_caching
      storage_account_type = var.storage_type
    }

    source_image_reference {
     publisher = var.image_publisher
     offer     = var.image_offer
     sku       = var.image_sku
     version   = var.image_version
    }

    user_data = data.template_cloudinit_config.config.rendered

  }

Solution

  • Found the errors. Changed the extension to '.cfg.'. Added 'custom_data' instead of user_data. Added '#cloud-config' to the 1st line of the file. Made sure I removed any spaces at end of my ssh key. And also felt like I was using the wrong ssh key to login the whole time. But anyways those things helped me.