Packer + autoinstall gets new IP several times in install process

Asked by Brantley Padgett

I am using Packer to build ubuntu 20.04 templates in a vsphere environment. It works about 50% of the time, the rest it fails because during the boot/install/reboot phases, the IP changes 3 different times and Packer can't keep up.

DHCP is handled by an upstream Juniper SRX router. I've got the `dhcp_identifier: mac` option set, as well as have used a late-command to add it to the 00-installer netplan, but it is not reliable. I've also tested feeding a specified mac into the packer configs and setting a static IP assignment in the SRX.

The DUID/UUID vs MAC thing seems to be the root of the issue, but I'm not sure what can be done about it. Even with a predefined MAC and a static address set for it, I get different IPs, even though in the SRX arp table I see the correct MAC with the incorrect IP.

Here's the user-data I'm passing, I've sanitized it a little but otherwise it's exactly what I'm sending:

```
#cloud-config
autoinstall:
  update: no
  identity:
    hostname: ubuntu2004template
    password: 'supersecurehash'
    realname: Admin
    username: admin
  version: 1
  ssh:
    install-server: yes
  locale: en_US
  keyboard:
    layout: us

## for static IP build on vmware/vcenter
# network:
# network:
# version: 2
# ethernets:
# enwild:
# match:
# name: en*
# addresses: [10.1.1.238/24]
# gateway4: 10.1.1.1
# nameservers:
# addresses:
# - 10.1.1.6

# network:
# network:
# version: 2
# ethernets:
# ens160:
# dhcp4: true
# dhcp6: false
# dhcp-identifier: mac

## for local vmware builds
# network:
# network:
# version: 2
# ethernets:
# ens33:
# dhcp4: true

## matching eth or en*
  network:
    network:
      version: 2
      renderer: networkd
      ethernets:
        eth0:
          match:
            name:en*
          dhcp4: true
          dhcp-identifier: mac
# otherif:
# match:
# name:eth*
# dhcp4: true
# dhcp-identifier: mac

  storage:
    config:
    - grub_device: true
      id: disk-sda
      path: /dev/sda
      ptable: gpt
      type: disk
      wipe: superblock-recursive
    - device: disk-sda
      flag: bios_grub
      id: partition-1
      number: 1
      size: 1048576
      type: partition
    - device: disk-sda
      wipe: superblock
      flag: ''
      number: 2
      preserve: false
      grub_device: false
      type: partition
      id: partition-2
      size: 1073741824
    - fstype: ext4
      volume: partition-2
      preserve: false
      type: format
      id: format-11
    - device: disk-sda
      size: -1
      wipe: superblock
      flag: ''
      number: 3
      preserve: false
      grub_device: false
      type: partition
      id: partition-3
    - name: ubuntu-vg
      devices: [partition-3]
      preserve: false
      type: lvm_volgroup
      id: lvm_volgroup-3
    - name: root
      volgroup: lvm_volgroup-3
      size: 35%
      preserve: false
      type: lvm_partition
      id: lvm_partition-9
    - name: swap
      volgroup: lvm_volgroup-3
      size: 5%
      preserve: false
      type: lvm_partition
      id: lvm_partition-10
    - fstype: swap
      volume: lvm_partition-10
      preserve: false
      type: format
      id: format-13
    - device: format-13
      path: ''
      type: mount
      id: mount-13
    - name: tmp
      volgroup: lvm_volgroup-3
      size: 10%
      preserve: false
      type: lvm_partition
      id: lvm_partition-11
    - fstype: ext4
      volume: lvm_partition-11
      preserve: false
      type: format
      id: format-14
    - name: var
      volgroup: lvm_volgroup-3
      size: 10%
      preserve: false
      type: lvm_partition
      id: lvm_partition-12
    - fstype: ext4
      volume: lvm_partition-12
      preserve: false
      type: format
      id: format-15
    - name: var_tmp
      volgroup: lvm_volgroup-3
      size: 5%
      preserve: false
      type: lvm_partition
      id: lvm_partition-13
    - fstype: ext4
      volume: lvm_partition-13
      preserve: false
      type: format
      id: format-16
    - name: log
      volgroup: lvm_volgroup-3
      size: 10%
      preserve: false
      type: lvm_partition
      id: lvm_partition-14
    - fstype: ext4
      volume: lvm_partition-14
      preserve: false
      type: format
      id: format-17
    - name: audit
      volgroup: lvm_volgroup-3
      size: 10%
      preserve: false
      type: lvm_partition
      id: lvm_partition-15
    - fstype: ext4
      volume: lvm_partition-15
      preserve: false
      type: format
      id: format-18
    - name: home
      volgroup: lvm_volgroup-3
      size: 10%
      preserve: false
      type: lvm_partition
      id: lvm_partition-16
    - fstype: ext4
      volume: lvm_partition-16
      preserve: false
      type: format
      id: format-19
    - fstype: ext4
      volume: lvm_partition-9
      preserve: false
      type: format
      id: format-21
    - device: format-21
      path: /
      type: mount
      id: mount-21
    - device: format-11
      path: /boot
      type: mount
      id: mount-11
    - device: format-14
      path: /tmp
      type: mount
      id: mount-14
      options: 'nodev,nosuid,noexec'
    - device: format-15
      path: /var
      type: mount
      id: mount-15
    - device: format-16
      path: /var/tmp
      type: mount
      id: mount-16
      options: 'nodev,nosuid,noexec'
    - device: format-17
      path: /var/log
      type: mount
      id: mount-17
    - device: format-18
      path: /var/log/audit
      type: mount
      id: mount-18
    - device: format-19
      path: /home
      type: mount
      id: mount-19
      options: 'nodev'
  packages:
    - open-vm-tools
    - ansible
    - wget
  user-data:
    timezone: America/Chicago
  late-commands:
    - "echo 'Defaults:admin !requiretty' > /target/etc/sudoers.d/admin"
    - "echo 'admin ALL=(ALL) NOPASSWD: ALL' >> /target/etc/sudoers.d/admin"
    - "chmod 440 /target/etc/sudoers.d/admin"
    - "sed -i -e 's/^# HOME=\\/home/HOME=\\/export\\/home/g' /target/etc/default/useradd"
    - "sed -i -e 's/^DHOME=\\/home/DHOME=\\/export\\/home/g' /target/etc/adduser.conf"
    - "mkdir -p /target/export/home"
    - "echo 'admin ALL=(ALL) NOPASSWD: ALL' >> /target/etc/sudoers.d/admin"
    - 'sed -i "s/^#Port 22/Port 22/g" /target/etc/ssh/sshd_config'
    - 'sed -i "/^Port 22/a Port 1981" /target/etc/ssh/sshd_config'
# - 'sed -i "s/dhcp4: true/&\n dhcp-identifier: mac/" /target/etc/netplan/00-installer-config.yaml'
# - "useradd -m -d /target/export/home/admin -p 'supersecurehash' -s /bin/bash -c 'admin' admin"
```

