Reputation: 18144
I want to output each VM created and their UUID e.g
data "vsphere_virtual_machine" "vms" {
count = "${length(var.vm_names)}"
name = "${var.vm_names[count.index]}"
datacenter_id = "12345"
}
output "vm_to_uuid" {
# value = "${data.vsphere_virtual_machine.newvms[count.index].name}"
value = "${data.vsphere_virtual_machine.newvms[count.index].id}"
}
Example output I'm looking for:
"vm_to_uuids":[
{
"name":"node1",
"id":"123456",
},
{
"name":"node2",
"id":"987654",
}
]
Upvotes: 2
Views: 4032
Reputation: 894
One thing you could do (if you wanted exactly that output), is use formatlist(format, args, ...)
data "vsphere_virtual_machine" "vms" {
count = "${length(var.vm_names)}"
name = "${var.vm_names[count.index]}"
datacenter_id = "12345"
}
output "vm_to_uuid" {
value = "${join(",", formatlist("{\"name\": \"%s\", \"id\": \"%s\"}", data.vsphere_virtual_machine.newvms.*.name, data.vsphere_virtual_machine.newvms.*.id))}"
}
Haven't tested the code, but you get the idea. Especially the quote escape is just a guess, but that's easy to figure out from here.
What happens is you take two lists (names and ID's) and format dict strings from each entry, after which you join them together using comma separation.
Upvotes: 1
Reputation: 38982
Use the wildcard attribute in the expression given for the output value to get the list of ids for the created VMs. e.g.
output "vm_to_uuids" {
value = "${data.vsphere_virtual_machine.*.id}"
}
The required syntax provided in your question is one exemption where to prefer function over form. Writing a terraform configuration that provides that isn't straightforward. Perhaps, I suggest to adopt other simpler ways to output this same information.
Names mapped to ids can be output:
output "vm_to_uuids" {
value = "${zipmap(
data.vsphere_virtual_machine.*.name,
data.vsphere_virtual_machine.*.id)}"
}
A map of names and ids can be output in a columnar manner:
output "vm_to_uuids" {
value = "${map("name",
data.vsphere_virtual_machine.*.name,
"id",
data.vsphere_virtual_machine.*.id)}"
}
A list of names and ids can be output in a columnar manner:
output "vm_to_uuids" {
value = "${list(
data.vsphere_virtual_machine.*.name,
data.vsphere_virtual_machine.*.id)}"
}
Upvotes: 4