简体   繁体   English

如何从使用 terraform-google-modules 创建的模块中打印 private IP

[英]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_templategoogle_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重复了该模块,以创建具有不同参数的实例组。

  • Say 2 instances each with hostname starting with some prefix in 2 groups说 2 个实例,每个实例的主机名都以 2 组中的某个前缀开头

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.

相关问题 如何使用 Terraform 制作 API 模块? - How to make API modules using Terraform? 如何动态获取terraform中创建的rds实例的server IP - how to dynamically get the server IP of rds instance created in terraform 如何使用 Python 和私有 IP 从 GKE 连接到 Cloud SQL - How to connect from GKE to Cloud SQL using Python and Private IP 如何使用 CDKTF 创建的 terraform 部署 Lambda? - How to deploy Lambda using terraform created by CDKTF? 如何从我的电脑连接到谷歌云平台中私有 ip 的 sql 实例 - How to connect from my pc to sql instance with private ip in google cloud platform 使用 Terraform,如何将用户列表从一个模块传递到另一个模块 - Using Terraform, how can I pass in a list of users from module to module 想要使用 terraform 在 gcp 中的单独 vpc 上创建具有私有和公共 ip 的云 sql 实例 - Want to create a cloud sql instance with private and public ip on a separate vpc in gcp using terraform Google CloudSQL - 如何删除由 terraform 创建的超级用户权限 - Google CloudSQL - How to remove remove superuser permissions created by terraform 使用 Terraform 将文件传递给新创建的 ec2 实例,而不共享“连接”部分中的私钥 - using Terraform to pass a file to newly created ec2 instance without sharing the private key in "connection" section 如何使用 terraform 将 EIP 附加到现有的 ENI(在 terraform 之外创建) - How to attach EIP to a existing ENI (Created outside of terraform) using terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM