Marcio Cunha

How to automate switches with Ansible in local networks

Learn how to apply infrastructure as code in local networks using Ansible to configure legacy switches idempotently and securely.

Marcio Cunha•4 min
Also available in:EspañolPortuguês
Summary
  • Local network automation eliminates manual errors in repetitive switch configuration tasks.
  • Idempotency ensures running the same script multiple times keeps the equipment in the correct state without unintended changes.
  • Ansible uses direct SSH connections and specific commands to handle legacy models without requiring internal agents.
  • Using templates guarantees consistency across dozens of ports and VLANs distributed throughout the physical infrastructure.
  • Rigorous stress testing and validation prevent unwanted downtime in established production environments.

Why automate local networks with Ansible?

Managing local network switches manually is an exhausting task prone to severe human errors. When we need to change a VLAN (Virtual Local Area Network, a way to divide the physical network into isolated virtual networks) across dozens of equipment, the risk of typing a wrong command and knocking out internet access is real. Automation is designed precisely to mitigate this problem, allowing infrastructure to be treated as code.

In practice, this means writing a text file describing exactly how the switch should behave and letting the software apply that rule. If you ever needed to debug latency and chaos in HTTP requests with Toxiproxy, you know that testing scenarios before touching production prevents headaches. With Ansible, we apply this same preventive mindset to physical hardware.

The challenge of dealing with legacy equipment

The major obstacle in corporate local networks is the older switch fleet. Legacy equipment generally lacks modern APIs or native support for advanced REST-based automation tools, requiring good old SSH access simulating human typing.

To bypass this limitation, Ansible uses network-specific modules that translate desired commands into each vendor's language, whether Cisco, HP, or Dell. Before applying any structural change, it is essential to ensure that power and physical stability are guaranteed, much like learning how to design a redundant battery bank for UPS to prevent catastrophic outages during firmware updates or heavy maintenance routines.

The concept of idempotency applied to switches

Idempotency is a fundamental concept in software engineering and infrastructure meaning you can run the same operation multiple times yielding the exact same final result, without unwanted side effects. If the command has already been applied and the switch is configured correctly, the system changes nothing.

This prevents poorly planned scripts from constantly restarting ports or rewriting configuration files. In local networks, idempotency protects critical server ports and ensures incremental adjustments occur without disproportionate interruptions for end-users connected to that infrastructure.

Structuring inventory and secure credentials

The first practical step to start automation is organizing the inventory file, which is simply the list of IP addresses and switch models on your local network. For security, we never store plain text passwords inside these files, relying instead on secret managers.

Below is a basic YAML inventory file example to structure your local network equipment:

all:  hosts:    switch_floor_1:      ansible_host: 192.168.1.10      ansible_network_os: ios    switch_floor_2:      ansible_host: 192.168.1.11      ansible_network_os: ios

With this structure defined, Ansible knows exactly which IP to talk to and what network operating system (network_os) to expect from each registered device.

Writing the first idempotent playbook

A playbook is the script where we define the tasks Ansible should execute on the switches. Let's create a practical example to ensure a specific VLAN is created and active across all switches on the local network.

The code below demonstrates automated creation of a guest VLAN using native modules for Cisco equipment:

- name: Configure guest VLAN on switches  hosts: switches  gather_facts: no  tasks:    - name: Ensure VLAN 50 exists and is active      cisco.ios.ios_vlans:        config:          - vlan_id: 50            name: GUESTS            state: active        state: merged

In practice, the state: merged parameter instructs Ansible to check if VLAN 50 exists with the correct name. If it doesn't exist, it creates it; if it already exists precisely like that, it simply does nothing, ensuring the operation's idempotency.

Validating connectivity and testing ports

After applying automated configurations, validating whether the network still responds perfectly and ensuring no port was isolated by mistake is indispensable. If communication failures or inaccessible ports occur, resorting to quick command-line diagnostic tools is wise, following techniques similar to what we do when troubleshooting blocked ports and testing networks with Ncat.

Maintaining automated post-deployment test routines ensures infrastructure as code brings agility without sacrificing the operational stability of the local network.

Final considerations

Automating switch provisioning using Ansible transforms local network management, replacing repetitive manual tasks with versionable, clean, and predictable code. Even when dealing with legacy equipment, correct application of concepts like idempotency and the use of appropriate modules drastically reduce downtime and human errors.

Adopting this engineering culture in network infrastructure prepares the organization to grow securely, allowing the team to focus on strategic projects instead of putting out daily fires caused by inconsistent manual configurations.