Packer does not set hostname on ubuntu 18.04 template

Hello

I am trying to define the hostname of a ubuntu 18.04 template during the templating process. I have tried multiple different posibilities and it just does not set the defined hostname.

This is the base of my packer config:

{
  "builders": [
    {
      "CPU_hot_plug": true,
      "CPUs": 2,
      "NestedHV": true,
      "RAM": 4096,
      "RAM_hot_plug": true,
      "RAM_reserve_all": true,
      "boot_wait": "5s",
      "boot_command": [
        "<enter><wait><f6><wait><esc><wait>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
        "<bs><bs><bs>",
        "/install/vmlinuz",
        " initrd=/install/initrd.gz",
        " priority=critical",
        " locale=en_US",
        " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg",
        "<enter>"

      ],
      "boot_order": "disk,cdrom",
      "cluster": "{{ user `vsphere_cluster` }}",
      "communicator": "ssh",
      "convert_to_template": true,
      "create_snapshot": true,
      "datacenter": "{{ user `vsphere_datacenter` }}",
      "datastore": "{{ user `vsphere_datastore` }}",
      "disk_controller_type": "pvscsi",
      "folder": "{{ user `vsphere_folder` }}",
      "guest_os_type": "ubuntu64Guest",
      "http_directory": "http",
      "insecure_connection": "true",
      "iso_checksum": "sha256:8c5fc24894394035402f66f3824beb7234b757dd2b5531379cb310cedfdf0996",
      "iso_urls": "http://cdimage.ubuntu.com/ubuntu/releases/18.04.5/release/ubuntu-18.04.5-server-amd64.iso",
      "network_adapters": [
        {
          "network": "{{ user `vsphere_network` }}",
          "network_card": "vmxnet3"
        }
      ],
      "password": "{{ user `vsphere_password` }}",
      "resource_pool": "{{ user `vsphere_resource_pool` }}",
      "ssh_password": "packer",
      "ssh_timeout": "300m",
      "ssh_username": "packer",
      "storage": [
        {
          "disk_size": 51200,
          "disk_thin_provisioned": true
        }
      ],
      "configuration_parameters": {
        "disk.EnableUUID": "TRUE"
      } ,
      "type": "vsphere-iso",
      "username": "{{ user `vsphere_username` }}",
      "vcenter_server": "{{ user `vsphere_server` }}",
      "vm_name": "{{ user `vm_name` }}"
    }
  ],
  "provisioners": [
    ....
  ],
  "sensitive-variables": [
     ...
  ],
  "variables": {
    ....
  }
}

My preseed.cfg:

# localization
d-i debian-installer/country string US
d-i debian-installer/locale string en_US.UTF-8

# user configuration
d-i passwd/user-fullname string packer
d-i passwd/username string packer
d-i passwd/user-password password packer
d-i passwd/user-password-again password packer
d-i user-setup/allow-password-weak boolean true

# partition configuration
d-i partman-auto/method string lvm
d-i partman-auto/disk string /dev/sda
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

# password configuration
d-i passwd/root-login boolean false
d-i passwd/root-password password packer
d-i passwd/root-password-again password packer

# time configuration
d-i time/zone string Europe/London
d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server string ntp.server.com

# software configuration
d-i pkgsel/include string open-vm-tools openssh-server cloud-init
d-i grub-installer/only_debian boolean true

# sudoers configuration
d-i preseed/late_command string \
echo 'packer ALL=(ALL) NOPASSWD: ALL' > /target/etc/sudoers.d/packer ; \
in-target chmod 440 /etc/sudoers.d/packer ;
d-i finish-install/reboot_in_progress note

These are the things I have tried to define the hostname:

  1. Add this in my boot_command
    netcfg/get_domain=local,
    netcfg/get_hostname=ubuntu,

  2. Add this in my preseed.cfg:
    d-i netcfg/get_hostname string ubuntu
    d-i netcfg/get_domain string local

  3. Add this in my preseed.cfg:
    d-i netcfg/hostname string ubuntu

No errors occur during the build process and the provisioners work as intended.

Any suggestions what I should try next?

I’ve seen people pass hostname=yournamehere directly in the boot command – we have an example in the boot commands here VSphere ISO - Builders | Packer by HashiCorp

You can also set the vm name according to vmware using the vm_name option: VSphere ISO - Builders | Packer by HashiCorp