Ansible Network Automation

Network devices such as routers, switches, and firewalls usually need typing into a command line, one device at a time. A change to fifty switches means fifty sessions and fifty chances for a typo. Ansible automates this work. Most network devices cannot run Python, so Ansible connects from the control node and sends commands through the device's own interface. Imagine a traffic controller who writes one set of instructions and sends them to every intersection at once.

Diagram: How Ansible Reaches Devices

  ┌──────────────┐
  │   Ansible    │  runs modules locally
  │ control node │  then talks to devices
  └──────┬───────┘
         │ network_cli (SSH)  /  netconf  /  httpapi
         ├───────────────────┬───────────────────┐
         ▼                   ▼                   ▼
   ┌───────────┐       ┌───────────┐       ┌───────────┐
   │ Router    │       │ Switch    │       │ Firewall  │
   │ (Cisco)   │       │ (Arista)  │       │ (Juniper) │
   └───────────┘       └───────────┘       └───────────┘

Connection Types

ConnectionUsed For
ansible.netcommon.network_cliDevices with a command line over SSH
ansible.netcommon.netconfDevices that support the NETCONF protocol
ansible.netcommon.httpapiDevices with a web based API
ansible.netcommon.localOlder modules that manage their own connection

Install Platform Collections

ansible-galaxy collection install cisco.ios arista.eos junipernetworks.junos ansible.netcommon
python3 -m pip install paramiko ansible-pylibssh

Define the Inventory

[switches]
sw1 ansible_host=10.1.1.11
sw2 ansible_host=10.1.1.12

[switches:vars]
ansible_network_os=cisco.ios.ios
ansible_connection=ansible.netcommon.network_cli
ansible_user=netadmin
ansible_become=true
ansible_become_method=enable

Store ansible_password in an encrypted vault file under group_vars/switches/ rather than in the inventory.

Collect Information

- name: Read switch details
  hosts: switches
  gather_facts: false
  tasks:
    - name: Show the version
      cisco.ios.ios_command:
        commands:
          - show version
      register: version_output

    - name: Print the first lines
      ansible.builtin.debug:
        msg: "{{ version_output.stdout_lines[0][:5] }}"

    - name: Gather structured facts
      cisco.ios.ios_facts:
        gather_subset: interfaces

Change Configuration

The ios_config module compares the wanted lines with the running configuration and sends only missing lines. This behavior keeps tasks idempotent.

    - name: Create a management banner and a VLAN
      cisco.ios.ios_config:
        lines:
          - banner motd ^Authorized access only^
        save_when: modified

    - name: Configure VLAN 20
      cisco.ios.ios_config:
        lines:
          - name Finance
        parents: vlan 20

Use Resource Modules

Resource modules describe a feature as structured data. They work the same way across vendors, so skills carry from one platform to another.

    - name: Set interface descriptions
      cisco.ios.ios_interfaces:
        config:
          - name: GigabitEthernet0/1
            description: Uplink to core
            enabled: true
        state: merged
StateBehavior
mergedAdds the given settings and keeps the rest
replacedReplaces settings of the listed items
overriddenMakes the device match the list exactly
deletedRemoves the listed settings
gatheredReads current settings as structured data

Back Up Configurations

    - name: Save the running configuration
      cisco.ios.ios_config:
        backup: true
        backup_options:
          dir_path: ./backups
          filename: "{{ inventory_hostname }}.cfg"

A daily playbook run produces a history of device settings that supports audits and quick recovery.

Safe Change Practices

  • Run with --check --diff to preview changes
  • Test on a lab device or virtual image before touching production
  • Use serial to change a small group at a time
  • Back up the configuration before every change
  • Keep credentials in Ansible Vault

Try This

Install a free network simulator or use a vendor sandbox. Add one device to the inventory. Run a playbook that gathers the version output and prints it. Add an interface description with a resource module and run the playbook twice to confirm the second run changes nothing.

Summary

Ansible automates routers, switches, and firewalls through SSH, NETCONF, and API connections. Platform collections supply command, configuration, fact, and resource modules. Idempotent configuration tasks, regular backups, and careful preview runs bring speed and consistency to network changes.