Skip to content

r/virtual_machines: linked clones depend on template_uuid instead of snapshot_id #831

@rickhlx

Description

@rickhlx

Terraform Version

v0.12.3

vSphere Provider Version

v1.12.0

Affected Resource(s)

Please list the resources as a list, for example:

  • vsphere_virtual_machine
  • vsphere_virtual_machine_snapshot

If this issue appears to affect multiple resources, it may be an issue with
Terraform's core, so please mention this.

Terraform Configuration Files

resource "random_string" "master_password" {
  length  = 16
  special = true
}

resource "vsphere_virtual_machine" "rdsh_master" {
  name             = var.master_name
  datastore_id     = data.vsphere_datastore.ds.id
  num_cpus         = 4
  memory           = 4096
  guest_id         = data.vsphere_virtual_machine.master_template.guest_id
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  scsi_type        = data.vsphere_virtual_machine.master_template.scsi_type
  annotation       = "Created by Terraform"
  firmware         = data.vsphere_virtual_machine.master_template.firmware

  wait_for_guest_net_timeout = 0

  network_interface {
    network_id   = data.vsphere_network.network.id
    adapter_type = "vmxnet3"
  }

  disk {
    label            = "disk0"
    size             = var.clone_disk_size
    thin_provisioned = true
    eagerly_scrub    = false
  }

  clone {
    template_uuid = data.vsphere_virtual_machine.master_template.id

    customize {

      windows_options {
        computer_name     = "USRDSH-PC-1"
        organization_name = "Org"
        workgroup         = "WORKGROUP"
        time_zone         = 35
        admin_password    = random_string.master_password.result
      }
      network_interface {}

      timeout = 30
    }
  }

}

resource "vsphere_virtual_machine_snapshot" "vm_snapshot" {
  virtual_machine_uuid = vsphere_virtual_machine.rdsh_master.id
  snapshot_name        = "baseline"
  description          = "Managed Snapshot by Terraform for Linked Clones"
  memory               = "false"
  quiesce              = "false"
  remove_children      = "true"
  consolidate          = "true"
}

resource "vsphere_virtual_machine" "rdsh_clone" {
  name             = format("%s-%d", var.clone_prefix, count.index)
  count            = var.clone_count
  datastore_id     = data.vsphere_datastore.ds.id
  num_cpus         = var.clone_num_cpus
  memory           = var.clone_memory
  guest_id         = data.vsphere_virtual_machine.master_template.guest_id
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  scsi_type        = data.vsphere_virtual_machine.master_template.scsi_type
  annotation       = "Created by Terraform"
  firmware         = data.vsphere_virtual_machine.master_template.firmware

  wait_for_guest_net_timeout = 0

  network_interface {
    network_id   = data.vsphere_network.network.id
    adapter_type = "vmxnet3"
  }

  disk {
    label            = "disk0"
    size             = var.clone_disk_size
    thin_provisioned = true
    eagerly_scrub    = false
  }

  clone {
    template_uuid = vsphere_virtual_machine.rdsh_master.id
    linked_clone  = true

    customize {
      windows_options {
        computer_name         = format("%s-%d", var.clone_prefix, count.index)
        join_domain           = "domain"
        organization_name     = "Org"
        domain_admin_user     = var.domain_admin_user
        domain_admin_password = var.domain_admin_password
        time_zone             = 35
        admin_password        = vsphere_virtual_machine_snapshot.vm_snapshot.id
      }

      network_interface {}

      timeout = 30
    }
  }

  depends_on = [vsphere_virtual_machine_snapshot.vm_snapshot]
}

Expected Behavior

When deleting the snapshot a new snapshot should be created, triggering new linked clones to be created (as a result of the password for the machines being the snapshot id).

Actual Behavior

Liked clone resources cannot find the snapshot associated to the template (since it does not exist) and throws an error.

Would expect that snapshot is created, then linked clones.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply
  2. delete snapshot manually
  3. terraform plan (fails)

Important Factoids

I believe the way to fix this is by optionally asking for a snapshot id when creating linked clones. And forcing a new resource if the id changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions