首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用terraform在azure虚拟机上创建ssh密钥?

如何使用terraform在azure虚拟机上创建ssh密钥?
EN

Stack Overflow用户
提问于 2019-09-14 02:11:49
回答 1查看 1K关注 0票数 0

我想使用terraform在azure虚拟机上创建一个新的ssh密钥。

我试过了,但不起作用。

代码语言:javascript
复制
provisioner "remote-exec" {
  inline = [
      "sudo apt-get update",
      "cat /dev/zero | ssh-keygen -q -N ''"
  ]
}
}

它会给出这个错误。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-09-14 09:01:14

您应该使用resource "azurerm_virtual_machine_extension"。它不需要SSH密钥。如下所示:

代码语言:javascript
复制
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
}

请注意,这只是一个命令执行。如果您想要执行多个命令,您可以创建一个shell脚本,上载它(以便它可以公开访问),然后执行以下操作:

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57928490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档