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

时间:2020-02-19 14:40:54

标签: azure ssh terraform packer

因此,我正在使用Packer创建一个Azure映像。

<!-- #include file="MyScript.aspx" -->

创建的图像很好,并且位于Azure的资源组中。

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

{
  "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"
      ]
    }
  ]
}

启动VMSS时,尝试在VM中进行SSH时会收到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... }

但是,如果我使用同一Centos映像但直接从Azure使用,则可以在VM中进行SSH。

此外,让我发疯的是,当我通过Packer创建Centos映像时,没有为其配置Ansible(实际上只是Centos映像),并且将其与比例尺设置一起使用时...我也无法在其中进行SSH。

像Packer一样让人讨厌。

1 个答案:

答案 0 :(得分:1)

看起来您正在跳过取消布建步骤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