【发布时间】:2021-09-29 19:24:06
【问题描述】:
我想在我的虚拟机中存储私有 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 存储在我的实例中
【问题讨论】:
-
让您的生活更轻松,在您的 bash 脚本中使用 gcp 云实例元数据
标签: terraform terraform-provider-gcp