如何将文件配置器与 google_compute_instance_template 一起使用? [英] How do I use the file provisioner with google_compute_instance_template?

查看:42
本文介绍了如何将文件配置器与 google_compute_instance_template 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Terraform, I need to copy files to Google Compute Engine instance templates. For this I normally use the file provisioner, but it doesn't work since it depends on SSH connections, which fail due to requiring externally accessible host addresses. Due to the dynamic nature of instance templates, I don't know how to assign externally accessible host addresses to instances.

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

How can I implement copying of files to GCE instances created through instance templates (via Terraform)?

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")}"
  }
}

推荐答案

我能够通过以下配置解决此问题.

I was able to resolve this issue with the following configuration.

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"]
    }
}

我使用的 SSH 密钥是由 gcloud 实例生成的,要复制的文件必须采用如图所示的格式.我确实遇到了使用 ~/ 或仅使用 ./ 来指定文件位置的问题.还需要注意的是,复制的文件是在ubuntu"帐户下的,这似乎是 GCE 默认在镜像上拥有的 ubuntu 上的默认帐户.

The SSH key I used was the one generated by the gcloud instance, and the file to copy has to be in the format as shown. I did run into problems with using ~/ or just ./ to designate where the file is. It's also important to note that the file copied under the "ubuntu" account, which appears to be a default account on ubuntu that GCE has on the image by default.

请注意,我还添加了 type = "ssh" 即使这是连接类型的默认值,因此不需要.我喜欢在我的配置文件中详细说明一些内容,所以我添加了它.

Just to note, I also added the type = "ssh" even though that is the default for the connection type, so it isn't needed. I like to be verbose with some things in my configuration files though, so I added it.

这篇关于如何将文件配置器与 google_compute_instance_template 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