Terraform - 如何在模块内引用我的输出?

woo*_*oof 3 terraform terraform-provider-gcp

尝试在 main.tf 中引用我的输出之一时遇到问题。我正在尝试使用实例的公共 IP 地址的输出来尝试使用remote-exec. 以下是文件:

\n

主.tf

\n
module "subnetwork" {\n  source = "../modules/uc1" \n  env                   = "${var.var_env}"\n  company               = "${var.var_company}"\n  depends_on = [\n    module.vpc\n  ]\n\n}\n\noutput "server_private_ip" {\n  value = google_compute_instance.default.network_interface[0].network_ip\n}\n\noutput "server_public_ip" {\n  value = google_compute_instance.default.network_interface[0].access_config[0].nat_ip\n}\n
Run Code Online (Sandbox Code Playgroud)\n

../模块/uc1:

\n
resource "google_compute_instance" "default" {\n  name         = "${format("%s","${var.company}-${var.tester}-${var.env}-${var.var_region_name}-instance1")}"\n  machine_type = "${var.var_machine_type}"\n  zone         = "${var.var_zone_name}"\n\n  tags = ["http", "https", "ssh"]\n\n  boot_disk {\n    initialize_params {\n      image = "${var.var_instance_image}"\n    }\n  }\n\n\n  metadata = {\n    ssh-keys = "root:${file(var.var_ssh)}"\n  }\n\n    \n  provisioner "remote-exec" {\n\n    connection {\n    user = "root"\n    host = module.uc1.server_public_ip <--- here is the error\n    type     = "ssh"\n    private_key = "${file(var.var_ssh)}"\n  }\n\n    script = "./scripts/nginx_instance.sh"\n  }\n\n  network_interface {\n    subnetwork = "${google_compute_subnetwork.public_subnet.name}"\n    access_config {\n      // Ephemeral IP\n    }\n  }\n\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

我遇到的问题是尝试在以下位置引用我的输出公共 IP 地址remote-exec

\n
  provisioner "remote-exec" {\n\n    connection {\n    user = "root"\n    host = module.uc1.server_public_ip\n    type     = "ssh"\n    private_key = "${file(var.var_ssh)}"\n  }\n\n    script = "./scripts/nginx_instance.sh"\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

这是错误:

\n
\xe2\x94\x82   on ..\\modules\\uc1\\instance.tf line 24, in resource "google_compute_instance" "default":  \n\xe2\x94\x82   24:     host = module.uc1.server_public_ip\n\xe2\x94\x82\n\xe2\x94\x82 No module call named "uc1" is declared in module.subnetwork.\n
Run Code Online (Sandbox Code Playgroud)\n

Mar*_*k B 7

模块名称subnetwork不是uc1. uc1是模块源文件所在文件夹的名称。声明模块时:module "subnetwork" {您将其命名为“subnetwork”。参考它是:

host = module.subnetwork.server_public_ip
Run Code Online (Sandbox Code Playgroud)

server_public_ip假设您在模块内声明了一个名为的输出。