user312307
user312307

Reputation: 140

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
}

Upvotes: 0

Views: 198

Answers (2)

Marko E
Marko E

Reputation: 18213

If you want to output all the values for a given attribute when using the for_each meta-argument, you can use the values built-in function:

output "vm_uuids" {
  value = values(vsphere_virtual_machine.vm)[*].uuid
}

EDIT: As per the added code, the change should be as follows for this to work:

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
}

module abc {
  source   = "../../abc"
  for_each = var.vms
  re       = vsphere_virtual_machine.vm[each.key].uuid
}

Alternatively, you could use resource chaining with for_each:

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
}

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

Upvotes: 2

Helder Sepulveda
Helder Sepulveda

Reputation: 17664

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
}

Upvotes: 2

Related Questions