Setting a static IP on ubuntu 20.04 with cloud-init

So, it took me numerous tries, digging, and beating of head against curtin and the like, but I have finally figured out how to set a static IP on a host using cloud-init.

Mind you, I am not talking about setting a static iP for while the installer is running. That’s easy, and well documented. I’m talking about the next time it boots.

The solution requires two bits. They’re pretty straightforward, and both go in the cloud-init file

Bit one goes at the end of the autoinstall section, and deletes the default configuration and writes a permanent one instead. Note that the filesystem is mounted as /target


late-commands:
– rm /target/etc/cloud/cloud.cfg.d/50-curtin-networking.cfg
– mv /tmp/00-installer-config.yaml /target/etc/netplan

Bit two actually writes out the /tmp/00-installer-config.yaml that late-commands will be editing


write_files:
– path: /tmp/00-installer-config.yaml
owner: root:root
permissions: “0644”
content: |
network:
version: 2
ethernets:
ens160:
critical: true
dhcp-identifier: mac
dhcp4: true
ens192:
dhcp4: no
addresses:
– {ipaddr}/16
gateway4: 172.16.1.1
nameservers:
addresses:
– 172.16.5.6
search:
– search.domain

Leave a Reply