Automate VLAN tagging on KVM using Ansible

 This blogpost will provide details and steps for automating VLAN-tagged bridge setup on a KVM host using Ansible, and then optionally connect it to OLVM if needed.

Use Ansible to:

  1. Create a VLAN sub-interface on a physical NIC.

  2. Create a Linux bridge attached to that VLAN interface.

  3. Ensure the bridge is ready to be used by VMs or OLVM.


Prerequisites

  • Ansible installed on a control node.

  • SSH access to the KVM host(s).

  • nmcli or network role availability (for Red Hat-based distros).


Ansible Playbook Example

Directory Structure:

vlan-bridge-setup/
├── inventory
└── vlan_bridge.yml

inventory:

[kvmhosts]
192.168.125.100 ansible_user=root

vlan_bridge.yml:


- name: Configure VLAN tagged bridge on KVM host
  hosts: kvmhosts
  become: yes
  tasks:
    - name: Create VLAN interface eth0.100
      nmcli:
        conn_name: vlan100
        ifname: eth0
        type: vlan
        vlan_id: 100
        state: present
        autoconnect: yes

    - name: Create bridge br-vlan100
      nmcli:
        conn_name: br-vlan100
        ifname: br-vlan100
        type: bridge
        state: present
        autoconnect: yes

    - name: Add VLAN interface to the bridge
      nmcli:
        conn_name: br-vlan100-slave
        ifname: vlan100
        type: bridge-slave
        master: br-vlan100
        state: present
        autoconnect: yes

    - name: Bring up all interfaces
      command: nmcli connection up "{{ item }}"
      loop:
        - vlan100
        - br-vlan100
        - br-vlan100-slave

After Playbook Execution

  • Bridge br-vlan100 is ready on VLAN 100.

  • In OLVM, go to:

    • Network > Networks → Add a new logical network with VLAN ID 100.

    • Map it to the host and bind it to br-vlan100.


Use in VM

For VMs outside of OLVM using virt-install or virsh:

virt-install --name test-dbvm --ram 2048 --disk size=10 \

  --vcpus 2 --os-type linux --os-variant OEL7.0 \

  --network bridge=br-vlan100,model=virtio \

  --cdrom /path/to/iso


Automating VLAN tagging offers significant benefits, especially in complex or large-scale virtualized environments. It ensures consistency across hosts by applying standardized configurations, reducing the risk of human error that can lead to misconfigured networks or security gaps. Automation accelerates deployment by eliminating repetitive manual tasks, enabling rapid provisioning of VLAN-tagged interfaces and bridges for virtual machines. It also enhances scalability, making it easier to manage changes across many servers or clusters. Furthermore, automated VLAN configuration improves compliance and auditing by maintaining predictable, version-controlled network setups that can be tracked and rolled back if needed.

Thanks for reading :)

regards,
Syed Zaheer


Comments

Popular posts from this blog

Disable Firewall on Oracle Linux 8

Installation of Oracle Applications R12.1.1 on Linux and vmware

Enable Desktop on Oracle Solaris 11.4