cloud-init ignoring static IP network configuration
Asked Answered
T

2

7

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.

Transpose answered 13/10, 2019 at 21:57 Comment(1)
After doing more research I ran across this "User-data cannot change an instance’s network configuration.". Apparently only the vendors and cloud providers can set networking information? Why? cloudinit.readthedocs.io/en/latest/topics/network-config.htmlTranspose
T
11

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

Transpose answered 14/10, 2019 at 0:18 Comment(3)
I guess it also does not work on Hetzner Cloud. Can't tell for sure, but have tried everything and it didn't work. I guess this is the case there too. Thanks for sharing.Coenesthesia
For me, this isn't working under Proxmox when using Debian Buster in KVM. It starts and looking for DHCP. IP is set after a while, but with large delay.Overbear
I am having this exact same issue with an Ubuntu 20.4 cloud image. I have done all the same things but nothing works.Arther
L
1

If you notice, in /etc/cloud/cloud.cfg.d there exists a file called 99-fake-cloud.cfg (or something similar). If you delete this, then cloud-init will configure the network using the parameters in your user-data file (i.e. - /etc/cloud/cloud.cfg)

Larimore answered 14/12, 2022 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.