我正在尝试使用带Terraform的ssh密钥在新配置的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应用中得到的输出是
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密钥未正确传播到VM。有什么提示为什么这行不通吗?
答案 0 :(得分:0)
似乎您只是以其他方式尝试过,请尝试以下对我有用的代码
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",
]
}
}