[英]how to print private IP from the module created using terraform-google-modules
I have created some gcp instances using terraform module:我使用 terraform 模块创建了一些 gcp 实例:
module "instance_template" {
source = "terraform-google modules/vm/google//modules/instance_template"
...
}
module "compute_instance" {
source = "terraform-google-
modules/vm/google//modules/compute_instance"
num_instances = 4
...
}
then how do I get and output the private ip of these 4 instances after I run terraform apply?那么在我运行 terraform apply 之后,如何获取这 4 个实例的私有 ip 和 output?
This module does not have output as private Ips.此模块没有 output 作为私有 Ips。 It has only outputs instances_self_links and available_zones它只有输出 instance_self_links 和 available_zones
Better to use, resource block of google_compute_instance_template
and google_compute_instance_from_template
更好用, google_compute_instance_template
和google_compute_instance_from_template
的资源块
Then you can use output block to fetch all 4 private ips然后你可以使用 output 块来获取所有 4 个私有 ips
output {
value = google_compute_instance_from_template.instances[*].network_ip
}
The module outputs instances_details
from which you can get the ip addresses.该模块输出instances_details
,您可以从中获取 ip 地址。
Below is an example to get the IPs of all the instances created下面是获取所有创建的实例的IP的示例
output "vm-ips" {
value = flatten(module.compute_instance[*].instances_details.*.network_interface.0.network_ip)
}
Output: Output:
vm-ips = [
"10.128.0.14",
"10.128.0.15",
"10.128.0.16",
]
In you had repeated the module with for-each
to create groups of instances with different params.在其中,您使用for-each
重复了该模块,以创建具有不同参数的实例组。
Then, you can get all their IPs as follows:然后,你可以得到他们所有的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
}
]
]
])
}
Output: Output:
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"
},
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.