gpt4 book ai didi

google-cloud-platform - Terraform:无法使用生成的 SSH key 在计算引擎 VM 上远程执行

转载 作者:行者123 更新时间:2023-12-02 02:45:20 26 4
gpt4 key购买 nike

我正在尝试使用带有 Terraform 的 ssh key 在新配置的 Google Cloud Platform Compute Engine VM 上远程执行一些命令。这是我的代码:

resource "tls_private_key" "ssh-key" {
algorithm = "RSA"
rsa_bits = 4096
}

resource "google_compute_instance" "static-content" {

# ...

metadata {
sshKeys = "root:${tls_private_key.ssh-key.public_key_openssh}"
}

connection {
type = "ssh"
user = "root"
private_key = "${tls_private_key.ssh-key.private_key_pem}"
}

provisioner "remote-exec" {
inline = [
"curl -L https://github.com/aelsabbahy/goss/releases/download/v0.3.6/goss-linux-amd64 -o ~/goss",
"chmod +x ~/goss",
"~/goss -g ~/gossfile.yml validate",
]
}

}

我在 Terraform apply 中得到的输出是

google_compute_instance.static-content: Still creating... (2m10s elapsed)
google_compute_instance.static-content (remote-exec): Connecting to remote host via SSH...
google_compute_instance.static-content (remote-exec): Host: 35.198.166.131
google_compute_instance.static-content (remote-exec): User: root
google_compute_instance.static-content (remote-exec): Password: false
google_compute_instance.static-content (remote-exec): Private key: true
google_compute_instance.static-content (remote-exec): SSH Agent: false
google_compute_instance.static-content (remote-exec): Checking Host Key: false

所以看起来 ssh key 没有正确传播到 VM。为什么这不起作用的任何提示?

最佳答案

看起来你只是以不同的方式尝试过,试试下面对我有用的代码

provisioner "remote-exec" {
connection {
type = "ssh"
port = 22
user = "username"
agent = "false"
private_key = "${file("/path/to/your/pem_file")}"
}
inline = [
"your command goes here",
]
}
}

关于google-cloud-platform - Terraform:无法使用生成的 SSH key 在计算引擎 VM 上远程执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55614545/

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