【发布时间】:2017-12-13 07:33:39
【问题描述】:
我已使用 Terraform 在 Azure 上成功创建了一个 VM 作为资源组的一部分。下一步是在新机器上 ssh 并运行一些命令。为此,我创建了一个配置器作为 VM 资源的一部分并设置了 SSH 连接:
resource "azurerm_virtual_machine" "helloterraformvm" {
name = "terraformvm"
location = "West US"
resource_group_name = "${azurerm_resource_group.helloterraform.name}"
network_interface_ids = ["${azurerm_network_interface.helloterraformnic.id}"]
vm_size = "Standard_A0"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "14.04.2-LTS"
version = "latest"
}
os_profile {
computer_name = "hostname"
user = "some_user"
password = "some_password"
}
os_profile_linux_config {
disable_password_authentication = false
}
provisioner "remote-exec" {
inline = [
"sudo apt-get install docker.io -y"
]
connection {
type = "ssh"
user = "some_user"
password = "some_password"
}
}
}
如果我运行“terraform apply”,它似乎进入了一个无限循环,试图 ssh 失败,一遍又一遍地重复这个日志:
azurerm_virtual_machine.helloterraformvm (remote-exec): Connecting to remote host via SSH...
azurerm_virtual_machine.helloterraformvm (remote-exec): Host:
azurerm_virtual_machine.helloterraformvm (remote-exec): User: testadmin
azurerm_virtual_machine.helloterraformvm (remote-exec): Password: true
azurerm_virtual_machine.helloterraformvm (remote-exec): Private key: false
azurerm_virtual_machine.helloterraformvm (remote-exec): SSH Agent: true
我确定我做错了什么,但我不知道是什么:(
编辑:
我已经尝试在没有配置器的情况下设置这台机器,并且我可以使用给定的用户名/密码通过 SSH 连接到它。但是我需要在 Azure 门户中查找主机名,因为我不知道如何从 Terraform 中检索它。怀疑日志中的“Host:”这行是空的,不知道和这个有没有关系?
更新:
我尝试了不同的方法,例如在与
的连接中指明主机名host = "${azurerm_public_ip.helloterraformip.id}"
和
host = "${azurerm_public_ip.helloterraformips.ip_address}"
如文档中所述,但没有成功。
我也尝试过使用 ssh-keys 代替密码,但结果相同 - 无限循环连接尝试,没有明确的错误消息说明为什么它没有连接。
【问题讨论】:
-
您可以使用这些凭据手动 ssh 进入该框吗?
-
对,我的错,您没有“创建 NSG”步骤,因此无需为此创建规则。
-
@evilSnobu 是的,我可以使用用户/密码组合手动 ssh 进入机器。我已使用该信息更新了问题。
-
@zapatilla 我检查了你的问题,根据我的理解,你想在创建 VM 时使用 docker。对于 Azure VM,您不需要通过 ssh 访问它。您可以使用 Azure 自定义脚本扩展来执行此操作。 terraform 支持扩展。我测试了它,它对我来说很好用。你可以看看我的回答。