如何在脚本中使用 terraform 输出
how to use terraform output in a script
我想在我的 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
资源文件如下图
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
我认为 Terraform remote-exec 是正确的选择。您可以在此处阅读更多信息: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"]
}
我想在我的 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
资源文件如下图
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
我认为 Terraform remote-exec 是正确的选择。您可以在此处阅读更多信息: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"]
}