Random One-Liners for Fixing Bulk-Generated Hosts

2024-08-31

Apparently when deploying multiple VMs via xen-orchestra, cloud-init will not respect the per-vm name field in the advanced options menu. Instead of recreating them one-by-one, I bulk renamed them with a one-liner. Enjoy.

Rename hosts in bulk by IP address:

for x in 5,hosta 6,hostb 7,hostc 8,hostd; do IFS=',' read -r a b <<< $x; ssh user@10.0.1.$a "sh -c \"sudo hostnamectl set-hostname ${b}.example.com\""; done

This can also be adapted to force re-run cloud-init first time setup:

for x (5 6 7 8 9 10 11); do ssh user@10.0.1.$x 'sh -c "sudo cloud-init clean --logs;sudo reboot now"'; done

Thanks to https://stackoverflow.com/a/36393986 for reminding me that combining read and IFS can be a powerful tool.