Windows上带SSH的Terraform Remote-exec

时间:2019-05-17 09:33:17

标签: ssh terraform windows-server terraform-provider-openstack

我已经安装了Windows服务器并使用Chocolatey安装了ssh。如果我手动运行此命令,则在连接和运行命令时不会出现问题。当我尝试使用Terraform运行命令时,它可以成功连接但不运行任何命令。

我从使用Winrm开始,然后我可以运行命令,但是由于在Winrm上创建服务结构集群存在一些问题,我决定尝试使用ssh代替,并且在手动运行时它可以工作并且集群启动。所以这似乎是前进的方向。

我已经安装了Linux VM,并通过使用私钥使ssh工作。因此,我尝试使用与Windows上的Linux VM相同的配置,但是它仍然要求我使用密码。

为什么能够通过ssh手动运行命令并且仅使用Terraform connect而没有运行命令是什么原因呢?我正在Windows 2016的OpenStack上运行

null_resource.sf_cluster_install (remote-exec): Connecting to remote host via SSH...
null_resource.sf_cluster_install (remote-exec):   Host: 1.1.1.1
null_resource.sf_cluster_install (remote-exec):   User: Administrator
null_resource.sf_cluster_install (remote-exec):   Password: true
null_resource.sf_cluster_install (remote-exec):   Private key: false
null_resource.sf_cluster_install (remote-exec):   SSH Agent: false
null_resource.sf_cluster_install (remote-exec):   Checking Host Key: false
null_resource.sf_cluster_install (remote-exec): Connected!
null_resource.sf_cluster_install: Creation complete after 4s (ID: 5017581117349235118)

这是我用来运行命令的脚本:

resource "null_resource" "sf_cluster_install" {
  # count = "${local.sf_count}"
  depends_on = ["null_resource.copy_sf_package"]

  # Changes to any instance of the cluster requires re-provisioning
  triggers = {
    cluster_instance_ids = "${openstack_compute_instance_v2.sf_servers.0.id}"
  }

  connection = {
    type = "ssh"
    host = "${openstack_networking_floatingip_v2.sf_floatIP.0.address}"
    user = "Administrator"

   # private_key = "${file("~/.ssh/id_rsa")}"

   password = "${var.admin_pass}"
 }

  provisioner "remote-exec" {
    inline = [
      "echo hello",
      "powershell.exe Write-Host hello",
      "powershell.exe New-Item C:/tmp/hello.txt -type file"
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

connection块放在provisioner块内:

provisioner "remote-exec" {
  connection = {
    type = "ssh"
    ...
  }

  inline = [
    "echo hello",
    "powershell.exe Write-Host hello",
    "powershell.exe New-Item C:/tmp/hello.txt -type file"
  ]
}