hashicorp / terraform-provider-vsphere

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

Issue with VirtualMachine and VirtualDisk attachment at creation and Use as secondary disk during same apply. #2303

Open asteriskie opened 6 days ago

asteriskie commented 6 days ago

Community Guidelines

Terraform

1.7.1

Terraform Provider

hashicorp/vsphere

VMware vSphere

8.0.3.00400

Description

Not sure if my tf version has anything to do with this but can try if it truly is related. It would seem that the following is true: I can create a vsphere_virtual_machine resource fine I can create the vsphere_virtual_disk resource fine

What I cannot do is create them, then attach them in the same terraform apply.

For example, I am creating the vsphere_virtual_machine with a single boot disk and a secondary disk that is created separately from the vm....hence the vsphere_virtual_disk
Scenario 1: When I try to include that secondary disk as an attachment to the vm, when the virtual machine is initially created, it will not attach as it keeps asking about the datastore_id even though I am explicitly setting it.

Scenario 2: When I try to create the vsphere_virtual_disk and the vsphere_virtual_machine independently but do not attach the first time through, it creates all resources. From there I can re-enable the code to attach the volume in which the vm then allows the volume to be attached to the vm as intended.

Hopefully these 2 scenarios make sense. As you can imagine I would quite like for scenario 1 to work.

These are not being created presently on a clustered datastore.

I'm sorry I cannot provide full code but I followed the documentation and that code really is enough just need to attach the volume.

Affected Resources or Data Sources

resource/vsphere_virtual_disk resource /vshpere_virtual_machine

Terraform Configuration

resource "vsphere_virtual_machine" "vm1" {
  ------------------------
  #ifI comment this out, do a terraform apply, then re-enable this code it attaches to the vm.
  disk{
    attach = true
    label = "disk1"
    path = vsphere_virtual_disk.data.vmdk_path
    datastore_id = data.vsphere_datastore.datastore.id
    disk_mode = "persistent"
    unit_number = 1
  }
  --------
}

resource "vsphere_virtual_disk" "data" {
  size               = var.persistentDisk.size
  type               = "thin"
  vmdk_path          = "/datadisks/${var.vm.vm_hostname}-${var.persistentDisk.name}.vmdk" 
  create_directories = true
  datacenter         = data.vsphere_datacenter.datacenter.name
  datastore          = data.vsphere_datastore.datastore.name
}

Debug Output

would rather not post what was requested:

│ Error: disk.1: datastore_id for disk "disk1" is required when attach is set
│ 
│   with module.vsphere-vms["hostA"].vsphere_virtual_machine.vm,
│   on .terraform/modules/vsphere-vms/vm.tf line 1, in resource "vsphere_virtual_machine" "vm":
│    1: resource "vsphere_virtual_machine" "vm" {
│ 

Panic Output

No response

Expected Behavior

Allow both the vm and the disk to be created with the disk attached on first runthrough.

Actual Behavior

Receive error about missing a datastore_id even though it's been provided.

Steps to Reproduce

Use document sample code to create a vm and disk but DO include a secondary disk as an attached disk. Do not run without as it will create a vm and disk separately fine.

Environment Details

No response

Screenshots

No response

References

No response

github-actions[bot] commented 6 days ago

Hello, asteriskie! 🖐

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.