Configuring a static IP address when not using LXD managed DHCP

While developing MAAS I often have the need to create containers for testing. MAAS itself runs a DHCP server so when creating a container to run MAAS I need a network which MAAS can control. To do that I need to start a container with a static IP address. Network configuration in MAAS and many other clouds is actually done by cloud-init using netplan syntax. You can do the same with LXD

lxc init ubuntu:20.04 maas
net_cfg=$(mktemp setup_maas_XXXX)
cat <<EOF >> $net_cfg
network:
    version: 2
    ethernets:
        # MAAS network
        eth0:
            addresses: [10.0.0.2/24]
            gateway4: 10.0.0.1
            nameservers:
                search: [maas]
                addresses: [10.0.0.2, 10.0.0.1]
        # Standard LXD network
        eth1:
            dhcp4: true
EOF
lxc file push $net_cfg maas/etc/cloud/cloud.cfg.d/network.cfg
rm $net_cfg
lxc start maas

You could also set user.network-config to a suitable cloud-init network config YAML to have it applied on first boot of the container.