[英]How do I create a ssh key on a azure virtual machine using terraform?
I Want to create a new ssh key on an azure virtual machine using terraform? 我想使用Terraform在Azure虚拟机上创建新的SSH密钥吗?
I tried this but didn't work. 我试过了,但是没用。
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"cat /dev/zero | ssh-keygen -q -N ''"
]
}
}
It gives this error. 它给出了这个错误。
azurerm_virtual_machine.terraform-app-VM: Still creating... [5m30s elapsed]
azurerm_virtual_machine.terraform-app-VM (remote-exec): Connecting to remote host via SSH...
azurerm_virtual_machine.terraform-app-VM (remote-exec): Host:
azurerm_virtual_machine.terraform-app-VM (remote-exec): User: root
azurerm_virtual_machine.terraform-app-VM (remote-exec): Password: false
azurerm_virtual_machine.terraform-app-VM (remote-exec): Private key: false
azurerm_virtual_machine.terraform-app-VM (remote-exec): Certificate: false
azurerm_virtual_machine.terraform-app-VM (remote-exec): SSH Agent: true
azurerm_virtual_machine.terraform-app-VM (remote-exec): Checking Host Key: false
azurerm_virtual_machine.terraform-app-VM: Still creating... [5m40s elapsed]
Error: timeout - last error: SSH authentication failed (root@:22): ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
You should use resource "azurerm_virtual_machine_extension"
. 您应该使用resource "azurerm_virtual_machine_extension"
。 It does not need SSH key. 它不需要SSH密钥。 Like so: 像这样:
resource "azurerm_virtual_machine_extension" "test" {
name = "<some_name>"
location = "<resource_group_location>"
resource_group_name = "${azurerm_resource_group.<resource_group>.name}"
virtual_machine_name = "${azurerm_virtual_machine.<vm>.name}"
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
settings = <<SETTINGS
{
"commandToExecute": "<your_command>"
}
SETTINGS
}
Note, this is just single command execution. 注意,这只是单个命令执行。 If you want multiple commands to be executed you can create a shell script, upload it (so it is publicly accessible) and do: 如果要执行多个命令,则可以创建一个shell脚本,然后将其上传(可以公开访问)并执行以下操作:
resource "azurerm_virtual_machine_extension" "test" {
name = "<some_name>"
location = "<resource_group_location>"
resource_group_name = "${azurerm_resource_group.<resource_group>.name}"
virtual_machine_name = "${azurerm_virtual_machine.<vm>.name}"
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
settings = <<SETTINGS
{
"fileUris": ["https://url/to/file/<file>.sh"],
"commandToExecute": "sh <file>.sh"
}
SETTINGS
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.