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:
- Network Devices: Switches or routers that support VLAN and trunk configurations.
- Ansible Installed: Set up Ansible on your control node.
- Device Access: Confirm SSH access to your network devices.
- Inventory File: Prepare an inventory file listing your devices.
- Ansible Modules: Familiarity with Ansible's network modules, such as
ios_configfor 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
- Use Variables: Define VLAN IDs and names in a variables file for flexibility.
- Test in a Lab: Always test your playbooks in a non-production environment.
- Backup Configurations: Create backups before applying changes.
- 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
Post a Comment