Terraform-VMWare-Modules / terraform-vsphere-vm

Terraform vSphere module for provisioning Virtual Machines
https://registry.terraform.io/modules/Terraform-VMWare-Modules/vm/vsphere/
MIT License
169 stars 176 forks source link

Migrating VMs to new hosts labels disks as 'deleted' #160

Open jwealthdale opened 8 months ago

jwealthdale commented 8 months ago

We've just added new hosts to our existing VMware cluster running ESXi 8.0. Migrating existing VMs from the old hosts to the new hosts and importing TF resources back into state causes disk to be labelled as 'deleted' and new disks are created on apply. The underlying datastores are the same.

Versions

Terraform: 1.7.5 vSphere Provider: 2.7.0

# module.xxx.vsphere_virtual_machine.vm[0] will be updated in-place
  ~ resource "vsphere_virtual_machine" "vm" {
      ~ host_system_id                          = "xxx" -> (known after apply)
        id                                      = "xxx"
      + ignored_guest_ips                       = []
      ~ imported                                = true -> false
        name                                    = "xxx"
      ~ resource_pool_id                        = "xxx" -> "xxx"
        tags                                    = []
        # (68 unchanged attributes hidden)

      + clone {
          + linked_clone  = false
          + template_uuid = "xxx"
          + timeout       = 30

          + customize {
              + dns_server_list = [
                  + "x.x.x.x",
                  + "x.x.x.x",
                ]
              + ipv4_gateway    = "x.x.x.x"
              + timeout         = 10

              + network_interface {
                  + ipv4_address = "x.x.x.x"
                  + ipv4_netmask = 24
                }

              + windows_options {
                  + admin_password        = (sensitive value)
                  + auto_logon            = true
                  + auto_logon_count      = 2
                  + computer_name         = "xxx"
                  + domain_admin_password = (sensitive value)
                  + domain_admin_user     = "xxx"
                  + full_name             = "Administrator"
                  + join_domain           = "xxx"
                  + organization_name     = "xxx"
                  + run_once_command_list = []
                  + time_zone             = xxx
                }
            }
        }

      ~ disk {
          ~ label            = "Hard disk 1" -> "<deleted>"
            # (19 unchanged attributes hidden)
        }
      + disk {
          + attach           = false
          + controller_type  = "scsi"
          + datastore_id     = "xxx"
          + disk_mode        = "persistent"
          + disk_sharing     = "sharingNone"
          + eagerly_scrub    = false
          + io_limit         = -1
          + io_reservation   = 0
          + io_share_count   = 0
          + io_share_level   = "normal"
          + keep_on_remove   = false
          + key              = 0
          + label            = "disk0"
          + size             = 100
          + thin_provisioned = false
          + unit_number      = 0
          + write_through    = false
        }

        # (2 unchanged blocks hidden)
    }

As a workaround we've had to ignore changes to disk.

damnsam commented 8 months ago

I had that happen too, usually it means that the disk attributes changed so much between states that it thinks it's all new. Whenever I have a VM move to a new vCenter/datacenter/cluster/etc, I would do the following:

I would test this with a fresh local tfstate and test vm to get comfortable with this process before affecting live resources.