Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VM Folder Placement Module - Path for type VM not valid #2030

Closed
4 tasks done
penguinpages opened this issue Oct 2, 2023 · 8 comments
Closed
4 tasks done

VM Folder Placement Module - Path for type VM not valid #2030

penguinpages opened this issue Oct 2, 2023 · 8 comments
Labels
bug Type: Bug question/provider Question: Provider

Comments

@penguinpages
Copy link

penguinpages commented Oct 2, 2023

Community Guidelines

  • I have read and agree to the HashiCorp Community Guidelines .
  • Vote on this issue by adding a 👍 reaction to the original issue initial description to help the maintainers prioritize.
  • Do not leave "+1" or other comments that do not add relevant information or questions.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Terraform

1.5.7

Terraform Provider

2.0.2

VMware vSphere

8.0

Description

main.tf

Defined Use of Folder Type VM path:

data "vsphere_folder" "rancher" {
  path = "datacenter/vm/demos/suse/rancher"
}

Call for vm deploy

module "rke-prod-sc-01" {
  source  = "./modules/ovf_deploy"
  vm_name = "rke-prod-sc-01"
  vm_network = data.vsphere_network.managment_a110.id
  vsphere_folder = data.vsphere_folder.rancher.path
  vm_template = data.vsphere_content_library_item.linux_sles_15_5.id
  vm_datastore = data.vsphere_datastore.vsanDatastore.id
  vm_resource_pool = data.vsphere_resource_pool.NormalPriorityVMs.id
  cloud_init_meta = base64encode(templatefile("./modules/ovf_deploy/metadata.tftmpl",
    {
      hostname = "rke-prod-sc-01"
      dhcp_enabled = false
      node_ip = "172.16.110.91"
      gateway_ip = "172.16.110.1"
      dns_search_domain = "acme.local"
      nameserver = "172.16.110.241,172.16.110.242"
    }))
  cloud_init_user = base64encode(templatefile("./modules/ovf_deploy/userdata.tftmpl",
    {
      username = "rkeadmin"
      ssh_key = "ecdsa-sha2-nistp256 AAAAE2Vj...blah"
    }))
}

Per instructions the PATH for folder should be datacenter/<vm> or datacenter etc..>/folder/subfolder.

vmware_folder_path

Affected Resources or Data Sources

Deploy of VMs within folder path. This is important to customer who will not authorize deployment service accounts that have VM create at top level in vcenter. Workaround is to allow all datacenter but this is a stop deploy for them to move to production

Terraform Configuration

terraform { 
  required_providers {
    vsphere = {
      source  = "hashicorp/vsphere"
      version = "2.4.3"
    }
  }
}

provider "vsphere" {
  user                 = var.vsphere_user
  password             = var.vsphere_password
  vsphere_server       = var.vsphere_server
  allow_unverified_ssl = true
}

# vSphere data

data "vsphere_datacenter" "datacenter" {
  name = "datacenter"
}

data "vsphere_compute_cluster" "cluster" {
  name          = "cluster"
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_resource_pool" "NormalPriorityVMs" {
  name          = "NormalPriorityVMs"
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_content_library" "rke_content_library" {
  name = "rke_content_library"
}

data "vsphere_folder" "rancher" {
  path = "datacenter/vm/demos/suse/rancher"
}

# Networking

