Exploring Infrastructure as Code with Ansible for Networking
This post introduces Infrastructure as Code with Ansible for networking, covering why Ansible is a strong choice for network automation, how to write playbooks for tasks like VLAN configuration, and how to use Jinja2 templates for scalable configuration management. It includes practical CLI example
Managing network infrastructure manually is a recipe for configuration drift, inconsistency, and late-night troubleshooting sessions. Infrastructure as Code (IaC) solves this by treating your network configurations the same way developers treat software: as version-controlled, repeatable, testable code. Ansible has become one of the most popular tools for achieving this in networking environments, and for good reason.
What Is Infrastructure as Code in a Networking Context?
Infrastructure as Code means defining your network's desired state in files rather than relying on manual CLI commands. Instead of SSHing into each device and typing commands one at a time, you describe what you want your network to look like, and a tool like Ansible makes it happen. This approach gives you:
- Repeatability: the same playbook produces the same result every time
- Version control: store your configs in Git and track every change
- Auditability: know exactly who changed what and when
- Speed: apply changes across hundreds of devices in minutes
Why Ansible for Network Automation?
Ansible stands out in the network automation space for a few practical reasons. It is agentless, meaning you do not need to install anything on your network devices. It communicates over SSH or through device APIs, which makes it compatible with a wide range of Cisco, Juniper, Arista, and other vendor equipment. Playbooks are written in YAML, which is readable even for engineers who are not Python developers.
Ansible also ships with a large collection of purpose-built network modules. For Cisco IOS devices, you have modules like cisco.ios.ios_config, cisco.ios.ios_vlans, and cisco.ios.ios_interfaces. These modules understand the structure of network configurations and handle the underlying communication details for you.
A Practical Example: Configuring VLANs with Ansible
Let's walk through a real-world scenario: pushing a VLAN configuration to a Cisco IOS switch. First, your inventory file defines your devices:
[switches]
sw01 ansible_host=192.168.1.10 ansible_network_os=cisco.ios.ios ansible_user=admin ansible_password=Cisco123 ansible_connection=network_cliNext, write a playbook that defines the desired VLAN state:
---
- name: Configure VLANs on Access Switches
hosts: switches
gather_facts: false
tasks:
- name: Ensure VLANs are present
cisco.ios.ios_vlans:
config:
- vlan_id: 10
name: HR
state: active
- vlan_id: 20
name: Engineering
state: active
state: mergedRun it with:
ansible-playbook -i inventory.ini configure_vlans.ymlAnsible connects to the switch, checks the current VLAN state, and only makes changes if they are needed. This idempotent behavior is a core IaC principle: running the playbook ten times produces the same result as running it once.
Configuration Management at Scale
Where Ansible really shines is configuration management across large environments. Using Jinja2 templates, you can generate device-specific configurations from a shared template and a variables file. This means your OSPF configuration, NTP settings, or interface descriptions can all be defined once and applied consistently across your entire fleet.
A simple template file ntp.j2 might look like this:
ntp server {{ primary_ntp }}
ntp server {{ secondary_ntp }}
clock timezone {{ timezone }} 0Pair that with a variables file per device or group, and you have a scalable, consistent configuration management workflow. Store everything in a Git repository and you have a full audit trail of every network change.
Getting Started
To begin, install Ansible and the Cisco IOS collection:
pip install ansible
ansible-galaxy collection install cisco.iosStart small. Pick one repetitive task you do manually today, whether that is configuring NTP, pushing banners, or managing VLANs. Write a playbook for it, test it in a lab, and commit it to Git. That first playbook is the foundation of your network IaC practice.
What's Next
Now that you understand the basics of Infrastructure as Code with Ansible, the logical next step is exploring how to use Ansible roles to organize your playbooks into reusable, modular components. Roles let you package tasks, templates, and variables together so your automation scales cleanly as your network grows. We will cover that in the next post.