cloud-init ignoring static IP network configuration

Brennan Cheung picture Brennan Cheung · Oct 13, 2019 · Viewed 9.1k times · Source

I running the Ubuntu 18.04 cloud image and trying to configure networking through cloud-init. For some reason it is ignoring my networking when I try to assign a static IP and just falls back to using DHCP. I'm not sure why and I'm not sure how to debug it. Does anyone know if I am doing something wrong or how I should further troubleshoot this:

Here is my config.yaml I'm using to generate my config.img:

#cloud-config
network:
    version: 2
    ethernets:
        ens2:
            dhcp4: false
            dhcp6: false
            addresses: [10.0.0.40/24]
            gateway4: 10.0.0.1
password: secret # for the 'ubuntu' user in case we can't SSH in
chpasswd: { expire: false }
ssh_pwauth: true
users:
    - default
    - name: brennan
      ssh_import_id: gh:brennancheung
      sudo: ALL=(ALL) NOPASSWD:ALL
hostname: vm
runcmd:
    - [ sh, -xc, "echo Here is the network config for your instance" ]
    - [ ip, a ]
final_message: "Cloud init is done.  Woohoo!"

Everything else in the config seems to be working, it's as if it doesn't even see the network portion though.

I'm attaching the .img as a cdrom to read the cloud-init. You can see how I'm running it here: https://github.com/brennancheung/playbooks/blob/master/cloud-init-lab/Makefile

NOTE: Once I'm logged into the box I can replace the config in /etc/netplan with the network section above and re-apply it and the networking comes up fine with a static IP. So I think there aren't any obvious errors that I am missing. This leads me to believe it is related to the cloud-init networking module(s) and not netplan itself.

Answer

Brennan Cheung picture Brennan Cheung · Oct 14, 2019

I finally figure it out. Hopefully this helps someone else.

Apparently you can't supply networking configuration in user-data. You have to specify it in the cloud provider's data source or in metadata. In order to do that you have to move the network section into its own file and build the cloud-init image with the --network-config=... option.

Ex:

cloud-localds -v --network-config=network-config-v2.yaml seed.img user-data.yaml

I have the complete setup for configuring and booting a cloud instance in a local KVM if it helps anyone else out.

https://github.com/brennancheung/playbooks/tree/master/cloud-init-lab