Skip to content

Instantly share code, notes, and snippets.

@Informatic
Last active April 9, 2024 07:09
Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Informatic/0b6b24374b54d09c77b9d25595cdbd47 to your computer and use it in GitHub Desktop.
Save Informatic/0b6b24374b54d09c77b9d25595cdbd47 to your computer and use it in GitHub Desktop.
cloud-init "nocloud" networking setup

cloud-init is absolute cancer. Its code is horrible. It has no documentation at all.

It took me 5 fucking hours to figure out how to properly configure networking on recent cloud-init (Ubuntu 16.04 cloud image) with local datasource.

It's not mentioned anywhere you need to provide dsmode: local. (but only if you need network-config, besides that everything is fine; someone below noted that -m flag does the same thing, good to know) Of course nobody needs documentation for network-config format either. (cloudinit/net/__init__.py is a protip, enjoy the feces dive)

Oh, and by the way - no, it's not possible to provide network-config to uvt-kvm without patching shit.

Use -N flag for cloud-localds for network-config.

dsmode: local
# ↑ THIS. SHIT.
---
version: 1
config:
- type: physical
name: ens3
subnets:
- control: auto
type: static
address: |
192.168.21.37
pre-up echo xD > /tmp/yesitsthereyo # quality configuration injection, real thing
#cloud-config
chpasswd:
list: |
root:password
expire: False
write_files:
- content: |
You don't really need that, just a bloody test.
path: /root/test.txt
@mcbenjemaa
Copy link

mcbenjemaa commented Aug 9, 2023

should i put this in the network-config file?

The network-config is not working, I only see the default cloud-init dhcp config.

the script you provided adds the 10-custom.yaml but there's still 50-cloud-init.yaml by default.

it doesn't change the IP, should I disable it?

@mcbenjemaa
Copy link

@StudioLE , thanks for your suggestion.

I have this working now. 💯

#cloud-config
hostname: proxmox-test-control-plane
manage_etc_hosts: true
user: ionos
chpasswd:
  expire: false
users:
  - default
package_upgrade: true

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

  - path: /etc/netplan/99-custom.yaml
    content: |
      network:
        version: 2
        renderer: networkd
        ethernets:
          eth0:
            dhcp4: no
            addresses:
            - 10.10.10.66/24
            routes:
            - to: default
              via: 10.10.10.1
            nameservers:
              addresses: [8.8.8.8, 8.8.4.4]

  - path: /var/lib/cloud/scripts/per-instance/30-network-configure
    permissions: 0o500
    content: |
        #!/bin/bash
        set -uo pipefail
        
        echo-success () {
          echo -e "\e[32m ✔  $1\e[0m" >&2
        }
        
        echo-information () {
          echo -e "\e[34m ⓘ  $1\e[0m" >&2
        }
        
        echo-error () {
          echo -e "\e[31m !  $1\e[0m" >&2
        }
        
        echo-warning () {
          echo -e "\e[33m ⚠  $1\e[0m" >&2
        }
        
        echo-subsidiary () {
          echo -e "\e[37m    $1\e[0m" >&2
        }
        
        echo-step () {
          echo -e "\e[0m »  $1\e[0m" >&2
        }
        
        echo-step "Applying Netplan configuration"
        if netplan apply
        then
          echo-success "Applied Netplan configuration"
        else
          echo-error "Failed to apply netplan configuration"
        fi

@pentiumoverdrive
Copy link

@StudioLE I want to set a static ip address with nocloud but that doesn't work, on proxmox. I'm mounting the cloud-init data using an ISO cd drive. Do you have a workaround?
I have put this in metadata and user data, but that doesn't work.

network:
    version: 2
    ethernets:
        eth0:
            match:     
              name: en*
            set-name: eth0
            addresses: [10.10.10.84/32]
            gateway4: 10.10.10.1
            nameservers:
              addresses:
                - 8.8.8.8
                - 8.8.4.4

The indentation isn't consistent in your example, I'm not sure if that may be causing issues? In some places you're using 4 spaces and others 2 spaces.

Also worth noting that gateway4: is deprecated you should use default routes instead.

Rewriting your example would look something like this:

network:
  version: 2
  ethernets:
    eth0:
      match:     
        name: en*
      set-name: eth0
      addresses: 
      - 10.10.10.84/32
      routes:
      - to: default
        via: 10.10.10.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

Thank you!
Saved me hours of hair pulling trying to build images without dhcpd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment