Skip to main content

Configuring VLANs and Trunking with Ansible: A Step-by-Step Guide

Configuring VLANs and Trunking with Ansible: A Step-by-Step Guide

As network environments grow increasingly complex, automation has become a critical tool for NetOps professionals. Ansible, with its simplicity and power, is an excellent choice for automating network configurations. In this guide, we'll explore how to configure VLANs and trunking on network devices using Ansible.


What Are VLANs and Trunking?

VLANs (Virtual Local Area Networks) are used to segment network traffic logically, enhancing security and performance. Each VLAN acts as an independent broadcast domain, isolating traffic between devices in different VLANs.

Trunking allows multiple VLANs to traverse a single physical link between network devices, enabling efficient communication across VLANs in a network.

Automating these configurations with Ansible simplifies deployment, reduces errors, and ensures consistency across devices.


Why Use Ansible for VLAN and Trunk Configuration?

  • Consistency: Automate repetitive tasks across multiple devices.
  • Scalability: Easily apply configurations to hundreds of devices.
  • Version Control: Track changes using Git for better management.
  • Ease of Use: Write human-readable YAML playbooks that are simple to manage.

Prerequisites

Before diving into the configuration, ensure the following:

  1. Network Devices: Switches or routers that support VLAN and trunk configurations.
  2. Ansible Installed: Set up Ansible on your control node.
  3. Device Access: Confirm SSH access to your network devices.
  4. Inventory File: Prepare an inventory file listing your devices.
  5. Ansible Modules: Familiarity with Ansible's network modules, such as ios_config for Cisco devices.

Step 1: Setting Up Your Inventory File

Create an inventory file (hosts.yml) to define your network devices:

all:
  hosts:
    switch1:
      ansible_host: 192.168.1.10
      ansible_user: admin
      ansible_password: password123
      ansible_network_os: cisco.ios
    switch2:
      ansible_host: 192.168.1.20
      ansible_user: admin
      ansible_password: password123
      ansible_network_os: cisco.ios

Step 2: Writing the Ansible Playbook

Create a playbook file (vlan_trunk_config.yml) to configure VLANs and trunking:

---
- name: Configure VLANs and Trunk Ports
  hosts: all
  gather_facts: no

  tasks:
    - name: Create VLANs
      ios_config:
        lines:
          - vlan 10
          - name Sales
          - vlan 20
          - name HR
        save_when: modified

    - name: Configure Trunk Ports
      ios_config:
        lines:
          - interface GigabitEthernet0/1
          - switchport mode trunk
          - switchport trunk allowed vlan 10,20
        save_when: modified

Step 3: Running the Playbook

Use the ansible-playbook command to execute your playbook:

ansible-playbook -i hosts.yml vlan_trunk_config.yml

The playbook will:

  • Create VLAN 10 (Sales) and VLAN 20 (HR).
  • Configure GigabitEthernet0/1 as a trunk port, allowing traffic for VLANs 10 and 20.

Step 4: Verifying the Configuration

Log into the network devices and verify the configuration:

  • Check VLANs:

    show vlan brief
    
  • Check trunk ports:

    show interfaces trunk
    

Best Practices for VLAN and Trunk Automation

  1. Use Variables: Define VLAN IDs and names in a variables file for flexibility.
  2. Test in a Lab: Always test your playbooks in a non-production environment.
  3. Backup Configurations: Create backups before applying changes.
  4. Modular Playbooks: Split tasks into separate playbooks for easier management.

Conclusion

Ansible makes VLAN and trunk configuration straightforward, enabling NetOps teams to focus on higher-level tasks. By automating these fundamental network operations, you can achieve greater efficiency and reliability in your network deployments.

Ready to take your automation skills further? Explore advanced Ansible topics like integrating with APIs or building compliance checks. Stay tuned for more tutorials on network automation!


Keywords for SEO: Ansible VLAN configuration, Ansible trunk configuration, network automation with Ansible, VLAN trunking tutorial, Cisco Ansible automation, NetOps automation tools.

Comments

Popular posts from this blog

Zero-Touch Provisioning (ZTP) with Ansible: Simplifying Network Deployments

  Zero-Touch Provisioning (ZTP) with Ansible: Simplifying Network Deployments Modern networks demand rapid scalability and minimal downtime, which makes manual device provisioning a cumbersome and error-prone task. Enter Zero-Touch Provisioning (ZTP) — a method to automate the initial configuration of devices as soon as they are powered on and connected to the network. Combining ZTP with Ansible creates a powerful solution to simplify and streamline network deployments. In this guide, we’ll explore how to implement ZTP using Ansible. What is Zero-Touch Provisioning (ZTP)? Zero-Touch Provisioning (ZTP) is a process where network devices automatically download and apply their configuration without manual intervention. Key benefits include: Time Savings: Automate initial configuration for large-scale deployments. Consistency: Ensure standardized configurations across devices. Error Reduction: Minimize manual configuration errors. ZTP typically involves: A DHCP server assigni...

Monitoring Network Devices with SNMP and Ansible: A Comprehensive Guide

  Monitoring Network Devices with SNMP and Ansible: A Comprehensive Guide Efficient network monitoring is a cornerstone of reliable IT infrastructure. Simple Network Management Protocol (SNMP) has long been a go-to tool for monitoring device health, performance, and availability. Pairing SNMP with Ansible empowers NetOps professionals to automate monitoring tasks, making network management smoother and more scalable. In this guide, we will explore how to monitor network devices using SNMP and Ansible. What is SNMP? SNMP (Simple Network Management Protocol) is a protocol used to collect and organize information about managed devices on IP networks. It enables network administrators to: Retrieve performance metrics (e.g., CPU and memory usage). Monitor interface statuses. Receive alerts for device issues (via SNMP traps). Why Integrate SNMP with Ansible? Combining SNMP with Ansible offers numerous benefits: Automation: Simplify repetitive monitoring tasks. Centralized Mana...