如何在脚本中使用 terraform 输出 [英] how to use terraform output in a script

查看:32
本文介绍了如何在脚本中使用 terraform 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的虚拟机中存储私有 IP现在我创建了一个 terraform 资源创建计算实例,保留 IP,创建磁盘并附加到 VM,然后运行脚本文件 autoo.sh

I want to Store private IP in my VM for now I created a terraform resource create compute instance , reserve IP , create disk and attach to VM and then run a script file autoo.sh

#!/bin/bash
touch myip.txt
private_ip=$(google_compute_instance.default.network_interface.0.network_ip)
echo "$private_ip" >> myip.txt

资源文件如下所示

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" {
  name         = "rohan-instance"
  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     
    }  } 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 "public_ip" {
  value = google_compute_address.static.address
}
output "Private_ip" {
  value = google_compute_instance.default.network_interface.0.network_ip
}

所以基本上我想将我的私有 IP 存储在我的实例中

so basically I want to store my private IP in my instance

推荐答案

我认为 Terraform remote-exec 是正确的选择.您可以在此处阅读更多信息:https://www.terraform.io/docs/language/resources/provisioners/remote-exec.html.

Terraform remote-exec is the right option I think. You can read more on it here: https://www.terraform.io/docs/language/resources/provisioners/remote-exec.html.

希望这会有所帮助:

您的脚本文件:

cat get-ip.sh
#!/bin/bash
touch myip.txt
#private_ip=$(google_compute_instance.default.network_interface.0.network_ip) # will not work as this is running on remote machine. So, it should be changed to something that can get IP.
private_ip = "127.0..0.1"
echo "$private_ip" >> /tmp/ip.sh

您的资源将如下所示:

resource "null_resource" "write_file" {
  connection {
    host = google_compute_instance.default.ip # depending on how to connect
    user = "<user_name>"                      # depending on how to connect
    private_key = "key_name"                  # depending on how to connect
  }

  provisioner "remote-exec" {
    inline = [
      "chmod +x /tmp/ip.sh",
      "sh /tmp/ip.sh",
    ]
  }
  depends_on = ["google_compute_instance.default"]
}

这篇关于如何在脚本中使用 terraform 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