如何将文件配置程序与google_compute_instance_template一起使用?

akn*_*ds1 6 ssh google-compute-engine terraform

使用Terraform,我需要将文件复制到Google Compute Engine实例模板.为此,我通常使用文件配置器,但它不起作用,因为它依赖于SSH连接,由于需要外部可访问的主机地址而失败.由于实例模板的动态特性,我不知道如何为实例分配外部可访问的主机地址.

如何实现将文件复制到通过实例模板创建的GCE实例(通过Terraform)?

示例Terraform google_compute_instance_template定义

resource "google_compute_instance_template" "node" {
  name = "kubernetes-node-template"
  machine_type = "g1-small"
  can_ip_forward = true
  tags = ["staging", "node"]

  network_interface {
    network = "default"
  }

  provisioner "file" {
    source = "worker/assets/kubelet.service"
    destination = "/etc/systemd/system/kubelet.service"
  }

  connection {
    user = "core"
    type = "ssh"
    private_key = "${file("~/.ssh/id_rsa")}"
  }
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*ron 5

我可以使用以下配置解决此问题。

resource "google_compute_instance" "hubmud" {
    name = "hubmud"
    machine_type = "f1-micro"

    tags = ["buildserver", "jenkins", "central", "terraformer"]
    tags = [ "http-server" ]

    zone = "us-central1-b"

    disk {
        image = "ubuntu-1404-trusty-v20160406"
    }

    network_interface {
        network = "default"
        access_config {}
    }

    provisioner "file" {
        source = "installations.sh"
        destination = "installations.sh"
        connection {
            type = "ssh"
            user = "ubuntu"
            private_key = "${file("~/.ssh/google_compute_engine")}"
        }
    }

    provisioner "remote-exec" {
        inline = [
          "chmod +x ~/installations.sh",
          "cd ~",
          "./installations.sh"
        ]
        connection {
            type = "ssh"
            user = "ubuntu"
            private_key = "${file("~/.ssh/google_compute_engine")}"
        }

    }

    service_account {
        scopes = ["userinfo-email", "compute-ro", "storage-ro"]
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用的SSH密钥是gcloud实例生成的密钥,要复制的文件必须采用如下所示的格式。我确实遇到了使用~/或仅./指定文件位置的问题。还需要注意的是,复制到“ ubuntu”帐户下的文件,该帐户似乎是GCE默认情况下在映像上ubuntu上的默认帐户。

请注意,type = "ssh"即使连接类型是默认值,我也添加了它,因此不需要它。我喜欢在配置文件中详细说明一些事情,因此添加了它。

  • 我认为这个问题的关键在于作者希望提供一个实例模板,而不是简单的实例。解决方案中应清楚指出这一差异 (3认同)