VLAN Generation Script for Netplan

Once it was necessary to add several hundred VLANs without IP addresses to the Netplan configuration and in order to simplify the process, I came up with the following script:

n=0; while (($n < 1000)); do ((++n));
echo "        vlan$n:
            id: $n
            link: ens3f0
            dhcp4: no
            dhcp6: no"; done

The script generates a VLAN from 1 to 1000, I saved the result to a file:

./script > ixnfo.txt

Then I opened it in a text editor and copied the desired ranges.

I will give an example of a finished configuration file:

network:
    ethernets:
#       TO UPLINK
        ens3f1:
            addresses:
            - 192.168.10.82/30
            gateway4: 192.168.10.81
            nameservers:
                addresses:
                - 192.168.5.2
                - 1.1.1.1
#       TO CLIENTS
        ens3f0: {}
    vlans:
        vlan501:
            id: 501
            link: ens3f0
            addresses:
            - 10.55.55.60/24
#            gateway4: 10.55.55.2
        vlan225:
            id: 225
            link: ens3f0
            dhcp4: no
            dhcp6: no
        vlan226:
            id: 226
            link: ens3f0
            dhcp4: no
            dhcp6: no
        vlan227:
            id: 227
            link: ens3f0
            dhcp4: no
            dhcp6: no
        vlan228:
            id: 228
            link: ens3f0
            dhcp4: no
            dhcp6: no
        vlan301:
            id: 301
            link: ens3f0
            dhcp4: no
            dhcp6: no
        vlan302:
            id: 302
            link: ens3f0
            dhcp4: no
            dhcp6: no

    version: 2

See also my articles:
How to configure networking with Netplan
Configuring the Network in Linux
DHCP configuration script

Leave a comment

Leave a Reply