Ansible Network Examples: Inventory, Facts and cli_command - 夜莺博客

Ansible Network Examples: Inventory, Facts and cli_command

Ansible automates network devices through a connection layer that speaks the device CLI (ansible.netcommon.network_cli), and the official documentation set includes ready-made examples that work across Arista EOS, Cisco IOS and VyOS from one playbook. This article walks through those examples: building a multi-vendor inventory, collecting facts while backing up configurations, and running platform-independent commands with cli_command.

Inventory: Groups and Connection Variables

[all:vars]
ansible_connection=ansible.netcommon.network_cli
ansible_user=ansible

[switches:children]
eos
ios
vyos

[eos]
veos01 ansible_host=veos-01.example.net

[eos:vars]
ansible_become=yes
ansible_become_method=enable
ansible_network_os=arista.eos.eos
ansible_user=my_eos_user

[ios]
ios01 ansible_host=ios-01.example.net

[ios:vars]
ansible_become=yes
ansible_become_method=enable
ansible_network_os=cisco.ios.ios

Note the pattern: ansible_network_os tells Ansible which platform module set to load, and ansible_become_method=enable handles the IOS/EOS enable-mode jump automatically.

Example 1: Collect Facts and Back Up Configs

The first official example gathers facts from every device and writes backup files of the running configuration:

---
- hosts: switches
  gather_facts: false
  connection: ansible.netcommon.network_cli
  tasks:
    - name: Collect facts from network devices
      ansible.builtin.import_role:
        name: ansible.netcommon.network
      vars:
        network_operations: gather_facts
        network_gather_facts_dir: "{{ playbook_dir }}/facts"

Running the playbook produces a recap like eos01 ok=7 changed=2 and drops a timestamped fact file per device under the facts directory - an instant configuration backup and inventory audit in one command.

Example 2: Platform-Independent cli_command

Instead of one module per vendor, ansible.netcommon.cli_command runs the same command everywhere; the when clause keeps it honest about which OS is answering:

---
- hosts: network
  gather_facts: false
  connection: ansible.netcommon.network_cli
  tasks:
    - name: Run cli_command on Arista
      ansible.netcommon.cli_command:
        command: show ip int br
      register: result
      when: ansible_network_os == 'arista.eos.eos'

    - name: Display the output
      ansible.builtin.debug:
        var: result.stdout_lines

Why This Pattern Matters

  • One inventory, many vendors - the group structure keeps secrets and connection methods per platform without duplicating playbooks.
  • Backup as a side effect - facts collection with the network role writes config backups, giving you free change history.
  • cli_command over vendor modules - fewer moving parts for read-only tasks; use vendor modules when you need structured output or config diffs.

Related Reading on This Site

See the Ansible facts collection and config backup examples and the Ansible Cisco automation playbooks.

原文链接:https://docs.ansible.com/projects/ansible/latest/network/user_guide/network_best_practices_2.5.html