[英]how to use terraform output in a script
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我现在想在我的 VM 中存储私有 IP 我创建了一个 terraform 资源,创建计算实例,保留 IP,创建磁盘并附加到 VM,然后运行脚本文件 autoo.sh
#!/bin/bash
touch myip.txt
private_ip=$(google_compute_instance.default.network_interface.0.network_ip)
echo "$private_ip" >> myip.txt
and the recourse file is shown below和追索文件如下所示
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
}
so basically I want to store my private IP in my instance所以基本上我想在我的实例中存储我的私有 IP
Terraform remote-exec is the right option I think. Terraform remote-exec 是我认为的正确选择。 You can read more on it here: https://www.terraform.io/docs/language/resources/provisioners/remote-exec.html .您可以在此处阅读更多相关信息: https : //www.terraform.io/docs/language/resources/provisioners/remote-exec.html 。
Hope this helps:希望这可以帮助:
Your script file:你的脚本文件:
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
Your resource would look something like this:您的资源将如下所示:
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"]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.