hashicorp / terraform-provider-vsphere

Terraform Provider for VMware vSphere
https://registry.terraform.io/providers/hashicorp/vsphere/
Mozilla Public License 2.0
612 stars 449 forks source link

Error destroying `r/virtual_machine` with attached virtual disk #1753

Closed skupfer closed 2 years ago

skupfer commented 2 years ago

Community Guidelines

Terraform

v1.2.8

Terraform Provider

vSphere 2.0.2

VMware vSphere

v7.0.3.00700

Description

We have a case where we want to provide VMs with a persistent data disk which survives up/down-grading the template because the data is independent. The r/vsphere_virtual_disk data disk will get created first on terraform apply and attached afterwards in the r/vsphere_virtual_machine block.

If running terraform destroy the VM will get deleted but will throw the error:

Error: Error caused by file ds:///vmfs/volumes/5eb0222d-0ef0f2c2-6110-e4434bce26d0/persistent_disk_storage/pdisks_persistent.vmdk attached to persistent_test

I need to run the command again to finally destroy the attached disk.

I hope the provided block is enough. I think the problem is that on destruction it is handled top-down, too and therefore it tries to delete the attached disk first without detaching it first.

Affected Resources or Data Sources

Terraform Configuration

resource "vsphere_virtual_disk" "PersistentDataDisk" {
  count             = var.persistent_data_disk == true ? length(var.vms) : 0
  size                  = var.vms[count.index].flavor.disks[1].size
  datacenter            = var.datacenter
  vmdk_path             = "${var.vms[count.index].flavor.persistent_vmdk_path}/${var.vms[count.index].general.host_name}_persistent.vmdk"
  datastore             = var.vms[count.index].general.datastore
  type                  = "thin"
  create_directories    = true
}

resource "vsphere_virtual_machine" "vms" {
[ ...]

  dynamic disk {
    for_each = [for i in var.vms[count.index].flavor.disks: { 
      size   = i.size
      number = i.number
      attach = var.persistent_data_disk == true ? true : false
      path   = "${var.vms[count.index].flavor.persistent_vmdk_path}/${var.vms[count.index].general.host_name}_persistent.vmdk"
    }]
    content {
      label = "disk${disk.value.number}"
      unit_number = disk.value.number
      size = disk.value.number != 1 ? disk.value.size : null
      thin_provisioned = disk.value.number != 1 ? true : null
      attach = disk.value.attach == true && disk.value.number == 1 ? disk.value.attach : null
      path = disk.value.attach == true && disk.value.number == 1 ? disk.value.path : null
      datastore_id = disk.value.attach == true && disk.value.number == 1 ? data.vsphere_datastore.datastores[count.index].id : null
    }
  }

[...]
}

Debug Output

vsphere_virtual_disk.PersistentDataDisk[0]: Destroying... [id=persistent_disk_storage/pdisks_persistent.vmdk]
vsphere_virtual_machine.vms[0]: Destroying... [id=42157a18-6f69-8984-0c1b-4b8601c4ea2a]
vsphere_virtual_machine.vms[0]: Destruction complete after 8s
β•·
β”‚ Error: Error caused by file ds:///vmfs/volumes/5eb0222d-0ef0f2c2-6110-e4434bce26d0/persistent_disk_storage/pdisks_persistent.vmdk attached to persistent_test 
β”‚ 
β”‚ 
β•΅

Panic Output

No response

Expected Behavior

First the VM will get destroyed, then the attached disk

Actual Behavior

Tries destroying the attached disk, destroys the VM and throws an error

Steps to Reproduce

Run terraform apply, then terraform destroy

Environment Details

No response

Screenshots

No response

References

No response

github-actions[bot] commented 2 years ago

Hello, skupfer! πŸ–

Thank you for submitting an issue for this provider. The issue will now enter into the issue lifecycle.

If you want to contribute to this project, please review the contributing guidelines and information on submitting pull requests.

tenthirtyam commented 2 years ago

Reminds me of similar issue GH-1678 - you'll need a depends_on to force the removal of the disk before the deletion of the virtual machine.

Ryan Johnson Senior Staff Solutions Architect | Product Engineering @ VMware, Inc.

tenthirtyam commented 2 years ago

Duplicate of #1678.

skupfer commented 2 years ago

It is indeed a duplicate but I haven't found this. I gave it a try and due to "A single static variable reference is required" i tried linking it to the first vm created

depends_on = [vsphere_virtual_machine.vms[0]] or depends_on = [vsphere_virtual_machine.vms]

but it does not create the virtual_disk and fails because it cannot find it. From the linked issue I got that I have to add the depends_on on the r/vsphere_virtual_disk.

  # vsphere_virtual_disk.PersistentDataDisk[0] will be created
  + resource "vsphere_virtual_disk" "PersistentDataDisk" {
      + adapter_type       = "lsiLogic"
      + create_directories = true
      + datacenter         = "LP"
      + datastore          = "S-LP11"
      + id                 = (known after apply)
      + size               = 10
      + type               = "thin"
      + vmdk_path          = "persistent_disk_storage/pdisks_persistent.vmdk"
    }

  # vsphere_virtual_machine.vms[0] will be created
  + resource "vsphere_virtual_machine" "vms" {

[...]
  Enter a value: yes

vsphere_virtual_machine.vms[0]: Creating...
vsphere_virtual_machine.vms[0]: Still creating... [10s elapsed]
vsphere_virtual_machine.vms[0]: Still creating... [20s elapsed]
vsphere_virtual_machine.vms[0]: Still creating... [30s elapsed]
vsphere_virtual_machine.vms[0]: Still creating... [40s elapsed]

Think I don't get the correct usage

skupfer commented 2 years ago

Ok, found it. I had to link it reverse.

depends_on = [data.vsphere_virtual_machine.template, vsphere_virtual_disk.PersistentDataDisk]

Thanks for your help!

tenthirtyam commented 2 years ago

You’re welcome! Have a good weekend!

Ryan Johnson Senior Staff Solutions Architect | Product Engineering @ VMware, Inc.

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.