instances_details
您可以从中获取 IP 地址的模块输出。
下面是获取创建的所有实例的 IP 的示例
output "vm-ips" {
value = flatten(module.compute_instance[*].instances_details.*.network_interface.0.network_ip)
}
输出:
vm-ips = [
"10.128.0.14",
"10.128.0.15",
"10.128.0.16",
]
在您重复该模块for-each
以创建具有不同参数的实例组。
- 说 2 个实例,每个实例的主机名都以 2 组中的某个前缀开头
然后,您可以按如下方式获取他们的所有 IP:
output "vm-ips" {
value = flatten([
for group in module.compute_instance[*] : [
for vm_details in group: [
for detail in vm_details.instances_details: {
"name" = detail.name
"ip" = detail.network_interface.0.network_ip
}
]
]
])
}
输出:
vm-ips = [
{
"ip" = "10.128.0.17"
"name" = "w1-001"
},
{
"ip" = "10.128.0.18"
"name" = "w1-002"
},
{
"ip" = "10.128.0.20"
"name" = "w2-001"
},
{
"ip" = "10.128.0.19"
"name" = "w2-002"
},
]