Home > other >  Cloudinit file inside terraform config file not working
Cloudinit file inside terraform config file not working

Time:09-27

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

  }

CodePudding user response:

Try this:

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

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

In this example I change this line content = data.template_file.users_data.rendered for this one content = "${data.template_file.users_data.rendered}"

Hope this helps!

CodePudding user response:

Found the error. Looks like I was using the wrong ssh key to login. And also added #cloud-config to the 1st line of the file, maybe this was the error. But anyways those 2 things helped me.

  • Related