Ansible Network Automation for Cisco, Juniper and Arista - 夜莺博客

Ansible Network Automation for Cisco, Juniper and Arista

Ansible's network modules let one playbook talk to Cisco IOS, Juniper Junos, Arista EOS and many other network operating systems, which makes it the fastest way to standardize show-command collection and configuration delivery in a multi-vendor environment. The tricky part for newcomers is the inventory: network devices need ansible_connection and ansible_network_os set per host, and each platform has its own command module. This article shows a working multi-vendor inventory, playbooks using both platform-specific modules and the platform-independent cli_command module, and a configuration backup pattern you can copy.

Building the Inventory

Group devices by platform so playbooks can target them selectively. Each host needs the connection type and network OS declared:

[switches:children]
eos
ios
junos

[eos]
eos01.example.net

[ios]
ios01.example.net

[junos]
junos01.example.net

[switches:vars]
ansible_connection=ansible.netcommon.network_cli
ansible_user=admin
ansible_ssh_private_key_file=~/.ssh/id_rsa

[eos:vars]
ansible_network_os=arista.eos.eos
[ios:vars]
ansible_network_os=cisco.ios.ios
[junos:vars]
ansible_network_os=junipernetworks.junos.junos

Running Commands with Platform Modules

The classic pattern registers the command output and prints it, using a when condition per platform:

---
- hosts: switches
  gather_facts: false
  tasks:
    - name: Run show command on Arista EOS
      arista.eos.eos_command:
        commands: show ip interface brief
      register: result
      when: ansible_network_os == 'arista.eos.eos'

    - name: Run show command on Cisco IOS
      cisco.ios.ios_command:
        commands: show ip interface brief
      register: result
      when: ansible_network_os == 'cisco.ios.ios'

    - name: Run show command on Junos
      junipernetworks.junos.junos_command:
        commands: show interfaces terse
      register: result
      when: ansible_network_os == 'junipernetworks.junos.junos'

    - name: Display collected output
      debug:
        var: result.stdout_lines

Simplifying with the Platform-Independent cli_command

When the command syntax is identical across platforms, drop the per-vendor modules and use ansible.netcommon.cli_command with a single task:

---
- hosts: switches
  gather_facts: false
  connection: ansible.netcommon.network_cli
  tasks:
    - name: Collect interface status from all platforms
      ansible.netcommon.cli_command:
        command: show interfaces status
      register: result
      when: ansible_network_os in ['arista.eos.eos', 'cisco.ios.ios', 'junipernetworks.junos.junos']

    - name: Display results
      debug:
        var: result.stdout_lines

Backing Up Running Configurations

Gather facts and save each device's running configuration locally - the standard backup pattern:

---
- hosts: switches
  gather_facts: false
  tasks:
    - name: Backup config from each platform
      cisco.ios.ios_config:
        backup: yes
      when: ansible_network_os == 'cisco.ios.ios'

    - name: Backup Junos configuration
      junipernetworks.junos.junos_config:
        backup: yes
      when: ansible_network_os == 'junipernetworks.junos.junos'

Backups land in the playbook's backup/ directory with hostname and timestamp in the filename.

Configuration Changes: cli_config and Resource Modules

For configuration delivery, ansible.netcommon.cli_config applies a config snippet over network_cli, while vendor resource modules (cisco.ios.ios_vlans, arista.eos.eos_vlans, junipernetworks.junos.junos_vlans) declare desired state and let Ansible compute the diff - the safest model for idempotent VLAN, interface, SNMP and NTP management:

---
- hosts: cisco
  gather_facts: false
  tasks:
    - name: Manage VLANs with a resource module
      cisco.ios.ios_vlans:
        config:
          - name: servers
            vlan_id: 10
        state: merged

Verify Your Playbook

$ ansible-playbook -i inventory.ini collect_show.yml
$ ansible-playbook -i inventory.ini --syntax-check backup.yml

Related reading: Ansible network facts and config backup examples and Ansible Cisco network automation playbooks.

Original article: Ansible Network Examples (official docs) | Juniper: Ansible for Junos modules overview