0
  • 打包机 v1.7.1
  • vCenter Server 6.7

有一个很好的时间让这个工作,特别是构建后的 ssh 连接。Packer 将克隆并引导 OVA 正常,但随后出现错误“等待 SSH 可用”。

使用带有以下参数/属性的官方仿生和焦点 OVA:

bash-5.1# ovftool bionic-server-cloudimg-amd64.ova 
OVF version:   1.0
VirtualApp:    false
Name:          Ubuntu 18.04 Server (20210609)

Download Size:  337.39 MB

Deployment Sizes:
  Flat disks:   10.00 GB
  Sparse disks: Unknown

Networks:
  Name:        VM Network
  Description: The VM Network network

Virtual Machines:
  Name:               ubuntu-bionic-18.04-cloudimg-20210609
  Operating System:   ubuntu64guest
  Virtual Hardware:
    Families:         vmx-10 
    Number of CPUs:   2
    Cores per socket: 1
    Memory:           1024.00 MB

    Disks:
      Index:          0
      Instance ID:    9
      Capacity:       10.00 GB
      Disk Types:     SCSI-VirtualSCSI 

    NICs:
      Adapter Type:   VmxNet3
      Connection:     VM Network

Properties:
  Key:         instance-id
  Label:       A Unique Instance ID for this instance
  Type:        string
  Description: Specifies the instance id.  This is required and used to 
               determine if the machine should take "first boot" actions 
  Value:       id-ovf

  Key:         hostname
  Type:        string
  Description: Specifies the hostname for the appliance
  Value:       ubuntuguest

  Key:         seedfrom
  Label:       Url to seed instance data from
  Type:        string
  Description: This field is optional, but indicates that the instance should 
               'seed' user-data and meta-data from the given url.  If set to 
               'http://example.com/sm-' is given, meta-data will be pulled from
               http://example.com/sm-meta-data and user-data from 
               http://example.com/sm-user-data.  Leave this empty if you do not
               want to seed from a url. 

  Key:         public-keys
  Label:       ssh public keys
  Type:        string
  Description: This field is optional, but indicates that the instance should 
               populate the default user's 'authorized_keys' with this value 

  Key:         user-data
  Label:       Encoded user-data
  Type:        string
  Description: In order to fit into a xml attribute, this value is base64 
               encoded . It will be decoded, and then processed normally as 
               user-data. 

  Key:         password
  Label:       Default User's password
  Type:        string
  Description: If set, the default user's password will be set to this value to
               allow password based login.  The password will be good for only 
               a single login.  If set to the string 'RANDOM' then a random 
               password will be generated, and written to the console. 

References:
  File:  ubuntu-bionic-18.04-cloudimg.vmdk

所以:

  • 实例 ID
  • 主机名
  • 种子来自
  • 公钥
  • 用户数据
  • 密码

我通过 Packer 的vsphere-clone builder传递了其中一些属性;这是我的模板文件的片段:

source "vsphere-clone" "ubuntu-18" {
  template            = "ubuntu-18.ova"
  network             = "${var.networkName}"
  CPUs                = "${var.cores}"
  RAM                 = "${var.memMB}"
  boot_wait           = "5s"
  communicator        = "ssh"
  create_snapshot     = true
  datacenter          = "${var.vcenter_dc}"
  datastore           = "${var.vcenter_datastore}"
  cluster             = "${var.vcenter_cluster}"
  folder              = "${var.vcenter_template_folder}"
  convert_to_template = true
  notes                          = "${var.vm_notes}"
  password                       = "${var.vcenter_pass}"
  shutdown_command               = "sudo shutdown -P now"
  ssh_handshake_attempts         = "50"
  ssh_private_key_file           = "${var.privateKey}"
  ssh_pty                        = true
  ssh_timeout                    = "20m"
  ssh_username                   = "admin"
  username       = "${var.vcenter_user}"
  vcenter_server = "${var.vcenter_server}"
  insecure_connection = true
  vm_name        = "${var.vm_name}"
  vapp {
    properties = {
      instance-id  = "${var.vm_name}"
      hostname     = "${var.vm_name}"
      public-keys  = "${var.publicKey}"
      user-data    = "${var.userData}"
    }
  }
}

奇怪的是,instance-idandhostname属性在启动时设置得很好。这是我的用户数据,我将相同的公钥从下面的用户数据传递到上面的“公钥”属性:

#cloud-config
chpasswd:
  list: |
    ubuntu:<generic pass>
  expire: False
users:
  - name: admin
    gecos: admin
    ssh_authorized_keys:
      - <pubkey>
    groups: sudo, users, admin
    shell: /bin/bash
    sudo: ["ALL=(ALL:ALL) NOPASSWD:ALL"]
disable_root: true
package_upgrade: true
packages:
  - open-vm-tools

所以流程是:从 vapp 属性传递用户数据 > 使用允许的密钥创建管理员用户 > 用于 ssh 身份验证的私钥。但是在它启动到提示符后,它会出错:“Packer 在尝试通过 SSH 连接时遇到身份验证错误。” 我今天以完全相反的设置开始了这一天,其中根本没有涉及任何密钥;仅使用密码,结果相同。我无法使用默认用户或我创建的帐户使用默认和我创建的密码登录控制台。

一旦实例启动,Packer 将等待大约 6 分钟然后放弃。在那段时间,我尝试使用相同的私钥和默认(“ubuntu”)和我创建的“admin”帐户手动 ssh 到实例。我立即收到“权限被拒绝(公钥)”错误。

用于构建的命令如下:

packer build -only "*.ubuntu-20" -var vm_name=ubuntu-20-test -var-file <contains vcenter vars> templates/ubuntu.pkr.hcl

vCenter 密码和ssh_private_key_file值作为环境变量存在的位置。

4

0 回答 0