terraform provisioner 无法连接到机器

terraform provisoner not able to connect to a machine

这是我的代码,我正在尝试使用 terraform provisioner 配置 puppet 节点,但它显示以下错误,已设置连接主机密码、用户 ID 等

错误是 - 错误:超时 - 最后一个错误:拨号 tcp :22: connectex:无法建立连接,因为目标机器主动拒绝它。

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

错误显示原因由。实际上,我认为使用 remote_exec provisioner 并不是一个好方法。 Terraform 还显示:

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.

对于Azure虚拟机,cloud-init和VM extension比较合适