0
votes

This is my code and I am trying to configure puppet node using terraform provisioner but its showing following error, have set connection host password,userid etc

The Error is- Error: timeout - last error: dial tcp :22: connectex: No connection could be made because the target machine actively refused it.

resource "azurerm_resource_group" "main" {
  name     = "az-testing"
  location = "West US 2"

}
resource "azurerm_network_security_group" "Customernsg1" {
  name                = "demoNsg1"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name
}

resource "azurerm_network_security_rule" "customerrule1" {
  name                        = "SSH"
  priority                    = 100
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "22"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = azurerm_resource_group.main.name
  network_security_group_name = azurerm_network_security_group.Customernsg1.name
}


resource "azurerm_virtual_network" "main" {
  name                = "vnetwork"
  address_space       = ["*.*.*.*/16"]
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name
}

resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.main.name
  virtual_network_name = azurerm_virtual_network.main.name
  address_prefix       = "*.*.*.*/24"
}

resource "azurerm_subnet_network_security_group_association" "example" {
  subnet_id                     = azurerm_subnet.internal.id
  network_security_group_id     = azurerm_network_security_group.Customernsg1.id
}
resource "azurerm_public_ip" "example" {
  name                    = "test-pip"
  location                = azurerm_resource_group.main.location
  resource_group_name     = azurerm_resource_group.main.name
  allocation_method       = "Dynamic"
}

resource "azurerm_network_interface" "main" {
  name                = "vmnic"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.internal.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.example.id

  }
}


resource "azurerm_virtual_machine" "main" {
  name                  = "demo-vm"
  location              = azurerm_resource_group.main.location
  resource_group_name   = azurerm_resource_group.main.name
  network_interface_ids = [azurerm_network_interface.main.id]
  vm_size               = "Standard_DS1_v2"
  #user_data            = data.template_file.node_userdata.rendered

  # Uncomment this line to delete the OS disk automatically when deleting the VM
  # delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  # delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"

  }
  storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags = {
    environment = "staging"
  }


 provisioner "remote-exec" {
  connection {
    type        = "ssh"
    host        = azurerm_public_ip.example.ip_address
     user        = "testadmin"
    password    = "Password1234!"


  }

    inline = [
"rpm -Uvh $ wget https://apt.puppetlabs.com/puppet6-release-bionic.deb",
"sudo dpkg -i puppet6-release-bionic.deb",
"sudo apt-get install puppet-agent",
"export PATH=/opt/puppetlabs/bin:$PATH",
"puppet config set server $ puppet.demo.local  --section main",
"puppet resource service puppet ensure=running enable=true",
    ]
  }
}

showing following error

1
Any more questions? Do you solve the problem?Charles Xu
Is there anything unexpected in the answer? I didn't see any updates and you also didn't accept it.Charles Xu

1 Answers

0
votes

The error shows the reason that caused by. Actually, I don't think it's a good way to use the remote_exec provisioner. Terraform also shows:

Provisioners should only be used as a last resort. For most common situations there are better alternatives. For more information, see the main Provisioners page.

For the Azure virtual machine, the cloud-init and the VM extension are more appropriate.