如何使用 terraform 获取私有 ip

时间:2021-07-22 09:50:18

标签: terraform terraform-provider-gcp

大家好,我创建了一个 GCP 资源,这将创建一个实例并保留 IP 并创建磁盘并附加到 VM 并运行脚本文件。

但现在我想使用输出资源打印实例的私有IP

resource "google_compute_attached_disk" "default3" {
  disk     = google_compute_disk.default2.id
  instance = google_compute_instance.default.id
}
resource "google_compute_firewall" "firewall" {
  name    = "gritfy-firewall-externalssh"
  network = "default"
  allow {    protocol = "tcp"
    ports    = ["22"]
  }  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["externalssh"]
}resource "google_compute_address" "static" {
  name = "vm-public-address"
  project = "fit-visitor-305606"
  region = "asia-south1"
  depends_on = [ google_compute_firewall.firewall ]
} resource "google_compute_instance" "default" {
  machine_type = "custom-8-16384"
  zone         = "asia-south1-a"
  boot_disk {
    initialize_params {
      image = "centos-cloud/centos-7"
    }  }
  network_interface {
    network = "default"
    access_config { 
        nat_ip = google_compute_address.static.address     
    }  }
  metadata = {
    ssh-keys = "${var.user}:${file(var.publickeypath)}"
  }  lifecycle {
    ignore_changes = [attached_disk]
  }    provisioner "file" {
    source = "autoo.sh"
    destination = "/tmp/autoo.sh"
    connection {
        host = google_compute_address.static.address
        type = "ssh"
        user = var.user
        private_key = file(var.privatekeypath)
    }  }
    provisioner "remote-exec" {
    inline = [
      "sudo chmod +x /tmp/autoo.sh",
      "bash /tmp/autoo.sh",
    ]    connection {
        host = google_compute_address.static.address
        type = "ssh"
        user = var.user
        private_key = file(var.privatekeypath)
    }    }
}resource "google_compute_disk" "default2" {
  name  = "test-disk"
  type  = "pd-balanced"
  zone  = "asia-south1-a"
  image = "centos-7-v20210609"
  size =  20
}
#output "privateip" {
#  value = google_compute_instance.default.network_interface[1]
#}

谁能帮我打印计算实例的私有IP

0 个答案:

没有答案