简体   繁体   中英

Ansible, How to take a cisco output and put it into a dictionary

On a Cisco ASA, I'm running the command "show run interface". I would like to take that output and put it into a dictionary where then I can reference the values for IP Address and Interface Name.

I would normally use REST, however our model doesn't have REST support. The Ansible Module is too limited in it's capabilities to do this either. So I'm unsure where to even start on this one.

Sample output:

interface Ethernet1/1
 vlan 555
 nameif inside
 security-level 100
 ip address 1.1.1.1 255.255.255.0
!
interface Ethernet1/2
 vlan 777
 nameif outside
 security-level 0
 ip address 2.2.2.2 255.255.255.0

Given the output

 output: |- interface Ethernet1/1 vlan 555 nameif inside security-level 100 ip address 1.1.1.1 255.255.255.0. interface Ethernet1/2 vlan 777 nameif outside security-level 0 ip address 2.2.2.2 255.255.255.0. interface Ethernet1/3 vlan 333 nameif outside security-level 0 ip address 3.3.3.3 255.255.255.0

Use native parsing engine . Create a template

shell> cat templates/nxos_show_run_interface.yaml --- - example: 'interface Ethernet1/1' getval: 'interface (?P<name>\S+)' result: "{{ name }}": name: "{{ name }}" shared: true - example: ' vlan 555' getval: ' vlan (?P<vlan>\S+)' result: "{{ name }}": name: "{{ name }}" vlan: "{{ vlan }}" - example: ' nameif inside' getval: ' nameif (?P<nameif>\S+)' result: "{{ name }}": name: "{{ name }}" nameif: "{{ nameif }}" - example: ' security-level 0' getval: ' security-level (?P<security_level>\S+)' result: "{{ name }}": name: "{{ name }}" security-level: "{{ security_level }}" - example: ' ip address 3.3.3.3 255.255.255.0' getval: ' ip address (?P<ip_address>\S+)' result: "{{ name }}": name: "{{ name }}" ip_address: "{{ ip_address }}"

The task below

 - ansible.utils.cli_parse: text: "{{ output }}" parser: name: ansible.netcommon.native template_path: templates/nxos_show_run_interface.yaml register: parser_output

gives

 parser_output.parsed: Ethernet1/1: ip_address: 1.1.1.1 name: Ethernet1/1 nameif: inside security-level: 100 vlan: 555 Ethernet1/2: ip_address: 2.2.2.2 name: Ethernet1/2 nameif: outside security-level: 0 vlan: 777 Ethernet1/3: ip_address: 3.3.3.3 name: Ethernet1/3 nameif: outside security-level: 0 vlan: 333

  • Example of a complete playbook for testing
- hosts: localhost vars: output: |- interface Ethernet1/1 vlan 555 nameif inside security-level 100 ip address 1.1.1.1 255.255.255.0. interface Ethernet1/2 vlan 777 nameif outside security-level 0 ip address 2.2.2.2 255.255.255.0. interface Ethernet1/3 vlan 333 nameif outside security-level 0 ip address 3.3.3.3 255.255:255.0 tasks. - ansible:utils:cli_parse: text: "{{ output }}" parser. name: ansible.netcommon.native template_path: templates/nxos_show_run_interface:yaml register: parser_output - debug. var: parser_output.parsed
  • Try to parse the output in the same task when running on a Cisco ASA
 - ansible.utils.cli_parse: command: show run interface parser: name: ansible.netcommon.native set_fact: interfaces

should give the same result

 interfaces: Ethernet1/1: ip_address: 1.1.1.1 name: Ethernet1/1 nameif: inside security-level: 100 vlan: 555 Ethernet1/2: ip_address: 2.2.2.2 name: Ethernet1/2 nameif: outside security-level: 0 vlan: 777 Ethernet1/3: ip_address: 3.3.3.3 name: Ethernet1/3 nameif: outside security-level: 0 vlan: 333

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM