0

在 Azure 中创建 VM 后,我想复制文件并运行一些 shell 命令。我使用provisioner 'file'provisioner 'remote-exec'使用 ssh 密钥创建了 VM。在提供者文件之前一切正常,我收到以下错误:

Error: timeout - last error: dial tcp :22: connect: connection refused

当我这样做时ssh -i id_rsa <username>@<ip_address>,它工作正常。我从 Azure 门户获取此 IP 地址。

这是我的 tf 文件:

resource "azurerm_resource_group" "myterraformgroup" {
    name     = "terrafromresources"
    location = "eastus"
}

resource "azurerm_virtual_network" "myterraformnetwork" {
    name                = "terraformvnet"
    address_space       = ["10.0.0.0/16"]
    location            = "eastus"
    resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"
}

resource "azurerm_network_security_group" "myterraformnsg" {
    name                = "terraformNetworkSecurityGroup"
    location            = "eastus"
    resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"

    security_rule {
        name                       = "SSH"
        priority                   = 100
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    }
}

resource "azurerm_public_ip" "myterraformpublicip" {
    name                         = "myPublicIP"
    location                     = "eastus"
    resource_group_name          = "${azurerm_resource_group.myterraformgroup.name}"
    allocation_method            = "Dynamic"
}

resource "azurerm_linux_virtual_machine" "myterraformvm" {
    name                  = "terraformVM"
    location              = "eastus"
    resource_group_name   = "${azurerm_resource_group.myterraformgroup.name}"
    network_interface_ids = ["${azurerm_network_interface.myterraformnic.id}"]
    size                  = "Standard_DS1_v2"

    computer_name  = "terrafromvm"
    admin_username = "azureuser"
    disable_password_authentication = true

    admin_ssh_key {
        username       = "azureuser"
        public_key     = "${file("id_rsa.pub")}"
    }

    connection {
        type = "ssh"
        user = "azureuser"
        host = "${azurerm_public_ip.myterraformpublicip.fqdn}"
        private_key = "${file("id_rsa")}"
        timeout = "5m"
    }

    provisioner "file" {
        source = "example_file.txt"
        destination = "/tmp/example_file.yml"
    }

    provisioner "remote-exec" {
        inline = [
          "sudo apt-get update",
        ]
    }
}

id_rsa 和 id_rsa.pub 在同一个文件夹中是 .tf 文件。

还尝试了 10m 和 15m 的更高超时。

谢谢

4

1 回答 1

1

此 github问题解决了与您相同的问题,并为此问题提供了适当的解释。

解决此问题的方法是将 allocation_method 更新为“静态”。

希望这可以帮助!

于 2020-05-29T06:20:52.403 回答