无法在使用 Packer 创建的 Azure VM 中进行 SSH

Unable to SSH in Azure VM created with Packer

所以,我正在使用 Packer 创建 Azure 映像。

{
  "builders": [{
    "type": "azure-arm",

    "client_id"      : "{{user `client_id`}}",
    "client_secret"  : "{{user `client_secret`}}",
    "subscription_id": "{{user `subscription_id`}}",
    "tenant_id"      : "{{user `tenant_id`}}",

    "managed_image_resource_group_name": "{{user `resource_group`}}",
    "managed_image_name": "CentOS7_w_GitlabCE_{{timestamp}}",

    "os_type"        : "Linux",
    "image_publisher": "OpenLogic",
    "image_offer"    : "CentOS",
    "image_sku"      : "7.3",
    "image_version"  : "latest",

    "location": "{{user `location`}}",
    "vm_size" : "Standard_DS2_v2"
  }],
  "provisioners": [
    {
      "type": "ansible",
      "playbook_file": "./gitlab/ansible/install-gitlab.yml",
      "extra_arguments": [
        "-vvvv"
      ]
    }
  ]
}

图像创建得很好并且存在于我在 Azure 中的资源组中。

然后,我将其详细信息输入到 Terraform 中以创建比例集。

data "azurerm_image" "image" {
  count = "${var.create_gitlab ? 1 : 0}"

  //notice: the image must have been created beforehand by Packer (inside the specific resource group)
  name                = "${var.vm_img_built_via_packer}"
  resource_group_name = "${var.resource_group}"
}

resource "azurerm_virtual_machine_scale_set" "vmss" {

...other stuff....

  storage_profile_image_reference {
    // reference the id of the custom image created with Packer
    id = "${data.azurerm_image.image.id}"
  }

  os_profile {
    computer_name_prefix = "${var.prefix}-vm"
    admin_username       = "someuser"
  }

  os_profile_linux_config {
    disable_password_authentication = true

    ssh_keys {
      path     = "/home/someuser/.ssh/authorized_keys"
      key_data = "${file(var.someuser_ssh_pubkey)}"
    }
  }

...other stuff...

}

当我启动 VMSS 时,当我尝试在 VM 中使用 SSH 时,我得到 Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

但是,如果我使用相同的 Centos 映像但直接来自 Azure,我可以在 VM 中使用 SSH。

此外,让我生气的是,当我通过 Packer 创建一个 Centos 映像时,没有使用 Ansible 提供它(实际上只是一个 Centos 映像),并将它与规模集一起使用......我也无法通过 SSH 进入它。

感觉 Packer 做了一些令人讨厌的事情。

您似乎正在跳过取消配置步骤 https://packer.io/docs/builders/azure-arm.html#deprovision,该步骤对于清空网络和本地帐户配置以及之后重复使用图像是强制性的。

对于Linux你需要执行这个命令:

/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync

看这里的例子:https://github.com/hashicorp/packer/blob/master/examples/azure/linux_custom_image.json

Azure 文档:https://docs.microsoft.com/en-us/azure/virtual-machines/linux/capture-image#step-1-deprovision-the-vm