NetwNETW
Ansible course
CHAPTER 07

Ansible for Networking

Configuring routers, switches, and firewalls

12 min read3 sections
In this chapter
Why Ansible for networking?Network modulesExample: configure a switch

01Why Ansible for networking?

Network devices are often configured by hand (CLI). This is slow, error-prone, and hard to audit. Ansible automates it: you write the desired config in YAML, and Ansible pushes it to all devices. Rollback is as simple as reverting the playbook.

Why this exists

Network engineers spend hours typing commands into routers. Ansible turns that into a repeatable, reviewable, version-controlled process.

02Network modules

Ansible has modules for Cisco IOS, Arista EOS, Juniper JunOS, F5, and more. These modules connect over SSH or API and push configuration.

  • ▸cisco.ios.ios_config: push config to Cisco IOS devices
  • ▸arista.eos.eos_config: push config to Arista EOS devices
  • ▸junipernetworks.junos.junos_config: push config to Juniper JunOS
  • ▸ansible.netcommon.cli_command: run any CLI command on a network device
  • ▸ansible.netcommon.net_lldp: gather LLDP neighbor info
  • ▸f5networks.f5_modules.bigip_virtual_server: configure F5 load balancers

03Example: configure a switch

This playbook configures a Cisco access switch with VLANs and access ports.

---
- name: Configure access switch
  hosts: switches
  gather_facts: no
  connection: network_cli
  tasks:
    - name: Set hostname
      cisco.ios.ios_config:
        lines:
          - hostname SW-ACCESS-01

    - name: Configure VLANs
      cisco.ios.ios_config:
        lines:
          - vlan 10
          - name USERS
          - vlan 20
          - name SERVERS

    - name: Configure access port
      cisco.ios.ios_config:
        lines:
          - interface GigabitEthernet0/1
          - switchport mode access
          - switchport access vlan 10
          - spanning-tree portfast

    - name: Save config
      cisco.ios.ios_config:
        save_when: modified
Check your understanding
0 / 1 correct
Why use Ansible for network devices?