I provision multiple desktop computers with Ubuntu 24.04 LTS via netboot and autoinstall + cloud-init and during that process I would like to set the hostname to whatever is configured on my dhcp- server.
Right now my user-data looks like this:
#cloud-config
autoinstall:
...
identity:
hostname: localhost
...
...
late-commands:
- curtin in-target -- bash -c 'hostname=$(nslookup "$(hostname -I | awk "{print \$1}")" | awk "/name =/ {print \$4}" | sed "s/\.$//" | cut -d"." -f1); echo "$hostname" > /etc/hostname; hostnamectl set-hostname "$hostname"'
Which works, but seems like a dirty way to set the hostname dynamically.
I’m wondering if there is a better way to do it, preferably without using any late-commands.
An ideal solution would be if the configuration just uses the hostname supplied by the dhcp server.
What I tried:
(i) Ommitting the hostname key in the identity section - Is not allowed, as the key is required.
(ii) Toggling preserve_hostname true and false in the user-data section - neither seem to affect anything.
(iii) Explicitly setting use_hostname to true in the dhcp4-overrides section of the network configuration - does not change anything at all.
I’m completely new to using autoinstall and cloud-init so there is probably something obvious am missing.