3

我正在尝试使用带有 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 密钥没有正确传播到虚拟机。任何提示为什么这不起作用?

4

1 回答 1

1

似乎您只是以不同的方式尝试了它,请尝试使用以下对我有用的代码

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",
  ]
}
}
于 2019-04-10T14:21:33.840 回答