data "vsphere_network" "managment_a110" {
  name          = "managment_a110"
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

# Storage devices in vcenter

data "vsphere_datastore" "vsanDatastore" {
  name          = "vsanDatastore"
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

#vm templates

data "vsphere_content_library_item" "linux_sles_15_5" {
  name       = "linux-sles-15.5"
  type       = "ovf"
  library_id = data.vsphere_content_library.rke_content_library.id
}

#tags

# resource "vsphere_tag_category" "k8s-region" {  # Tag catagory is defined as datacenter name. Remark out as objects existed
#   name        = "k8s-region"
#   cardinality = "SINGLE"
#   description = "Managed by Terraform"

#   associable_types = [
#     "Datacenter",
#   ]
# }

# resource "vsphere_tag" "region1" {
#   name        = "region1"
#   category_id = "${vsphere_tag_category.k8s-region.id}"
#   description = "Managed by Terraform"
# }

# resource "vsphere_tag_category" "k8s-zone" {
#   name        = "k8s-zone"
#   cardinality = "SINGLE"
#   description = "Managed by Terraform"

#   associable_types = [
#     "ClusterComputeResource",
#   ]
# }

# resource "vsphere_tag" "az1" {
#   name        = "az1"
#   category_id = "${vsphere_tag_category.k8s-zone.id}"
#   description = "Managed by Terraform"
# }
module "rke-prod-sc-01" {
  source  = "./modules/ovf_deploy"
  vm_name = "rke-prod-sc-01"
  vm_network = data.vsphere_network.managment_a110.id
  vsphere_folder = data.vsphere_folder.rancher.path
  vm_template = data.vsphere_content_library_item.linux_sles_15_5.id
  vm_datastore = data.vsphere_datastore.vsanDatastore.id
  vm_resource_pool = data.vsphere_resource_pool.NormalPriorityVMs.id
  cloud_init_meta = base64encode(templatefile("./modules/ovf_deploy/metadata.tftmpl",
    {
      hostname = "rke-prod-sc-01"
      dhcp_enabled = false
      node_ip = "172.16.110.91"
      gateway_ip = "172.16.110.1"
      dns_search_domain = "acme.local"
      nameserver = "172.16.110.241,172.16.110.242"
    }))
  cloud_init_user = base64encode(templatefile("./modules/ovf_deploy/userdata.tftmpl",
    {
      username = "rkeadmin"
      ssh_key = "ecdsa-sha2-nistp256 AA...blah"
    }))
}

Debug Output

  Enter a value: yes

module.rke-prod-sc-03.vsphere_virtual_machine.ovf_deploy: Creating...
module.rke-prod-sc-02.vsphere_virtual_machine.ovf_deploy: Creating...
module.rke-prod-sc-01.vsphere_virtual_machine.ovf_deploy: Creating...

│ Error: folder '/datacenter/vm/datacenter/vm/demos/suse/rancher' not found

│   with module.rke-prod-sc-01.vsphere_virtual_machine.ovf_deploy,
│   on modules/ovf_deploy/main.tf line 10, in resource "vsphere_virtual_machine" "ovf_deploy":
│   10: resource "vsphere_virtual_machine" "ovf_deploy" {

Panic Output

Terraform will perform the following actions:

  # module.rke-prod-sc-01.vsphere_virtual_machine.ovf_deploy will be created
  + resource "vsphere_virtual_machine" "ovf_deploy" {
      + annotation                              = (known after apply)
      + boot_retry_delay                        = 10000
      + change_version                          = (known after apply)
      + cpu_limit                               = -1
      + cpu_share_count                         = (known after apply)
      + cpu_share_level                         = "normal"
      + datastore_id                            = "datastore-27"
      + default_ip_address                      = (known after apply)
      + efi_secure_boot_enabled                 = true
      + ept_rvi_mode                            = "automatic"
      + extra_config                            = {
          + "guestinfo.metadata"          = "I2Nsb3..blah"
          + "guestinfo.metadata.encoding" = "base64"
          + "guestinfo.userdata"          = "I2Nsb3Vk..blah"
          + "guestinfo.userdata.encoding" = "base64"
        }
      + extra_config_reboot_required            = true
      + firmware                                = "efi"
      + folder                                  = "datacenter/vm/demos/suse/rancher"
      + force_power_off                         = true
      + guest_id                                = (known after apply)
      + guest_ip_addresses                      = (known after apply)
      + hardware_version                        = (known after apply)
      + host_system_id                          = (known after apply)
      + hv_mode                                 = "hvAuto"
      + id                                      = (known after apply)
      + ide_controller_count                    = 2
      + imported                                = (known after apply)
      + latency_sensitivity                     = "normal"
      + memory                                  = 8192
      + memory_limit                            = -1
      + memory_share_count                      = (known after apply)
      + memory_share_level                      = "normal"
      + migrate_wait_timeout                    = 30
      + moid                                    = (known after apply)
      + name                                    = "rke-prod-sc-01"
      + num_cores_per_socket                    = 1
      + num_cpus                                = 4
      + power_state                             = (known after apply)
      + poweron_timeout                         = 300
      + reboot_required                         = (known after apply)
      + resource_pool_id                        = "resgroup-76"
      + run_tools_scripts_after_power_on        = true
      + run_tools_scripts_after_resume          = true
      + run_tools_scripts_before_guest_shutdown = true
      + run_tools_scripts_before_guest_standby  = true
      + sata_controller_count                   = 0
      + scsi_bus_sharing                        = "noSharing"
      + scsi_controller_count                   = 1
      + scsi_type                               = "pvscsi"
      + shutdown_wait_timeout                   = 3
      + storage_policy_id                       = (known after apply)
      + swap_placement_policy                   = "inherit"
      + tools_upgrade_policy                    = "manual"
      + uuid                                    = (known after apply)
      + vapp_transport                          = (known after apply)
      + vmware_tools_status                     = (known after apply)
      + vmx_path                                = (known after apply)
      + wait_for_guest_ip_timeout               = 0
      + wait_for_guest_net_routable             = true
      + wait_for_guest_net_timeout              = 5

      + clone {
          + template_uuid = "0343f52c-4161-46b1-9992-5842093ac2b7"
          + timeout       = 30
        }

      + disk {
          + attach            = false
          + controller_type   = "scsi"
          + datastore_id      = "<computed>"
          + device_address    = (known after apply)
          + 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"
          + path              = (known after apply)
          + size              = 40
          + storage_policy_id = (known after apply)
          + thin_provisioned  = true
          + unit_number       = 0
          + uuid              = (known after apply)
          + write_through     = false
        }

      + network_interface {
          + adapter_type          = "vmxnet3"
          + bandwidth_limit       = -1
          + bandwidth_reservation = 0
          + bandwidth_share_count = (known after apply)
          + bandwidth_share_level = "normal"
          + device_address        = (known after apply)
          + key                   = (known after apply)
          + mac_address           = (known after apply)
          + network_id            = "dvportgroup-46087"
        }
    }

  # module.rke-prod-sc-02.vsphere_virtual_machine.ovf_deploy will be created
  + resource "vsphere_virtual_machine" "ovf_deploy" {
      + annotation                              = (known after apply)
      + boot_retry_delay                        = 10000
      + change_version                          = (known after apply)
      + cpu_limit                               = -1
      + cpu_share_count                         = (known after apply)
      + cpu_share_level                         = "normal"
      + datastore_id                            = "datastore-27"
      + default_ip_address                      = (known after apply)
      + efi_secure_boot_enabled                 = true
      + ept_rvi_mode                            = "automatic"
      + extra_config                            = {
          + "guestinfo.metadata"          = "I2Nsb3VkLW...blah"
          + "guestinfo.metadata.encoding" = "base64"
          + "guestinfo.userdata"          = "I2Nsb3VkLW...blah"
          + "guestinfo.userdata.encoding" = "base64"
        }
      + extra_config_reboot_required            = true
      + firmware                                = "efi"
      + folder                                  = "datacenter/vm/demos/suse/rancher"
      + force_power_off                         = true
      + guest_id                                = (known after apply)
      + guest_ip_addresses                      = (known after apply)
      + hardware_version                        = (known after apply)
      + host_system_id                          = (known after apply)
      + hv_mode                                 = "hvAuto"
      + id                                      = (known after apply)
      + ide_controller_count                    = 2
      + imported                                = (known after apply)
      + latency_sensitivity                     = "normal"
      + memory                                  = 8192
      + memory_limit                            = -1
      + memory_share_count                      = (known after apply)
      + memory_share_level                      = "normal"
      + migrate_wait_timeout                    = 30
      + moid                                    = (known after apply)
      + name                                    = "rke-prod-sc-02"
      + num_cores_per_socket                    = 1
      + num_cpus                                = 4
      + power_state                             = (known after apply)
      + poweron_timeout                         = 300
      + reboot_required                         = (known after apply)
      + resource_pool_id                        = "resgroup-76"
      + run_tools_scripts_after_power_on        = true
      + run_tools_scripts_after_resume          = true
      + run_tools_scripts_before_guest_shutdown = true
      + run_tools_scripts_before_guest_standby  = true
      + sata_controller_count                   = 0
      + scsi_bus_sharing                        = "noSharing"
      + scsi_controller_count                   = 1
      + scsi_type                               = "pvscsi"
      + shutdown_wait_timeout                   = 3
      + storage_policy_id                       = (known after apply)
      + swap_placement_policy                   = "inherit"
      + tools_upgrade_policy                    = "manual"
      + uuid                                    = (known after apply)
      + vapp_transport                          = (known after apply)
      + vmware_tools_status                     = (known after apply)
      + vmx_path                                = (known after apply)
      + wait_for_guest_ip_timeout               = 0
      + wait_for_guest_net_routable             = true
      + wait_for_guest_net_timeout              = 5

      + clone {
          + template_uuid = "0343f52c-4161-46b1-9992-5842093ac2b7"
          + timeout       = 30
        }

      + disk {
          + attach            = false
          + controller_type   = "scsi"
          + datastore_id      = "<computed>"
          + device_address    = (known after apply)
          + 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"
          + path              = (known after apply)
          + size              = 40
          + storage_policy_id = (known after apply)
          + thin_provisioned  = true
          + unit_number       = 0
          + uuid              = (known after apply)
          + write_through     = false
        }

      + network_interface {
          + adapter_type          = "vmxnet3"
          + bandwidth_limit       = -1
          + bandwidth_reservation = 0
          + bandwidth_share_count = (known after apply)
          + bandwidth_share_level = "normal"
          + device_address        = (known after apply)
          + key                   = (known after apply)
          + mac_address           = (known after apply)
          + network_id            = "dvportgroup-46087"
        }
    }

  # module.rke-prod-sc-03.vsphere_virtual_machine.ovf_deploy will be created
  + resource "vsphere_virtual_machine" "ovf_deploy" {
      + annotation                              = (known after apply)
      + boot_retry_delay                        = 10000
      + change_version                          = (known after apply)
      + cpu_limit                               = -1
      + cpu_share_count                         = (known after apply)
      + cpu_share_level                         = "normal"
      + datastore_id                            = "datastore-27"
      + default_ip_address                      = (known after apply)
      + efi_secure_boot_enabled                 = true
      + ept_rvi_mode                            = "automatic"
      + extra_config                            = {
          + "guestinfo.metadata"          = "I2Nsb3V..blah"
          + "guestinfo.metadata.encoding" = "base64"
          + "guestinfo.userdata"          = "I2Nsb3V...blah"
          + "guestinfo.userdata.encoding" = "base64"
        }
      + extra_config_reboot_required            = true
      + firmware                                = "efi"
      + folder                                  = "datacenter/vm/demos/suse/rancher"
      + force_power_off                         = true
      + guest_id                                = (known after apply)
      + guest_ip_addresses                      = (known after apply)
      + hardware_version                        = (known after apply)
      + host_system_id                          = (known after apply)
      + hv_mode                                 = "hvAuto"
      + id                                      = (known after apply)
      + ide_controller_count                    = 2
      + imported                                = (known after apply)
      + latency_sensitivity                     = "normal"
      + memory                                  = 8192
      + memory_limit                            = -1
      + memory_share_count                      = (known after apply)
      + memory_share_level                      = "normal"
      + migrate_wait_timeout                    = 30
      + moid                                    = (known after apply)
      + name                                    = "rke-prod-sc-03"
      + num_cores_per_socket                    = 1
      + num_cpus                                = 4
      + power_state                             = (known after apply)
      + poweron_timeout                         = 300
      + reboot_required                         = (known after apply)
      + resource_pool_id                        = "resgroup-76"
      + run_tools_scripts_after_power_on        = true
      + run_tools_scripts_after_resume          = true
      + run_tools_scripts_before_guest_shutdown = true
      + run_tools_scripts_before_guest_standby  = true
      + sata_controller_count                   = 0
      + scsi_bus_sharing                        = "noSharing"
      + scsi_controller_count                   = 1
      + scsi_type                               = "pvscsi"
      + shutdown_wait_timeout                   = 3
      + storage_policy_id                       = (known after apply)
      + swap_placement_policy                   = "inherit"
      + tools_upgrade_policy                    = "manual"
      + uuid                                    = (known after apply)
      + vapp_transport                          = (known after apply)
      + vmware_tools_status                     = (known after apply)
      + vmx_path                                = (known after apply)
      + wait_for_guest_ip_timeout               = 0
      + wait_for_guest_net_routable             = true
      + wait_for_guest_net_timeout              = 5

      + clone {
          + template_uuid = "0343f52c-4161-46b1-9992-5842093ac2b7"
          + timeout       = 30
        }

      + disk {
          + attach            = false
          + controller_type   = "scsi"
          + datastore_id      = "<computed>"
          + device_address    = (known after apply)
          + 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"
          + path              = (known after apply)
          + size              = 40
          + storage_policy_id = (known after apply)
          + thin_provisioned  = true
          + unit_number       = 0
          + uuid              = (known after apply)
          + write_through     = false
        }

      + network_interface {
          + adapter_type          = "vmxnet3"
          + bandwidth_limit       = -1
          + bandwidth_reservation = 0
          + bandwidth_share_count = (known after apply)
          + bandwidth_share_level = "normal"
          + device_address        = (known after apply)
          + key                   = (known after apply)
          + mac_address           = (known after apply)
          + network_id            = "dvportgroup-46087"
        }
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

module.rke-prod-sc-03.vsphere_virtual_machine.ovf_deploy: Creating...
module.rke-prod-sc-02.vsphere_virtual_machine.ovf_deploy: Creating...
module.rke-prod-sc-01.vsphere_virtual_machine.ovf_deploy: Creating...

│ Error: folder '/datacenter/vm/datacenter/vm/demos/suse/rancher' not found

│   with module.rke-prod-sc-01.vsphere_virtual_machine.ovf_deploy,
│   on modules/ovf_deploy/main.tf line 10, in resource "vsphere_virtual_machine" "ovf_deploy":
│   10: resource "vsphere_virtual_machine" "ovf_deploy" {



│ Error: folder '/datacenter/vm/datacenter/vm/demos/suse/rancher' not found

│   with module.rke-prod-sc-02.vsphere_virtual_machine.ovf_deploy,
│   on modules/ovf_deploy/main.tf line 10, in resource "vsphere_virtual_machine" "ovf_deploy":
│   10: resource "vsphere_virtual_machine" "ovf_deploy" {



│ Error: folder '/datacenter/vm/datacenter/vm/demos/suse/rancher' not found

│   with module.rke-prod-sc-03.vsphere_virtual_machine.ovf_deploy,
│   on modules/ovf_deploy/main.tf line 10, in resource "vsphere_virtual_machine" "ovf_deploy":
│   10: resource "vsphere_virtual_machine" "ovf_deploy" {


ansible@runner00:~/git/gitlab/rke_poc/acme.local/vmware_deploy$ terraform -v
Terraform v1.5.7
on linux_amd64
+ provider registry.terraform.io/hashicorp/vsphere v2.4.3`

Expected Behavior

Deploy vm in vcenter folder <datacenter>/vm/demos/suse/rancher.

Actual Behavior

Error not able to find path.

Steps to Reproduce

I can get vm deploy to complete if I remark out folder module and vm placement TF call.

Environment Details

I also tried with removal of datacenter/vm and it gave different result.

I can post as followup.

Screenshots

No response

References

No response

@penguinpages penguinpages added bug Type: Bug needs-triage Status: Issue Needs Triage labels Oct 2, 2023
@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Hello, penguinpages! 🖐

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.

@penguinpages
Copy link
Author

penguinpages commented Oct 3, 2023

Update Posting

vmfolder

Create new folder test_folder.

vmfolder_new

data "vsphere_folder" "rancher" {
  path = "datacenter/vm/test_folder"
}

vmfolder_deploy_success

So this is NOT a bug with the concept of the provider but ... that it can't handle sub folders

vmfolder_deploy_success

data "vsphere_folder" "rancher" {
  path = "datacenter/vm/test_folder/subfolder"
}
  Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

module.rke-prod-sc-03.vsphere_virtual_machine.ovf_deploy: Creating...
module.rke-prod-sc-01.vsphere_virtual_machine.ovf_deploy: Creating...
module.rke-prod-sc-02.vsphere_virtual_machine.ovf_deploy: Creating...

│ Error: folder '/datacenter/vm/datacenter/vm/test_folder/subfolder' not found

│   with module.rke-prod-sc-02.vsphere_virtual_machine.ovf_deploy,
│   on modules/ovf_deploy/main.tf line 10, in resource "vsphere_virtual_machine" "ovf_deploy":
│   10: resource "vsphere_virtual_machine" "ovf_deploy" {`

@tenthirtyam
Copy link
Collaborator

@penguinpages
Copy link
Author

penguinpages commented Oct 3, 2023

I did a review of your above code and your code

data "vsphere_folder" "folder" {
  path = "/${data.vsphere_datacenter.datacenter.name}/vm/${var.vsphere_folder}"
}

data "vsphere_virtual_machine" "template" {
  name          = var.vsphere_template
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

resource "vsphere_virtual_machine" "vm" {
  name                    = var.vm_name
  folder                  = trimprefix(data.vsphere_folder.folder.path, "/${data.vsphere_datacenter.datacenter.name}/vm") 
  num_cpus                = var.vm_cpus

Would still place VMs within default folder under datacenter. Vs many cusotmers expect VMs to be deployed within folders with subfolders to organize and create controls of permissions for developers.

Ex:

<datacenter>/vm(type of folder)/production/web_servers/
<datacenter>/vm(type of folder)/dev/web_servers/
<datacenter>/vm(type of folder)/app1/db/

etc...

this is a very typical logic of how to organize and control VMs and so module needs to deploy with subdirectory logic.

@FredPraca
Copy link

Just as a remark, the problem remains with v2.6.1 and v2.7.0

@FredPraca
Copy link

As a unrelated remark, would using a data exported vsphere_folder id inside the vsphere_virtual_machine be useful ?
Generally I set it once in a configuration module and use it for all my virtual machine.
But I can understand that directly typing the path as a string allows string manipulation.

@tenthirtyam tenthirtyam added this to the Backlog milestone Jun 12, 2024
@tenthirtyam
Copy link
Collaborator

THe ability to place virtual machines in a nested folder can certainly be achieved in the provider by taking the base inventory path into consideration.

Extending my prior example, below is an example you could run in your own environment that deploys two basic Ubuntu 24.04 LTS cloud image into seperate nested folders.

terraform {
  required_providers {
    vsphere = {
      source  = "hashicorp/vsphere"
      version = ">= 2.8.1"
    }
  }
  required_version = ">= 1.8.5"
}

provider "vsphere" {
  vsphere_server       = "m01-vc01.rainpole.io"
  user                 = "[email protected]"
  password             = "VMware1!"
  allow_unverified_ssl = true
}

data "vsphere_datacenter" "datacenter" {
  name = "m01-dc01"
}

data "vsphere_datastore" "datastore" {
  name          = "local-ssd-01"
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_compute_cluster" "cluster" {
  name          = "m01-cl01"
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_resource_pool" "pool" {
  name          = format("%s%s", data.vsphere_compute_cluster.cluster.name, "/Resources")
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_host" "host" {
  name          = "m01-esx01.rainpole.io"
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_network" "network" {
  name          = "DHCP"
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_folder" "folder-1" {
  path = "/${data.vsphere_datacenter.datacenter.name}/vm/foo/bar/baz/"
}

data "vsphere_folder" "folder-2" {
  path = "/${data.vsphere_datacenter.datacenter.name}/vm/hello/world/"
}

data "vsphere_ovf_vm_template" "ovf" {
  name             = "ubuntu-24.04-server-cloudimg-amd64"
  resource_pool_id = data.vsphere_resource_pool.pool.id
  datastore_id     = data.vsphere_datastore.datastore.id
  host_system_id   = data.vsphere_host.host.id
  remote_ovf_url   = "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.ova"
  ovf_network_map = {
    "VM Network" : data.vsphere_network.network.id
  }
}

resource "vsphere_virtual_machine" "ubuntu-cloud-image-01" {
  name                 = "ubuntu-01"
  folder               = trimprefix(data.vsphere_folder.folder-1.path, "/${data.vsphere_datacenter.datacenter.name}/vm")
  resource_pool_id     = data.vsphere_resource_pool.pool.id
  datastore_id         = data.vsphere_datastore.datastore.id
  datacenter_id        = data.vsphere_datacenter.datacenter.id
  host_system_id       = data.vsphere_host.host.id
  num_cpus             = data.vsphere_ovf_vm_template.ovf.num_cpus
  num_cores_per_socket = data.vsphere_ovf_vm_template.ovf.num_cores_per_socket
  memory               = data.vsphere_ovf_vm_template.ovf.memory
  guest_id             = data.vsphere_ovf_vm_template.ovf.guest_id
  dynamic "network_interface" {
    for_each = data.vsphere_ovf_vm_template.ovf.ovf_network_map
    content {
      network_id = network_interface.value
    }
  }
  wait_for_guest_net_timeout = 0
  wait_for_guest_ip_timeout  = 0

  ovf_deploy {
    allow_unverified_ssl_cert = true
    remote_ovf_url            = "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.ova"
    disk_provisioning         = "thin"
    ovf_network_map           = data.vsphere_ovf_vm_template.ovf.ovf_network_map
  }
  cdrom {
    client_device = true
  }

  vapp {
    properties = {
      "hostname"    = "ubuntu-01"
      "instance-id" = "ubuntu-01"
      "password"    = "VMware1!"
    }
  }

  lifecycle {
    ignore_changes = [
      vapp[0].properties,
    ]
  }
}


resource "vsphere_virtual_machine" "ubuntu-cloud-image-02" {
  name                 = "ubuntu-02"
  folder               = trimprefix(data.vsphere_folder.folder-2.path, "/${data.vsphere_datacenter.datacenter.name}/vm")
  resource_pool_id     = data.vsphere_resource_pool.pool.id
  datastore_id         = data.vsphere_datastore.datastore.id
  datacenter_id        = data.vsphere_datacenter.datacenter.id
  host_system_id       = data.vsphere_host.host.id
  num_cpus             = data.vsphere_ovf_vm_template.ovf.num_cpus
  num_cores_per_socket = data.vsphere_ovf_vm_template.ovf.num_cores_per_socket
  memory               = data.vsphere_ovf_vm_template.ovf.memory
  guest_id             = data.vsphere_ovf_vm_template.ovf.guest_id
  dynamic "network_interface" {
    for_each = data.vsphere_ovf_vm_template.ovf.ovf_network_map
    content {
      network_id = network_interface.value
    }
  }
  wait_for_guest_net_timeout = 0
  wait_for_guest_ip_timeout  = 0

  ovf_deploy {
    allow_unverified_ssl_cert = true
    remote_ovf_url            = "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.ova"
    disk_provisioning         = "thin"
    ovf_network_map           = data.vsphere_ovf_vm_template.ovf.ovf_network_map
  }
  cdrom {
    client_device = true
  }

  vapp {
    properties = {
      "hostname"    = "ubuntu-02"
      "instance-id" = "ubuntu-02"
      "password"    = "VMware1!"
    }
  }

  lifecycle {
    ignore_changes = [
      vapp[0].properties,
    ]
  }
}

The run:

terraform apply -auto-approve                           
data.vsphere_datacenter.datacenter: Reading...
data.vsphere_datacenter.datacenter: Read complete after 0s [id=datacenter-3]
data.vsphere_folder.folder-1: Reading...
data.vsphere_network.network: Reading...
data.vsphere_folder.folder-2: Reading...
data.vsphere_compute_cluster.cluster: Reading...
data.vsphere_host.host: Reading...
data.vsphere_datastore.datastore: Reading...
data.vsphere_folder.folder-2: Read complete after 0s [id=group-v35005]
data.vsphere_network.network: Read complete after 0s [id=network-18085]
data.vsphere_folder.folder-1: Read complete after 0s [id=group-v35008]
data.vsphere_compute_cluster.cluster: Read complete after 0s [id=domain-c18070]
data.vsphere_datastore.datastore: Read complete after 0s [id=datastore-18076]
data.vsphere_resource_pool.pool: Reading...
data.vsphere_host.host: Read complete after 0s [id=host-18072]
data.vsphere_resource_pool.pool: Read complete after 0s [id=resgroup-18071]
data.vsphere_ovf_vm_template.ovf: Reading...
data.vsphere_ovf_vm_template.ovf: Read complete after 1s [id=ubuntu-24.04-server-cloudimg-amd64]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # vsphere_virtual_machine.ubuntu-cloud-image-01 will be created
  + resource "vsphere_virtual_machine" "ubuntu-cloud-image-01" {
      + annotation                              = (known after apply)
      + boot_retry_delay                        = 10000
      + change_version                          = (known after apply)
      + cpu_limit                               = -1
      + cpu_share_count                         = (known after apply)
      + cpu_share_level                         = "normal"
      + datacenter_id                           = "datacenter-3"
      + datastore_id                            = "datastore-18076"
      + default_ip_address                      = (known after apply)
      + ept_rvi_mode                            = "automatic"
      + extra_config_reboot_required            = true
      + firmware                                = "bios"
      + folder                                  = "foo/bar/baz"
      + force_power_off                         = true
      + guest_id                                = "ubuntu64Guest"
      + guest_ip_addresses                      = (known after apply)
      + hardware_version                        = (known after apply)
      + host_system_id                          = "host-18072"
      + hv_mode                                 = "hvAuto"
      + id                                      = (known after apply)
      + ide_controller_count                    = 2
      + imported                                = (known after apply)
      + latency_sensitivity                     = "normal"
      + memory                                  = 1024
      + memory_limit                            = -1
      + memory_share_count                      = (known after apply)
      + memory_share_level                      = "normal"
      + migrate_wait_timeout                    = 30
      + moid                                    = (known after apply)
      + name                                    = "ubuntu-01"
      + num_cores_per_socket                    = 0
      + num_cpus                                = 2
      + power_state                             = (known after apply)
      + poweron_timeout                         = 300
      + reboot_required                         = (known after apply)
      + resource_pool_id                        = "resgroup-18071"
      + run_tools_scripts_after_power_on        = true
      + run_tools_scripts_after_resume          = true
      + run_tools_scripts_before_guest_shutdown = true
      + run_tools_scripts_before_guest_standby  = true
      + sata_controller_count                   = 0
      + scsi_bus_sharing                        = "noSharing"
      + scsi_controller_count                   = 1
      + scsi_type                               = "pvscsi"
      + shutdown_wait_timeout                   = 3
      + storage_policy_id                       = (known after apply)
      + swap_placement_policy                   = "inherit"
      + sync_time_with_host                     = true
      + tools_upgrade_policy                    = "manual"
      + uuid                                    = (known after apply)
      + vapp_transport                          = (known after apply)
      + vmware_tools_status                     = (known after apply)
      + vmx_path                                = (known after apply)
      + wait_for_guest_ip_timeout               = 0
      + wait_for_guest_net_routable             = true
      + wait_for_guest_net_timeout              = 0

      + cdrom {
          + client_device  = true
          + device_address = (known after apply)
          + key            = (known after apply)
        }

      + network_interface {
          + adapter_type          = "vmxnet3"
          + bandwidth_limit       = -1
          + bandwidth_reservation = 0
          + bandwidth_share_count = (known after apply)
          + bandwidth_share_level = "normal"
          + device_address        = (known after apply)
          + key                   = (known after apply)
          + mac_address           = (known after apply)
          + network_id            = "network-18085"
        }

      + ovf_deploy {
          + allow_unverified_ssl_cert = true
          + disk_provisioning         = "thin"
          + enable_hidden_properties  = false
          + ovf_network_map           = {
              + "VM Network" = "network-18085"
            }
          + remote_ovf_url            = "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.ova"
        }

      + vapp {
          + properties = {
              + "hostname"    = "ubuntu-01"
              + "instance-id" = "ubuntu-01"
              + "password"    = "VMware1!"
            }
        }
    }

  # vsphere_virtual_machine.ubuntu-cloud-image-02 will be created
  + resource "vsphere_virtual_machine" "ubuntu-cloud-image-02" {
      + annotation                              = (known after apply)
      + boot_retry_delay                        = 10000
      + change_version                          = (known after apply)
      + cpu_limit                               = -1
      + cpu_share_count                         = (known after apply)
      + cpu_share_level                         = "normal"
      + datacenter_id                           = "datacenter-3"
      + datastore_id                            = "datastore-18076"
      + default_ip_address                      = (known after apply)
      + ept_rvi_mode                            = "automatic"
      + extra_config_reboot_required            = true
      + firmware                                = "bios"
      + folder                                  = "hello/world"
      + force_power_off                         = true
      + guest_id                                = "ubuntu64Guest"
      + guest_ip_addresses                      = (known after apply)
      + hardware_version                        = (known after apply)
      + host_system_id                          = "host-18072"
      + hv_mode                                 = "hvAuto"
      + id                                      = (known after apply)
      + ide_controller_count                    = 2
      + imported                                = (known after apply)
      + latency_sensitivity                     = "normal"
      + memory                                  = 1024
      + memory_limit                            = -1
      + memory_share_count                      = (known after apply)
      + memory_share_level                      = "normal"
      + migrate_wait_timeout                    = 30
      + moid                                    = (known after apply)
      + name                                    = "ubuntu-02"
      + num_cores_per_socket                    = 0
      + num_cpus                                = 2
      + power_state                             = (known after apply)
      + poweron_timeout                         = 300
      + reboot_required                         = (known after apply)
      + resource_pool_id                        = "resgroup-18071"
      + run_tools_scripts_after_power_on        = true
      + run_tools_scripts_after_resume          = true
      + run_tools_scripts_before_guest_shutdown = true
      + run_tools_scripts_before_guest_standby  = true
      + sata_controller_count                   = 0
      + scsi_bus_sharing                        = "noSharing"
      + scsi_controller_count                   = 1
      + scsi_type                               = "pvscsi"
      + shutdown_wait_timeout                   = 3
      + storage_policy_id                       = (known after apply)
      + swap_placement_policy                   = "inherit"
      + sync_time_with_host                     = true
      + tools_upgrade_policy                    = "manual"
      + uuid                                    = (known after apply)
      + vapp_transport                          = (known after apply)
      + vmware_tools_status                     = (known after apply)
      + vmx_path                                = (known after apply)
      + wait_for_guest_ip_timeout               = 0
      + wait_for_guest_net_routable             = true
      + wait_for_guest_net_timeout              = 0

      + cdrom {
          + client_device  = true
          + device_address = (known after apply)
          + key            = (known after apply)
        }

      + network_interface {
          + adapter_type          = "vmxnet3"
          + bandwidth_limit       = -1
          + bandwidth_reservation = 0
          + bandwidth_share_count = (known after apply)
          + bandwidth_share_level = "normal"
          + device_address        = (known after apply)
          + key                   = (known after apply)
          + mac_address           = (known after apply)
          + network_id            = "network-18085"
        }

      + ovf_deploy {
          + allow_unverified_ssl_cert = true
          + disk_provisioning         = "thin"
          + enable_hidden_properties  = false
          + ovf_network_map           = {
              + "VM Network" = "network-18085"
            }
          + remote_ovf_url            = "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.ova"
        }

      + vapp {
          + properties = {
              + "hostname"    = "ubuntu-02"
              + "instance-id" = "ubuntu-02"
              + "password"    = "VMware1!"
            }
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.
vsphere_virtual_machine.ubuntu-cloud-image-02: Creating...
vsphere_virtual_machine.ubuntu-cloud-image-01: Creating...
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [10s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [10s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [20s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [20s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [30s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [30s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [40s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [40s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [50s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [50s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [1m0s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [1m0s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [1m10s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [1m10s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [1m20s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [1m20s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [1m30s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [1m30s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [1m40s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [1m40s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [1m50s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [1m50s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [2m0s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [2m0s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [2m10s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [2m10s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [2m20s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [2m20s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Still creating... [2m30s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [2m30s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-02: Creation complete after 2m38s [id=421e6109-bfeb-fb11-d5a0-0ee854f7b91d]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [2m40s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Still creating... [2m50s elapsed]
vsphere_virtual_machine.ubuntu-cloud-image-01: Creation complete after 2m58s [id=421ec242-1f61-c343-6826-5aa6244e4393]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

The results:

    {
      "mode": "data",
      "type": "vsphere_folder",
      "name": "folder-1",
      "provider": "provider[\"registry.terraform.io/hashicorp/vsphere\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "group-v35008",
            "path": "/m01-dc01/vm/foo/bar/baz/"
          },
          "sensitive_attributes": []
        }
      ]
    },
    {
      "mode": "data",
      "type": "vsphere_folder",
      "name": "folder-2",
      "provider": "provider[\"registry.terraform.io/hashicorp/vsphere\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "group-v35005",
            "path": "/m01-dc01/vm/hello/world/"
          },
          "sensitive_attributes": []
        }
      ]
    },
image

@tenthirtyam tenthirtyam added question/provider Question: Provider and removed needs-triage Status: Issue Needs Triage labels Jun 14, 2024
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2024
@tenthirtyam tenthirtyam removed this from the Backlog milestone Sep 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Type: Bug question/provider Question: Provider
Projects
None yet
Development

No branches or pull requests

3 participants