How to Parse Network Config into Structured Data Using Ansible Resource Modules

How to Parse Network Config into Structured Data Using Ansible Resource Modules

Ansible Resource Modules offer various states to manage network configuration sections effectively. For example use parsed to convert configurations into structured data, like so:

---
- name: Ansible Modules - Resource Modules (Parse)
  hosts: leaf1
  gather_facts: false

  tasks:
    - name: Parse the provided configuration
      cisco.ios.ios_l3_interfaces:
        running_config: "{{ lookup('file', 'leaf1_backup.cfg') }}"
        state: parsed
      register: l3_interfaces_parsed

    - name: Print parsed configuration
      debug:
        var: l3_interfaces_parsed.parsed

# Output:
# $ ansible-playbook -i inventory.yaml playbooks/pb_resource_module_parse.yaml
# ...
# ok: [leaf1] =>
#   l3_interfaces_parsed.parsed:
#   - ipv4:
#     - address: 10.1.1.1/30
#     name: GigabitEthernet0/0
#   - ipv4:
#     - address: 10.1.2.1/30
#     name: GigabitEthernet0/1
# ...

Subscribe to our newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!