简体   繁体   English

如何使用Terraform在Azure虚拟机上创建SSH密钥?

[英]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.

相关问题 如何使用 Azure 中的 terraform 创建 UIPathRobot 虚拟机? - How to create a UIPathRobot Virtual Machine using terraform in Azure? 如何在虚拟机规模集中使用 terraform 请求 azure 现场实例? - How do I request azure spot instances using terraform, in a virtual machine scale set? 使用 Terraform 为虚拟机密码创建 Key Vault 机密 - Using Terraform to create Key Vault secret for Virtual Machine password 如何在Azure中的特定虚拟机上创建应用程序服务? - How do i create app service on particular virtual machine in Azure? Terraform 如何将 SSH 密钥或 SSH 用户名和密码关联到 azurerm_windows_virtual_machine - Terraform how to associate an SSH Key or SSH username and password to azurerm_windows_virtual_machine 如何使用现有的 SSH 密钥存储在 azure 中使用 ARM 模板创建虚拟机 - How to use existing SSH key stored for Virtual Machine creation in azure using ARM template 如何使用Azure ARM创建公共虚拟机映像? - How do I create a public virtual machine image using Azure ARM? 如何使用Azure ARM创建多个私有虚拟机映像? - How do I create several private virtual machine images using Azure ARM? 如何使用 terraform 在虚拟机中启用 azure 监视器? - How to enable azure monitor in virtual machine using terraform? 使用 HCL/Terraform 的 azurerm_virtual_machine 块中的“admin_ssh_key”问题 - Issues with "admin_ssh_key" within azurerm_virtual_machine block using HCL/Terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM