1

I have setup a Windows server and installed ssh using Chocolatey. If I run this manually I have no problems connecting and running my commands. When I try to use Terraform to run my commands it connects successfully but doesn't run any commands.

I started by using winrm and then I could run commands but due to some problem with creating a service fabric cluster over winrm I decided to try using ssh instead and when running things manually it worked and the cluster went up. So that seems to be the way forward.

I have setup a Linux VM and got ssh working by using the private key. So I have tried to use the same config as I did with the Linux VM on the Windows but it still asked me to use my password.

What could the reason be for being able to run commands over ssh manually and using Terraform only connect but no commands are run? I am running this on OpenStack with Windows 2016

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)

Here is the script im using to run the commands:

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"
    ]
  }
}
user1842278
  • 1,039
  • 1
  • 12
  • 25

1 Answers1

0

Put the connection block inside the provisioner block:

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

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