【问题标题】:how to run a script in GCP machine using terraform如何使用 terraform 在 GCP 机器中运行脚本
【发布时间】:2021-09-12 13:09:00
【问题描述】:

我在本地笔记本电脑中有一个名为 auto.sh 的脚本文件,我想在配置后立即在 GCP 机器中运行此脚本文件 我创建了这个 terraform 文件

resource "google_compute_attached_disk" "default3" {
  disk     = google_compute_disk.default2.id
  instance = google_compute_instance.default.id
}

resource "google_compute_instance" "default" {
  name         = "test"
  machine_type = "custom-8-16384"
  zone         = "us-central1-a"

  tags = ["foo", "bar"]

  boot_disk {
    initialize_params {
      image = "centos-cloud/centos-7"
    }
  }

  network_interface {
    network = "default"

    access_config {
      
    }
  }
  metadata_startup_script = "touch abcd.txt" 

  lifecycle {
    ignore_changes = [attached_disk]
  }
}

resource "google_compute_disk" "default2" {
  name  = "test-disk"
  type  = "pd-balanced"
  zone  = "us-central1-a"
  image = "centos-7-v20210609"
  size =  100
}

现在运行良好,我想运行该脚本

【问题讨论】:

    标签: google-cloud-platform terraform terraform-provider-gcp


    【解决方案1】:

    你应该更换

    metadata_startup_script = "touch abcd.txt"  
    

    如果脚本足够短,可以使用内联脚本,或者使用类似的东西

    metadata_startup_script = "${file("/path/to/your/file")}"
    

    从文件中加载

    metadata_startup_script docs

    【讨论】:

    • 我只是替换为这个 metadata_startup_script = "${file("C:/Users/Rohan/Desktop/new/autoo.sh")}" 并在我的 autoo.sh 中包含一个命令触摸 abc.txt 但仍然没有创建
    猜你喜欢
    • 2021-09-17
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2021-07-11
    • 2020-10-19
    • 1970-01-01
    • 2022-01-07
    • 2021-04-05
    相关资源
    最近更新 更多