【问题标题】:how to run a bash script in gcp vm using terraform如何使用 terraform 在 gcp vm 中运行 bash 脚本
【发布时间】:2021-09-17 00:39:24
【问题描述】:

干草伙计们, 我想在 gcp 机器上运行一个脚本,因为我在文件下面创建了一个资源

  disk     = google_compute_disk.default2.id
  instance = google_compute_instance.default.id
} # aatach disk to vm

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"]
} # allow ssh

resource "google_compute_address" "static" {
  name = "vm-public-address"
  project = "fit-visitor-305606"
  region = "asia-south1"
  depends_on = [ google_compute_firewall.firewall ]
} # reserve ip

resource "google_compute_instance" "default" {
  name         = "new"
  machine_type = "custom-8-16384"
  zone         = "asia-south1-a"

  tags = ["foo", "bar"]

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

  network_interface {
    network = "default"

    access_config { 
        nat_ip = google_compute_address.static.address     
    }
  }
  metadata = {
    ssh-keys = "${var.user}:${file(var.publickeypath)}"
  }
  lifecycle {
    ignore_changes = [attached_disk]
  }
    provisioner "file" {
    source      = "autoo.sh"
    destination = "/tmp/autoo.sh"
  }
provisioner "remote-exec" {
    connection {
      host        = google_compute_address.static.address
      type        = "ssh"
      user        = var.user
      timeout     = "500s"
      private_key = file(var.privatekeypath)
    }
    inline = [
      "sudo yum -y install epel-release",
      "sudo yum -y install nginx",
      "sudo nginx -v",
    ]
  }
} # Create VM

resource "google_compute_disk" "default2" {
  name  = "test-disk"
  type  = "pd-balanced"
  zone  = "asia-south1-a"
  image = "centos-7-v20210609"
  size =  100
} # Create Disk 

使用它我可以创建虚拟机和磁盘,还可以将虚拟机附加到磁盘但无法运行我的脚本

错误日志是 =

并且私钥部分工作正常,密钥已分配给 VM,我尝试连接它所连接的密钥,这可能只是配置部分的问题 任何帮助或指导都会非常有帮助...

【问题讨论】:

  • 通常你想在null_resource中做这种事情。照原样,脚本将在创建计算实例后触发,但不会等待您可能需要的资源。空资源通过让您配置触发器使您的计算实例与代码执行分离,以便脚本在您知道它所依赖的所有云资源都已创建之前不会运行。

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


【解决方案1】:

就像错误消息所说,您需要配置器的连接配置。您还需要 remote-exec provisioner 来运行脚本。

    provisioner "file" {
    source = "autoo.sh"
    destination = "/tmp/autoo.sh"
    connection {
        type = "ssh"
        user = var.user
        private_key = file(var.privatekeypath)
    }
  }
    provisioner "remote-exec" {
    inline = [
      "chmod +x /tmp/autoo.sh",
      "cd /tmp",
      "./autoo.sh"
    ]
    connection {
        type = "ssh"
        user = var.user
        private_key = file(var.privatekeypath)
    }

来源:https://stackoverflow.com/a/36668395/5454632

【讨论】:

  • 嗯,这是工作,找到 thnaks 你能告诉我如何运行具有 root 访问权限的脚本
  • 我认为这与您的用户有关。如果您的用户有权以 sudo 权限运行命令,您可以以 root 身份运行它。
猜你喜欢
  • 2019-12-07
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 1970-01-01
  • 2022-09-22
  • 1970-01-01
  • 2022-01-05
相关资源
最近更新 更多