How do I use cloud-init to apply netplan?

After I manually edit /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg, how do I tell cloud-init to update /etc/netplan/50-cloud-init.yaml? In other words, it doesn't make sense to manually update both files; what's the standard process to re-apply the netplan?


Solution 1:

The cloud-init confuguration files are not the place the change your network configuration after first boot, because the network configuration will only be generated once by cloud-init (on first boot).

You can however change the netplan config file directly. Then use the following commands as root to enable your changes. They will stick across boots.

netplan generate
netplan apply

Only the command 'cloud-init clean' will regenerate the 50...yaml file, but I dont think you want that. That would reset everything, including hostname en machine-id (resulting in a new max-address and a new dhcp ip address).

Solution 2:

As Lismatro said, once you create your netplan config, generate then apply it.

In your cloud-init config, first tells netplan to use custom config:

write_files:
- path: /etc/cloud/cloud.cfg.d/99-custom-networking.cfg
  permissions: '0644'
  content: |
    network: {config: disabled}

Then create the netplan config (still in cloud-init config). Here is a simple example. Find more ways to configure network interfaces with netplan here:

- path: /etc/netplan/my-new-config.yaml
  permissions: '0644'
  content: |
    network:
        version: 2
        ethernets:
            ens3:
                dhcp4: true

Finally tell cloud-init to use it (again, still in cloud-init config):

runcmd:
 - rm /etc/netplan/50-cloud-init.yaml
 - netplan generate
 - netplan apply