How do I use cloud-init to apply netplan?

9,194

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
Share:
9,194

Related videos on Youtube

QuestionedE
Author by

QuestionedE

I started as an amateur C++ programmer, trained as an electrical engineer, worked a lot in the Microsoft stack (C#/.NET/ASP/MVC/SQL/SharePoint), and now work in Python.

Updated on September 18, 2022

Comments

  • QuestionedE
    QuestionedE almost 2 years

    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?

  • user988346
    user988346 over 4 years
    My netplan file says this at the top: " # This file is generated from information provided by # the datasource. Changes to it will not persist across an instance. # To disable cloud-init's network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} " It's still ok to edit this file?