Hello all. I'm working on a script that will need to convert an existing, straightforward netplan config with an ethernets section into a bridged config. This is so that the script can build bridged connections for KVM instances. I've come up with a way that should work, but I'm wondering if there is a better way.
Proof of concept using echo (the real one would edit the config file in-place):
Code:
IFNAME=$(ip route get 1.1.1.1 | grep -oP "(?<=dev )([^ ]*)")
echo "network:
ethernets:
enp3s0:
addresses:
- 192.168.1.16/24
dhcp6: true
nameservers:
addresses:
- 1.1.1.1
- 1.0.0.1
search:
- example.com
routes:
- to: default
via: 192.168.1.254
version: 2
"|
yq -y "{\"interfaces\": [\"$IFNAME\"] } as \$ifs |
{\"br0\":.network.ethernets.$IFNAME } as \$br |
{\"bridges\": \$br} as \$brs |
{\"$IFNAME\": {}} as \$nif |
del(.network.ethernets.$IFNAME) |
.network.ethernets += \$nif |
.network += \$brs |
.network.bridges.br0 += \$ifs"
Does anyone have a better way of doing this from bash?
The target environment is an Ubuntu Server 24.04 default install.
Bookmarks