remote-exec to azure linux vm with public ip

347 views
Skip to first unread message

Franco König

unread,
May 29, 2019, 3:50:41 AM5/29/19
to Terraform
hi all

how it is possible to make a remote-exec over ssh to a linux vm in azure with public ip? i try to add azurerm_public_ip.ipaddress but that not works. when i try it with the dns hostname that will work, but is it not possible to add public ip correct in azurerm provider to the connection string on the remote-exec block?

regards
frank

Andrew Savinykh

unread,
May 29, 2019, 5:09:36 AM5/29/19
to terrafo...@googlegroups.com
There is no trick here, it just works.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/b44953e0-c326-472f-aa36-2e0271ddb313%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Franco König

unread,
May 29, 2019, 5:21:01 AM5/29/19
to Terraform
and can you show me that in a example? i am not sure if i make it wright!

my code is:
resource "azurerm_public_ip" "vm" {
 count                        = var.nb_public_ip
 name                         = "${var.prefix}pip-${format("%02d", count.index + 1)}"
 location                     = var.location
 resource_group_name          = azurerm_resource_group.vm.name
 public_ip_address_allocation = var.public_ip_address_allocation
 domain_name_label            = "${var.vm_hostname}${format("%02d", count.index + 1)}"
 tags                         = var.tags
}

with fqdn hostname is it working so i have this code:

provisioner "remote-exec" {
   connection {
     type     = "ssh"
     host     = "${var.vm_hostname}${format("%02d", count.index + 1)}.westeurope.cloudapp.azure.com"
     user     = var.admin_username
     password = var.admin_password
   }

    inline = [
     "sudo apt-add-repository ppa:ansible/ansible -y",
     "sudo apt-get update -y",
     "sudo apt-get install ansible -y",
     "sudo apt-get upgrade -y",
     "sudo apt-get install htop -y",
     "sudo shutdown -r -t 10",
     "exit 0",
   ]
 }



Am Mittwoch, 29. Mai 2019 11:09:36 UTC+2 schrieb Andrew Savinykh:
There is no trick here, it just works.

On Wed, May 29, 2019 at 7:50 PM Franco König <franco...@gmail.com> wrote:
hi all

how it is possible to make a remote-exec over ssh to a linux vm in azure with public ip? i try to add azurerm_public_ip.ipaddress but that not works. when i try it with the dns hostname that will work, but is it not possible to add public ip correct in azurerm provider to the connection string on the remote-exec block?

regards
frank

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terrafo...@googlegroups.com.

Andrew Savinykh

unread,
May 29, 2019, 5:30:24 AM5/29/19
to terrafo...@googlegroups.com
Does this:  https://www.terraform.io/docs/providers/azurerm/r/public_ip.html#attributes-reference help? It says to use the data source and that's what worked for me:

data "azurerm_public_ip" "test" {
  name                = "linuxhost-ip"
  resource_group_name = "linux"
}

And then

  connection {
    host     = data.azurerm_public_ip.test.ip_address
    type     = "ssh"
...
  }


To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/97944c45-ff3b-462e-a49e-c4bd842a841e%40googlegroups.com.

Andrew Savinykh

unread,
May 29, 2019, 5:34:04 AM5/29/19
to terrafo...@googlegroups.com

Franco König

unread,
May 29, 2019, 7:58:36 AM5/29/19
to Terraform
did not work for me:

data "azurerm_public_ip" "vm" {
  count               = var.nb_instances
  name                = "${element(azurerm_public_ip.vm.*.name, count.index)}"
  resource_group_name = "${azurerm_resource_group.vm.name}"
}

provisioner "remote-exec" {
    connection {
      type     = "ssh"
      #host    = "${var.vm_hostname}${format("%02d", count.index + 1)}.westeurope.cloudapp.azure.com"
      host     = "${element(data.azurerm_public_ip.vm.*.ip_address, count.index)}"
      user     = var.admin_username
      password = var.admin_password
    }

azurerm_virtual_machine.vm-linux[0]: Still creating... [7m30s elapsed]
Error: timeout - last error: dial tcp :22: connectex: No connection could be made because the target machine actively refused it.
Reply all
Reply to author
Forward
0 new messages