Skip to content

Commit

Permalink
show ip vrf detail, more tests for show ip bgp summary (ansible-netwo…
Browse files Browse the repository at this point in the history
…rk#11)

* task file per command for future growth plus more tests

* merging

* cleaning

* Seperating tests for each command

* tests

* working parser template

* Interfaces being captured, tests

* `show ip vrf detail` parser and more tests (ansible-network#1)

* parser for `show ip vrf detail`
* more parser tests

* Test case for BGP not active. Fixed to pass tests.

* forgot the command map...
  • Loading branch information
Luke Russell authored and privateip committed Aug 31, 2018
1 parent e9e842c commit d990e28
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 39 deletions.
50 changes: 41 additions & 9 deletions parser_templates/cli/show_ip_bgp_summary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,60 @@
command: show ip bgp summary
network_os: ios

- name: match not active
register: not_active
export: true
pattern_match:
regex: "BGP not active"
match_all: true

- name: set_vars bgp state active
export: true
set_vars:
process_state: "active"

- name: set_vars bgp state not active
export: true
set_vars:
process_state: "not active"
when: "not_active.0.matches == 'BGP not active'"

- name: match sections
register: context
pattern_match:
regex: "Neighbor.+"
match_all: yes
match_greedy: yes
register: context
export: false
when: process_state == 'active'

- name: match lines
register: lines
pattern_match:
regex: "^[0-9a-f.]+"
content: "{{ context.0 }}"
match_all: yes
match_greedy: yes
content: "{{ context.0 }}"
register: lines
export: false
when: process_state == 'active'

- name: match neighbors
register: neighbors
loop: "{{ lines }}"
pattern_match:
regex: "(?P<ip>[0-9a-f.]+)\\s+(?P<version>\\d+)\\s+(?P<asn>\\d+)\\s+(?P<msgrcvd>\\d+)\\s+(?P<msgsent>\\d+)\\s+(?P<tblver>\\d+)\\s+(?P<inq>\\d+)\\s+(?P<outq>\\d+)\\s+(?P<timer>\\S+)\\s+(?P<state>\\S+)"
content: "{{ item }}"
loop: "{{ lines }}"
register: neighbors
export: false
when: process_state == 'active'

- name: template entries
register: ip_bgp_summary
loop: "{{ neighbors }}"
export: true
export_as: dict
extend: cisco_ios
when: process_state == 'active'
json_template:
template:
- key: "process_state"
value: "active"
- key: "{{ item.ip }}"
object:
- key: state_pfxrcd
Expand All @@ -44,8 +70,14 @@
value: "{{ item.timer }}"
- key: ip_version
value: "{{ item.version }}"

- name: template entries
register: ip_bgp_summary
loop: "{{ neighbors }}"
export: true
export_as: dict
extend: cisco_ios
when: process_state == 'not active'
json_template:
template:
- key: "process_state"
value: "not active"
147 changes: 147 additions & 0 deletions parser_templates/cli/show_ip_vrf_detail.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---

- name: show_ip_vrf_detail
parser_metadata:
version: 1.0
command: show ip vrf detail
network_os: ios

- name: match vrf section
register: section
pattern_match:
regex: "^VRF \\S+; default RD"
match_all: true
match_greedy: true

- name: match vrf values
loop: "{{ section }}"
register: vrf
pattern_group:

- name: match name
pattern_match:
regex: "^VRF (\\S+); default RD"
content: "{{ item }}"
register: name

- name: match description
pattern_match:
regex: "^ Description: (.*)\\n"
content: "{{ item }}"
register: description

- name: match route distinguisher
pattern_match:
regex: ".*; default RD (\\d+:\\d+)"
content: "{{ item }}"
register: rd

- name: match export route target section
pattern_match:
regex: "^ Export VPN route-target communities([\\s\\S]*)^ Import VPN"
content: "{{ item }}"
match_all: true
match_greedy: false
register: export_rt_section

- name: match import route target section
pattern_match:
regex: "^ Import VPN route-target communities([\\s\\S]*)import route-map"
content: "{{ item }}"
match_all: true
match_greedy: false
register: import_rt_section

- name: match interface section
pattern_match:
regex: "^ Interfaces:([\\s\\S]*)VRF Table ID"
content: "{{ item }}"
match_all: true
match_greedy: false
register: interface_section

- name: match vrf nested values
loop: "{{ vrf }}"
loop_control:
loop_var: vrf_item
register: vrf_nested
pattern_group:

- name: match vrf name
pattern_match:
regex: "(.*)"
content: "{{ vrf_item.name.matches.0 }}"
match_greedy: true
register: name

- name: match export route targets
pattern_match:
regex: "\\s+RT:(\\d+:\\d+)"
content: "{{ vrf_item.export_rt_section.0.matches }}"
match_all: true
register: export_rt

- name: match import route targets
pattern_match:
regex: "\\s+RT:(\\d+:\\d+)"
content: "{{ vrf_item.import_rt_section.0.matches }}"
match_all: true
register: import_rt

- name: match interfaces
pattern_match:
regex: "[\\s|\\\n]+(\\S*)[\\s|\\n]"
content: "{{ vrf_item.interface_section.0.matches }}"
match_all: true
register: interface

- name: build a dict for nested values with name as key
register: vrf_nested_values
export_as: dict
loop: "{{ vrf_nested }}"
loop_control:
loop_var: vrf_nested_item
json_template:
template:
- key: "{{ vrf_nested_item.name.0 }}"
object:
- key: export_rt
elements: "{{ nested_item.matches }}"
repeat_for: "{{ vrf_nested_item.export_rt }}"
repeat_var: nested_item
- key: import_rt
elements: "{{ nested_item.matches }}"
repeat_for: "{{ vrf_nested_item.import_rt }}"
repeat_var: nested_item
- key: interface
elements: "{{ nested_item.matches }}"
repeat_for: "{{ vrf_nested_item.interface }}"
repeat_var: nested_item

- name: ensure vrf_nested_values is a DICT
set_vars:
dict_vrf_nested_values: "{{ ({}| combine(*vrf_nested_values)) }}"

- name: template export json object
register: ip_vrf_detail
loop: "{{ vrf }}"
export: true
export_as: dict
extend: cisco_ios
json_template:
template:
- key: "{{ item.name.matches.0 }}"
object:
- key: name
value: "{{ item.name.matches.0 }}"
- key: description
value: "{{ item.description.matches.0 }}"
- key: rd
value: "{{ item.rd.matches.0 }}"
- key: export_rt
value: "{{ dict_vrf_nested_values[item.name.matches.0].export_rt }}"
- key: import_rt
value: "{{ dict_vrf_nested_values[item.name.matches.0].import_rt }}"
- key: interfaces
value: "{{ dict_vrf_nested_values[item.name.matches.0].interface }}"

4 changes: 1 addition & 3 deletions tests/output/show_ip_bgp_summary/03.14.00.S.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ BGP activity 18292/15523 prefixes, 74591/69053 paths, scan interval 60 secs

Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
10.15.17.2 4 64785 748767 766529 79501 0 0 7w1d 1125
172.31.3.17 4 65530 795592 739710 79501 0 0 7w1d 1641
10.1.19.2 4 1234 0 0 1 0 0 00:00:30 Active
72.31.0.13 4 65004 0 0 1 0 0 00:00:45 Idle
72.31.0.130 4 65530 8027 6488 2287 0 0 10:32:44 1644
72.31.0.13 4 65004 0 0 1 0 0 10:32:44 Idle
16 changes: 16 additions & 0 deletions tests/output/show_ip_bgp_summary/12.2(33)SXH5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
BGP router identifier 172.16.32.1, local AS number 65123
BGP table version is 3317, main routing table version 3317
140 network entries using 16380 bytes of memory
276 path entries using 14352 bytes of memory
1184/12 BGP path/bestpath attribute entries using 165760 bytes of memory
108 BGP AS-PATH entries using 2832 bytes of memory
84 BGP community entries using 2016 bytes of memory
74 BGP extended community entries using 1920 bytes of memory
0 BGP route-map cache entries using 0 bytes of memory
0 BGP filter-list cache entries using 0 bytes of memory
BGP using 203260 total bytes of memory
BGP activity 2362466/2358636 prefixes, 9381389/9370747 paths, scan interval 60 secs

Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
172.31.32.10 4 64910 5784429 5675902 3317 0 0 23w5d 114
172.16.32.1 4 65123 6281459 6288574 3317 0 0 28w5d 136
14 changes: 14 additions & 0 deletions tests/output/show_ip_bgp_summary/15.1(2)T5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BGP router identifier 10.29.19.69, local AS number 65312
BGP table version is 8405, main routing table version 8405
1390 network entries using 189040 bytes of memory
1390 path entries using 72280 bytes of memory
1016/1016 BGP path/bestpath attribute entries using 125984 bytes of memory
1008 BGP AS-PATH entries using 24516 bytes of memory
2 BGP extended community entries using 48 bytes of memory
0 BGP route-map cache entries using 0 bytes of memory
0 BGP filter-list cache entries using 0 bytes of memory
BGP using 411868 total bytes of memory
BGP activity 3131/1741 prefixes, 4889/3499 paths, scan interval 60 secs

Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
10.29.19.70 4 7474 78662 72306 8405 0 0 4d22h 1385
1 change: 1 addition & 0 deletions tests/output/show_ip_bgp_summary/bgp_not_active.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
% BGP not active
90 changes: 90 additions & 0 deletions tests/output/show_ip_bgp_summary/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---

### BGP not active

- name: not active - parse `show ip bgp summary`
command_parser:
file: "{{ playbook_dir }}/../parser_templates/cli/show_ip_bgp_summary.yaml"
content: "{{ lookup('file', '{{ playbook_dir }}/output/show_ip_bgp_summary/bgp_not_active.txt') }}"
register: result

- name: not active - test `show ip bgp summary` parser
assert:
that:
- "cisco_ios['ip_bgp_summary']['process_state'] == 'not active'"

- name: clear facts
meta: clear_facts

### IOS-XE 03.14.00.S

- name: 03.14.00.S.txt - parse `show ip bgp summary`
command_parser:
file: "{{ playbook_dir }}/../parser_templates/cli/show_ip_bgp_summary.yaml"
content: "{{ lookup('file', '{{ playbook_dir }}/output/show_ip_bgp_summary/03.14.00.S.txt') }}"
register: result

- name: 03.14.00.S.txt - test `show ip bgp summary` parser
assert:
that:
- "'ip_bgp_summary' in cisco_ios"
- "'10.15.17.2' in cisco_ios['ip_bgp_summary']"
- "cisco_ios['ip_bgp_summary']['10.15.17.2']['state_pfxrcd'] == 1125"
- "cisco_ios['ip_bgp_summary']['10.15.17.2']['asn'] == 64785"
- "cisco_ios['ip_bgp_summary']['10.15.17.2']['ip_version'] == 4"
- "cisco_ios['ip_bgp_summary']['10.15.17.2']['timer'] == '7w1d'"
- "'10.1.19.2' in cisco_ios['ip_bgp_summary']"
- "cisco_ios['ip_bgp_summary']['10.1.19.2']['state_pfxrcd'] == 'Active'"
- "cisco_ios['ip_bgp_summary']['10.1.19.2']['asn'] == 1234"
- "cisco_ios['ip_bgp_summary']['10.1.19.2']['ip_version'] == 4"
- "'{{ cisco_ios['ip_bgp_summary']['10.1.19.2']['timer'] | string }}' == '00:00:30'"
- "'72.31.0.13' in cisco_ios['ip_bgp_summary']"
- "cisco_ios['ip_bgp_summary']['72.31.0.13']['state_pfxrcd'] == 'Idle'"
- "cisco_ios['ip_bgp_summary']['72.31.0.13']['asn'] == 65004"
- "cisco_ios['ip_bgp_summary']['72.31.0.13']['ip_version'] == 4"
- "'{{ cisco_ios['ip_bgp_summary']['72.31.0.13']['timer'] | string }}' == '10:32:44'"

### IOS 15.1(2)T5.txt

- name: 15.1(2)T5.txt - parse `show ip bgp summary`
command_parser:
file: "{{ playbook_dir }}/../parser_templates/cli/show_ip_bgp_summary.yaml"
content: "{{ lookup('file', '{{ playbook_dir }}/output/show_ip_bgp_summary/15.1(2)T5.txt') }}"
register: result

- name: 15.1(2)T5.txt - test `show ip bgp summary` parser
assert:
that:
- "'ip_bgp_summary' in cisco_ios"
- "'10.29.19.70' in cisco_ios['ip_bgp_summary']"
- "cisco_ios['ip_bgp_summary']['10.29.19.70']['state_pfxrcd'] == 1385"
- "cisco_ios['ip_bgp_summary']['10.29.19.70']['asn'] == 7474"
- "cisco_ios['ip_bgp_summary']['10.29.19.70']['ip_version'] == 4"
- "cisco_ios['ip_bgp_summary']['10.29.19.70']['timer'] == '4d22h'"

### IOS 12.2(33)SXH5.txt

- name: 12.2(33)SXH5.txt - parse `show ip bgp summary`
command_parser:
file: "{{ playbook_dir }}/../parser_templates/cli/show_ip_bgp_summary.yaml"
content: "{{ lookup('file', '{{ playbook_dir }}/output/show_ip_bgp_summary/12.2(33)SXH5.txt') }}"
register: result

- name: 12.2(33)SXH5.txt - test `show ip bgp summary` parser
assert:
that:
- "'ip_bgp_summary' in cisco_ios"
- "'172.31.32.10' in cisco_ios['ip_bgp_summary']"
- "cisco_ios['ip_bgp_summary']['172.31.32.10']['state_pfxrcd'] == 114"
- "cisco_ios['ip_bgp_summary']['172.31.32.10']['asn'] == 64910"
- "cisco_ios['ip_bgp_summary']['172.31.32.10']['ip_version'] == 4"
- "cisco_ios['ip_bgp_summary']['172.31.32.10']['timer'] == '23w5d'"
- "'172.16.32.1' in cisco_ios['ip_bgp_summary']"
- "cisco_ios['ip_bgp_summary']['172.16.32.1']['state_pfxrcd'] == 136"
- "cisco_ios['ip_bgp_summary']['172.16.32.1']['asn'] == 65123"
- "cisco_ios['ip_bgp_summary']['172.16.32.1']['ip_version'] == 4"
- "'{{ cisco_ios['ip_bgp_summary']['172.16.32.1']['timer'] | string }}' == '28w5d'"

- name: all tests complete
debug:
msg: "All tests for `show ip bgp summary` passed"

0 comments on commit d990e28

Please sign in to comment.