Here's an example run where the IP changes multiple times in a single run:
```
show dhcp server binding routing-instance trust-vr

IP address Session Id Hardware address Expires State Interface
10.20.30.226 1116 00:00:00:00:5a:6e 82585 BOUND xe-6/0/1.99
10.20.30.241 1123 00:00:00:00:5a:6e 83605 BOUND xe-6/0/1.99
10.20.30.231 1126 00:00:00:00:5a:6e 85473 BOUND xe-6/0/1.99
10.20.30.233 1128 00:00:00:00:5a:6e 85875 BOUND xe-6/0/1.99
```

In that case, the .241 was the statically assigned DHCP address to the MAC, and though I sanitized them, the MACs were all the same.

I know there's a boot command option of `ip=dhcp`, is there a similar option setting the identifier to MAC? In the user-data above, the commented out sections are attempts with different options. The only reliable method has been a static IP not using dhcp, but that address gets copied to any VMs built with the template, so not ideal.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Bernard Stafford (bernard010) said (last edit ):
#1

https://help.ubuntu.com/community/DisklessUbuntuHowto
scroll to the static IP section of doc.
unless you are using kvm:
Generating a KVM MAC

If you are managing your guests via command line, the following script might be helpful to generate a randomized MAC using QEMU's registered OUI (52:54:00):

MACADDR="52:54:00:$(dd if=/dev/urandom bs=512 count=1 2>/dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\).*$/\1:\2:\3/')"; echo $MACADDR

If you're unsure about assigning an in-use MAC then check for a match in the output of "ip neigh". However, using this random method is relatively safe, giving you an approximately n in 16.8 million chance of a collision (where n is the number of existing QEMU/KVM guests on the LAN).
https://help.ubuntu.com/community/KVM/Networking
https://www.linux-kvm.org/page/Networking

Can you help with this problem?

Provide an answer of your own, or ask Brantley Padgett for more information if necessary.

To post a message you must log in.