hashicorp / terraform-provider-vsphere

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

Clone from content library item with windows_options #2131

Open alonkaliber opened 5 months ago

alonkaliber commented 5 months ago

Community Guidelines

Terraform

v1.6.6

Terraform Provider

v1.6.6

VMware vSphere

v2.6.1

Description

Hey when trying to clone a VM from content_library_item which is an OVA it works, but when trying to customize, i get this error suddenly

│ Error: cannot locate virtual machine or template with UUID "": virtual machine with UUID "" not found
│ 
│   with vsphere_virtual_machine.be_win_vms[2],
│   on vSphere.tf line 136, in resource "vsphere_virtual_machine" "be_win_vms":
│  136: resource "vsphere_virtual_machine" "be_win_vms" {
│ 
╵
╷
│ Error: cannot locate virtual machine or template with UUID "": virtual machine with UUID "" not found
│ 
│   with vsphere_virtual_machine.be_win_vms[0],
│   on vSphere.tf line 136, in resource "vsphere_virtual_machine" "be_win_vms":
│  136: resource "vsphere_virtual_machine" "be_win_vms" {
│ 
╵
╷
│ Error: cannot locate virtual machine or template with UUID "": virtual machine with UUID "" not found
│ 
│   with vsphere_virtual_machine.be_win_vms[1],
│   on vSphere.tf line 136, in resource "vsphere_virtual_machine" "be_win_vms":
│  136: resource "vsphere_virtual_machine" "be_win_vms" {
│ 

This is my code

only when i try to customize i get that error, why does that happen ?

Affected Resources or Data Sources

resource/vsphere_virtual_machine

Terraform Configuration

data "vsphere_content_library" "content_library" {
  name = "Servers"
}

data "vsphere_content_library_item" "windows_image_item" {
  depends_on = [
    vsphere_content_library_item.windows_ova,
    time_sleep.windows_nap
  ]
  name       = "windows_image"
  type       = "ovf"
  library_id = data.vsphere_content_library.content_library.id
}

data "vsphere_content_library_item" "linux_image_item" {
  depends_on = [
    vsphere_content_library_item.linux_ova,
    time_sleep.linux_nap
  ]
  name       = "linux_image"
  type       = "ovf"
  library_id = data.vsphere_content_library.content_library.id
}

resource "vsphere_content_library_item" "windows_ova" {
  name        = "windows_image"
  description = "Windows Image"
  file_url    = var.windows_image_local_path
  library_id  = data.vsphere_content_library.content_library.id
}

resource "vsphere_content_library_item" "linux_ova" {
  name        = "linux_image"
  description = "Linux Image"
  file_url    = var.linux_image_local_path
  library_id  = data.vsphere_content_library.content_library.id
}

resource "time_sleep" "windows_nap" {
  depends_on = [
    vsphere_content_library_item.windows_ova
  ]
  create_duration = "120s"
}

resource "time_sleep" "linux_nap" {
  depends_on = [
    vsphere_content_library_item.linux_ova
  ]
  create_duration = "120s"
}

// cloneings 
resource "vsphere_virtual_machine" "cp_win_vms" {
  depends_on = [
    data.vsphere_content_library_item.windows_image_item
  ]
  count = 3
  name  = "cp_win_${count.index + 1}"
  resource_pool_id = data.vsphere_resource_pool.pool.id
  datastore_id     = data.vsphere_datastore.EMC_SecondVolume.id
  host_system_id   = data.vsphere_host.esxi_3.id
  firmware = "efi"
  num_cpus = 4
  memory   = 16384 # 16GB
  guest_id = "windows9Server64Guest"
  folder = var.vcenter_folder
  scsi_type = "lsilogic-sas"
  nested_hv_enabled = true
  network_interface {
    network_id   = data.vsphere_network.network.id
  }
  disk {
    label            = "disk0"
    size             = 100
    thin_provisioned = true
  }  
  clone {
    template_uuid = data.vsphere_content_library_item.windows_image_item.id
      timeout = 30
   }
}

resource "vsphere_virtual_machine" "be_win_vms" {
  depends_on = [
    data.vsphere_content_library_item.windows_image_item
  ]
  count = 3
  name  = "be_win_${count.index + 1}"
  resource_pool_id = data.vsphere_resource_pool.pool.id
  datastore_id     = data.vsphere_datastore.EMC_SecondVolume.id
  host_system_id   = data.vsphere_host.esxi_1.id
  guest_id = "windows9Server64Guest"
  folder           = var.vcenter_folder
  firmware         = "efi"
  scsi_type        = "lsilogic-sas"
  num_cpus         = 8
  memory           = 16384 # 16GB
  wait_for_guest_net_timeout = 0
  wait_for_guest_ip_timeout  = 0
  nested_hv_enabled = true
  network_interface {
      network_id = data.vsphere_network.network.id
  }
  disk {
    label            = "disk0"
    size             = 100
    thin_provisioned = true
  }
  clone {
    template_uuid = data.vsphere_content_library_item.windows_image_item.id
      timeout = 30
    customize {
      windows_options {
        computer_name  = "be_win_${count.index + 1}"
        join_domain    = var.domain_name
        domain_admin_user = var.domain_admin_user
        domain_admin_password = var.domain_admin_password
      }
      network_interface {}
    }
   }
}

Debug Output

https://gist.github.com/alonkaliber/43eb78e64bc11ae39dd752d93a2f7d8a

Panic Output

No response

Expected Behavior

Expect terraformto have the ability to customize the vm's

Actual Behavior

Got these errors Error: cannot locate virtual machine or template with UUID "": virtual machine with UUID "" not found │ │ with vsphere_virtual_machine.be_win_vms[2], │ on vSphere.tf line 136, in resource "vsphere_virtual_machine" "be_win_vms": │ 136: resource "vsphere_virtual_machine" "be_win_vms" { │ ╵ ╷ │ Error: cannot locate virtual machine or template with UUID "": virtual machine with UUID "" not found │ │ with vsphere_virtual_machine.be_win_vms[0], │ on vSphere.tf line 136, in resource "vsphere_virtual_machine" "be_win_vms": │ 136: resource "vsphere_virtual_machine" "be_win_vms" { │ ╵ ╷ │ Error: cannot locate virtual machine or template with UUID "": virtual machine with UUID "" not found │ │ with vsphere_virtual_machine.be_win_vms[1], │ on vSphere.tf line 136, in resource "vsphere_virtual_machine" "be_win_vms": │ 136: resource "vsphere_virtual_machine" "be_win_vms" {

Steps to Reproduce

Add an OVA to content library, try to clone a VM from with it windows_options included

Environment Details

No response

Screenshots

No response

References

No response

github-actions[bot] commented 5 months ago

Hello, alonkaliber! 🖐

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.

daniel-ferradal-marquez commented 1 month ago

I am getting this same issue or very similar.

I am trying to clone from a library item also.

data "vsphere_content_library" "library" {
  name = "mylibary"

data "vsphere_content_library_item" "item" {
  name = "template_name"
  type = "vm_template"
  library_id = data.vpshere_content_library.library.id
}

data "vsphere_virtual_machine" "template_machine" {
  uuid = data.vsphere_content_library_item.item.id # Terraform complains of this UUID
}

# later in resource_virtual_machine:

clone {
  template_uuid = data.vsphere_virtual_machine.template_machine.id
}

this brings the correct UUID from the template, still terraform complains UUID not found, but when visiting this item in the library in the VCENTER Website the UUID referred for each item in the library goes by this format:

UUID_from_template:UUID_from_vcenter

So perhaps this is the issue here? Falling sort for the correct UUID or how it has to be referred?

tenthirtyam commented 1 month ago

Question: If you separate this into different plans for the sake of testing to remove the depends / potential race condition, does the issue still persist?

For example, you could use this sample to test this scenario.

https://github.com/vmware-samples/packer-examples-for-vsphere/tree/develop/terraform/vsphere-virtual-machine/content-library-ovf-windows-guest-customization