gpt4 book ai didi

ssh - 如何将文件配置程序与 google_compute_instance_template 结合使用?

转载 作者:行者123 更新时间:2023-12-03 07:23:44 32 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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 key 是 gcloud 实例生成的 key ,要复制的文件必须采用所示格式。我确实遇到了使用 ~/ 或仅 ./ 指定文件位置的问题。还需要注意的是,复制的文件是在“ubuntu”帐户下复制的,该帐户似乎是 GCE 默认在镜像上拥有的 ubuntu 上的默认帐户。

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

关于ssh - 如何将文件配置程序与 google_compute_instance_template 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36591653/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com