nutanix / terraform-provider-nutanix

Terraform Nutanix Provider
https://www.terraform.io/docs/providers/nutanix/
Mozilla Public License 2.0
101 stars 112 forks source link

Cloud_init and sysprep CDROMs being detected and destroyed forcing VM reboot #285

Closed gfenn-newbury closed 3 years ago

gfenn-newbury commented 3 years ago

Nutanix Cluster Information

Terraform Version

Affected Resource(s)

Terraform Configuration Files

resource "nutanix_virtual_machine" "vm" {
  count = var.platform == "ahv" && local.os_short_name == "rhe" ? 1 : 0
  name  = infoblox_record_host.host.name
  cluster_uuid = element([for c in flatten(data.nutanix_clusters.clusters[*].entities) :
  c.metadata["uuid"] if c.name == var.cluster], 0)
  description          = var.owner != "" ? "${var.owner} - terraform - ${lookup(data.external.environment.result, "date", formatdate("YYYYMMDD", timestamp()))}" : "${lookup(data.external.environment.result, "owner", "terraform")} - terraform - ${lookup(data.external.environment.result, "date", formatdate("YYYYMMDD", timestamp()))}"
  num_vcpus_per_socket = var.cpus <= local.cpu_max ? var.cpus : local.cpu_max
  num_sockets          = 1
  memory_size_mib      = var.memory <= local.memory_max ? var.memory : local.memory_max

  owner_reference = {
    kind = "user"
    name = data.nutanix_user.user[count.index].user_name
    uuid = data.nutanix_user.user[count.index].id
  }

  nic_list {
    subnet_uuid = data.nutanix_subnet.subnet[count.index].id
  }

  guest_customization_cloud_init_user_data = base64encode(data.template_file.cloudinit_user_data[count.index].rendered)

  disk_list {
    data_source_reference = {
      kind = "image"
      uuid = data.nutanix_image.image[count.index].id
    }
    device_properties {
      device_type = "DISK"
      disk_address = {
        adapter_type = "SCSI"
        device_index = "0"
      }
    }
  }

  dynamic "disk_list" {
    iterator = iii
    for_each = var.additional_disks
    content {
      disk_size_mib = iii.value * 1000
    }
  }
  ... (provisioners only beyond this point)
}

Actual Behavior

After initial apply to create the VM, a plan would display the following:

      - disk_list {
          - data_source_reference  = {} -> null
          - disk_size_bytes        = 0 -> null
          - disk_size_mib          = 0 -> null
          - uuid                   = "77ebeb80-c890-4130-90fc-85c14d546819" -> null
          - volume_group_reference = {} -> null

          - device_properties {
              - device_type  = "CDROM" -> null
              - disk_address = {
                  - "adapter_type" = "IDE"
                  - "device_index" = "0"
                } -> null
            }
        }

Steps to Reproduce

  1. Create a nutanix_virtual_machine resource without specifying the creation of a CDROM, but specifying guest_customization_cloud_init_user_data or guest_customization_sysprep
  2. After creation, run terraform plan again
  3. Observe a CDROM is present, which Terraform wants to destroy
  4. Run terraform apply, and observe the VM is rebooted

Important Factors

This has happened using multiple different versions of Terraform, and with provider version 1.1.1 and 1.2.0, all against AOS 5.19.*. This is not present in 5.15 LTS, and we are yet to test 5.20 LTS. ​

gfenn-newbury commented 3 years ago

Is there any update on this?

TheMoves commented 3 years ago

Switching from using the resource type of template_file to the built in templatefile for userdata fixed the issue for us - i didn't see anything about a CDROM being the culprit.