Terraform:无法使用生成的 SSH 密钥在计算引擎 VM 上远程执行 [英] Terraform: can't remote-exec on a compute engine VM with generated SSH key

查看:46
本文介绍了Terraform:无法使用生成的 SSH 密钥在计算引擎 VM 上远程执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to remotely execute some commands on a freshly provisioned Google Cloud Platform Compute Engine VM using ssh keys with Terraform. Here's my code:

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 中得到的输出是

The output I get in the Terraform apply is

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.任何提示为什么这不起作用?

So it seems like the ssh key is not properly propagated to the VM. Any hints why this doesn't work?

推荐答案

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

it seems like you just tried it in a different way, give it a try with below code which worked for me

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

这篇关于Terraform:无法使用生成的 SSH 密钥在计算引擎 VM 上远程执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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