terraformvmware

Need to retrieve vmware instance UUId for multiple instances


I am provisioning multiple vm's using below templates:

resource "vsphere_virtual_machine" "vm" {
  for_each                = var.vms
  name                    = each.value.name 
  folder                  = var.vsphere_folder
  num_cpus                = var.vm_cpus
  memory                  = var.vm_memory
  firmware                = data.vsphere_virtual_machine.template.firmware
  efi_secure_boot_enabled = data.vsphere_virtual_machine.template.efi_secure_boot_enabled
  guest_id                = data.vsphere_virtual_machine.template.guest_id
  scsi_type               = data.vsphere_virtual_machine.template.scsi_type
  datastore_id            = data.vsphere_datastore.datastore.id
  resource_pool_id        = data.vsphere_resource_pool.pool.id
}

I am looking to retrieve the uuid of these 6 instances as below and it does not work:

vsphere_virtual_machine.vm.*.uuid

How can I get the UUID's?

Code where I am using it:

module abc {
  source   = "../../abc"
  for_each = var.vms
  re       = vsphere_virtual_machine.vm.*.uuid
}

Solution

  • I see where your confusion is and I think is best clarified with an example:

    locals {
      vm = {
        "big" = {
          "uuid" = "foo"
          "name" = "my abc vm"
        },
        "small" = {
          "uuid" = "bar"
          "name" = "my def vm"
        }
      }
    }
    
    output "data" {
      value = values(local.vm)[*].uuid
    }
    
    resource "null_resource" "test" {
      for_each = local.vm
    
      triggers = {
        key = each.key
        uuid = each.value.uuid
        name = each.value.name
      }
    }
    

    in this example the local.vm is the equivalent to your vsphere_virtual_machine.vm

    if we just need the list we can do with the value function as suggested by Marko but if we need to pass information to another resource or a module we can loop with the for_each like you did initially in your vm


    Your module will look something like

    module abc {
      source   = "../../abc"
      for_each = vsphere_virtual_machine.vm
      re       = each.value.uuid
    }