我想输出创建的每个VM及其UUID,例如
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}"
}
我正在寻找的示例输出:
"vm_to_uuids":[
{
"name":"node1",
"id":"123456",
},
{
"name":"node2",
"id":"987654",
}
]
在为输出值给定的表达式中使用通配符属性来获取创建的VM的ID列表例如
output "vm_to_uuids" {
value = "${data.vsphere_virtual_machine.*.id}"
}
您的问题中提供的所需语法是一种豁免,在这种情况下,您更喜欢函数而不是形式。编写一个提供这种功能的地形配置并不简单。也许,我建议采用其他更简单的方式来输出同样的信息。
可以输出映射到id的名称:
output "vm_to_uuids" {
value = "${zipmap(
data.vsphere_virtual_machine.*.name,
data.vsphere_virtual_machine.*.id)}"
}
名称和ID的映射可以以列方式输出:
output "vm_to_uuids" {
value = "${map("name",
data.vsphere_virtual_machine.*.name,
"id",
data.vsphere_virtual_machine.*.id)}"
}
名称和ID的列表可以以列方式输出:
output "vm_to_uuids" {
value = "${list(
data.vsphere_virtual_machine.*.name,
data.vsphere_virtual_machine.*.id)}"
}
您可以做的一件事(如果您想要确切的输出(是使用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))}"
}
还没有测试过代码,但你已经明白了。尤其是引号转义只是一种猜测,但从这里很容易弄清楚。
实际情况是,您从每个条目中获取两个列表(名称和ID(并格式化dict字符串,然后使用逗号分隔将它们连接在一起。