Remote exec execution error

Error: remote-exec provisioner error

│ with azurerm_virtual_machine.example,
│ on main.tf line 380, in resource “azurerm_virtual_machine” “example”:
│ 380: provisioner “remote-exec” {

│ host for provisioner cannot be empty

coed:

terraform {

required_version = “>=0.12”

required_providers {
azurerm = {
source = “hashicorp/azurerm”
version = “~>2.0”
}
}
}
provider “azurerm”{
features {}
}

resource “azurerm_resource_group” “rg” {
name = “my-first-terraform-rg”
location = “eastus”
}

resource “azurerm_virtual_network” “myvnet” {
name = “my-vnet”
address_space = [ “10.0.0.0/16” ]
location = “eastus”
resource_group_name = azurerm_resource_group.rg.name
}

resource “azurerm_subnet” “frontendsubnet” {
name = “frontendSubnet”
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.myvnet.name
address_prefix = “10.0.1.0/24”

}
resource “azurerm_network_security_group” “example” {
name = “acceptanceTestSecurityGroup1”
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

security_rule {
name = “test123”
priority = 100
direction = “Inbound”
access = “Allow”
protocol = “Tcp”
source_port_range = “"
destination_port_range = "

source_address_prefix = “"
destination_address_prefix = "

}

}
resource “azurerm_subnet_network_security_group_association” “example” {
subnet_id = azurerm_subnet.frontendSubnet.id
network_security_group_id = azurerm_network_security_group.example.id
}

resource “azurerm_public_ip” “myvm1publicip” {
name = “pip1”
location = “eastus”
resource_group_name = azurerm_resource_group.rg.name
allocation_method = “Dynamic”
sku = “Basic”
}
data “azurerm_public_ip” “myterraformpublicip” {
name = azurerm_public_ip.myvm1publicip.name
resource_group_name = azurerm_resource_group.rg.name
}

resource “azurerm_network_interface” “myvm1nic” {
name = “myvm1-nic”
location = “eastus”
resource_group_name = azurerm_resource_group.rg.name

ip_configuration {
name = “ipconfig1”
subnet_id = azurerm_subnet.frontendsubnet.id
private_ip_address_allocation = “Dynamic”
public_ip_address_id = azurerm_public_ip.myvm1publicip.id
}
}

resource “azurerm_windows_virtual_machine” “example” {
name = “myvm1”
location = “eastus”
resource_group_name = azurerm_resource_group.rg.name
network_interface_ids = [ azurerm_network_interface.myvm1nic.id ]
size = “Standard_A1_v2”
delete_os_disk_on_termination = true
storage_image_reference {
publisher = “OpenLogic”
offer = “CentOS”
sku = “7.5”
version = “latest”

storage_os_disk {
name = “chiki”
caching = “ReadWrite”
create_option = “FromImage”
managed_disk_type = “Standard_LRS”
}

os_profile {
computer_name = “linuxhost”
admin_username = “terminator”
admin_password = “Password@1234”
}
os_profile_linux_config {
disable_password_authentication = false
}
resource “null_resource” “execute” {
connection {
type = “ssh”
host = azurerm_public_ip.myvm1publicip.ip_address
timeout = “5m”
user = “adminuser”
password = “Password123!”
}

provisioner “remote-exec” {
inline = [
“ls -a”,
“cat newfile.txt”
]
}
}
}

When i ran the above code for the first time i will get the following error:

Error: remote-exec provisioner error

│ with azurerm_virtual_machine.example,
│ on main.tf line 380, in resource “azurerm_virtual_machine” “example”:
│ 380: provisioner “remote-exec” {

│ host for provisioner cannot be empty

At this time the VM is created and a publc ip is already assigned(a.b.c.d)

When i run the same code without any changes for the next time it will run but it would be trying to connect to the IP (a.b.c.d). Whereas this time the VM has been build with another IP .

azurerm_virtual_machine.example (remote-exec): Target Platform: unix
azurerm_virtual_machine.example: Still creating… [6m30s elapsed]
azurerm_virtual_machine.example: Still creating… [6m40s elapsed]
azurerm_virtual_machine.example: Still creating… [6m50s elapsed]
azurerm_virtual_machine.example (remote-exec): Connecting to remote host via SSH…
azurerm_virtual_machine.example (remote-exec): Host: 40.71.162.255
azurerm_virtual_machine.example (remote-exec): User: terminator
azurerm_virtual_machine.example (remote-exec): Password: true
azurerm_virtual_machine.example (remote-exec): Private key: false
azurerm_virtual_machine.example (remote-exec): Certificate: false
azurerm_virtual_machine.example (remote-exec): SSH Agent: false
azurerm_virtual_machine.example (remote-exec): Checking Host Key: false
azurerm_virtual_machine.example (remote-exe

And finally time out.