mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 22:51:23 -07:00
Fix vrf parsing in eos_vrf and eos_eapi module (#35791)
* Fix vrf parsing in eos_vrf and eos_eapi module Fixes #30250 Fix logic to parse vrf when interface value in `show vrf` command output spans on multiple lines * Add idempotent test case
This commit is contained in:
parent
7a711cf942
commit
6a223d5576
3 changed files with 103 additions and 26 deletions
|
@ -213,7 +213,15 @@ def validate_local_http_port(value, module):
|
||||||
|
|
||||||
def validate_vrf(value, module):
|
def validate_vrf(value, module):
|
||||||
out = run_commands(module, ['show vrf'])
|
out = run_commands(module, ['show vrf'])
|
||||||
configured_vrfs = re.findall(r'^\s+(\w+)(?=\s)', out[0], re.M)
|
configured_vrfs = []
|
||||||
|
lines = out[0].strip().splitlines()[3:]
|
||||||
|
for l in lines:
|
||||||
|
if not l:
|
||||||
|
continue
|
||||||
|
splitted_line = re.split(r'\s{2,}', l.strip())
|
||||||
|
if len(splitted_line) > 2:
|
||||||
|
configured_vrfs.append(splitted_line[0])
|
||||||
|
|
||||||
configured_vrfs.append('default')
|
configured_vrfs.append('default')
|
||||||
if value not in configured_vrfs:
|
if value not in configured_vrfs:
|
||||||
module.fail_json(msg='vrf `%s` is not configured on the system' % value)
|
module.fail_json(msg='vrf `%s` is not configured on the system' % value)
|
||||||
|
|
|
@ -184,28 +184,43 @@ def map_obj_to_commands(updates, module):
|
||||||
def map_config_to_obj(module):
|
def map_config_to_obj(module):
|
||||||
objs = []
|
objs = []
|
||||||
output = run_commands(module, ['show vrf'])
|
output = run_commands(module, ['show vrf'])
|
||||||
lines = output[0].strip().splitlines()[2:]
|
|
||||||
|
|
||||||
for l in lines:
|
lines = output[0].strip().splitlines()[3:]
|
||||||
if not l:
|
|
||||||
|
out_len = len(lines)
|
||||||
|
index = 0
|
||||||
|
while out_len > index:
|
||||||
|
line = lines[index]
|
||||||
|
if not line:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
splitted_line = re.split(r'\s{2,}', l.strip())
|
splitted_line = re.split(r'\s{2,}', line.strip())
|
||||||
|
|
||||||
if len(splitted_line) == 1:
|
if len(splitted_line) == 1:
|
||||||
|
index += 1
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
obj = {}
|
obj = dict()
|
||||||
obj['name'] = splitted_line[0]
|
obj['name'] = splitted_line[0]
|
||||||
obj['rd'] = splitted_line[1]
|
obj['rd'] = splitted_line[1]
|
||||||
obj['interfaces'] = []
|
obj['interfaces'] = []
|
||||||
|
|
||||||
if len(splitted_line) > 4:
|
if len(splitted_line) > 4:
|
||||||
obj['interfaces'] = []
|
obj['interfaces'] = []
|
||||||
|
interfaces = splitted_line[4]
|
||||||
|
if interfaces.endswith(','):
|
||||||
|
while interfaces.endswith(','):
|
||||||
|
# gather all comma separated interfaces
|
||||||
|
if out_len <= index:
|
||||||
|
break
|
||||||
|
index += 1
|
||||||
|
line = lines[index]
|
||||||
|
vrf_line = re.split(r'\s{2,}', line.strip())
|
||||||
|
interfaces += vrf_line[-1]
|
||||||
|
|
||||||
for i in splitted_line[4].split(','):
|
for i in interfaces.split(','):
|
||||||
obj['interfaces'].append(i.strip().lower())
|
obj['interfaces'].append(i.strip().lower())
|
||||||
|
index += 1
|
||||||
objs.append(obj)
|
objs.append(obj)
|
||||||
|
|
||||||
return objs
|
return objs
|
||||||
|
@ -308,5 +323,6 @@ def main():
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -2,27 +2,18 @@
|
||||||
|
|
||||||
- name: setup - remove vrf
|
- name: setup - remove vrf
|
||||||
eos_vrf:
|
eos_vrf:
|
||||||
name: test
|
name: "{{ item }}"
|
||||||
state: absent
|
|
||||||
authorize: yes
|
|
||||||
provider: "{{ cli }}"
|
|
||||||
become: yes
|
|
||||||
|
|
||||||
- name: setup - remove vrf
|
|
||||||
eos_vrf:
|
|
||||||
name: test2
|
|
||||||
state: absent
|
|
||||||
authorize: yes
|
|
||||||
provider: "{{ cli }}"
|
|
||||||
become: yes
|
|
||||||
|
|
||||||
- name: setup - remove vrf
|
|
||||||
eos_vrf:
|
|
||||||
name: test3
|
|
||||||
state: absent
|
state: absent
|
||||||
authorize: yes
|
authorize: yes
|
||||||
provider: "{{ cli }}"
|
provider: "{{ cli }}"
|
||||||
become: yes
|
become: yes
|
||||||
|
with_items:
|
||||||
|
- test
|
||||||
|
- test1
|
||||||
|
- test2
|
||||||
|
- test3
|
||||||
|
- test4
|
||||||
|
- test5
|
||||||
|
|
||||||
- name: Create vrf
|
- name: Create vrf
|
||||||
eos_vrf:
|
eos_vrf:
|
||||||
|
@ -133,6 +124,66 @@
|
||||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||||
- "'session_name' not in result.commands"
|
- "'session_name' not in result.commands"
|
||||||
|
|
||||||
|
- name: Add multiple interfaces to vrf
|
||||||
|
eos_vrf:
|
||||||
|
name: test1
|
||||||
|
rd: 1:202
|
||||||
|
state: present
|
||||||
|
authorize: yes
|
||||||
|
interfaces:
|
||||||
|
- loopback10
|
||||||
|
- loopback11
|
||||||
|
- loopback12
|
||||||
|
- loopback13
|
||||||
|
- loopback14
|
||||||
|
- loopback15
|
||||||
|
- loopback1000
|
||||||
|
provider: "{{ cli }}"
|
||||||
|
become: yes
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result.changed == true"
|
||||||
|
- "'interface loopback10' in result.commands"
|
||||||
|
- "'vrf forwarding test1' in result.commands"
|
||||||
|
- "'interface loopback11' in result.commands"
|
||||||
|
- "'vrf forwarding test1' in result.commands"
|
||||||
|
- "'interface loopback12' in result.commands"
|
||||||
|
- "'vrf forwarding test1' in result.commands"
|
||||||
|
- "'interface loopback13' in result.commands"
|
||||||
|
- "'vrf forwarding test1' in result.commands"
|
||||||
|
- "'interface loopback14' in result.commands"
|
||||||
|
- "'vrf forwarding test1' in result.commands"
|
||||||
|
- "'interface loopback15' in result.commands"
|
||||||
|
- "'vrf forwarding test1' in result.commands"
|
||||||
|
- "'interface loopback1000' in result.commands"
|
||||||
|
- "'vrf forwarding test1' in result.commands"
|
||||||
|
- "'ansible_1' in result.session_name"
|
||||||
|
|
||||||
|
- name: Add multiple interfaces to vrf (idempotent)
|
||||||
|
eos_vrf:
|
||||||
|
name: test1
|
||||||
|
rd: 1:202
|
||||||
|
state: present
|
||||||
|
authorize: yes
|
||||||
|
interfaces:
|
||||||
|
- loopback10
|
||||||
|
- loopback11
|
||||||
|
- loopback12
|
||||||
|
- loopback13
|
||||||
|
- loopback14
|
||||||
|
- loopback15
|
||||||
|
- loopback1000
|
||||||
|
provider: "{{ cli }}"
|
||||||
|
become: yes
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "result.changed == false"
|
||||||
|
- "result.commands | length == 0"
|
||||||
|
|
||||||
- name: Create aggregate of VRFs
|
- name: Create aggregate of VRFs
|
||||||
eos_vrf:
|
eos_vrf:
|
||||||
aggregate:
|
aggregate:
|
||||||
|
@ -216,6 +267,7 @@
|
||||||
- name: Delete aggregate of VRFs
|
- name: Delete aggregate of VRFs
|
||||||
eos_vrf:
|
eos_vrf:
|
||||||
aggregate:
|
aggregate:
|
||||||
|
- { name: test1 }
|
||||||
- { name: test2 }
|
- { name: test2 }
|
||||||
- { name: test3 }
|
- { name: test3 }
|
||||||
- { name: test4 }
|
- { name: test4 }
|
||||||
|
@ -228,6 +280,7 @@
|
||||||
- name: Delete VRFs again (idempotent)
|
- name: Delete VRFs again (idempotent)
|
||||||
eos_vrf:
|
eos_vrf:
|
||||||
aggregate:
|
aggregate:
|
||||||
|
- { name: test1 }
|
||||||
- { name: test2 }
|
- { name: test2 }
|
||||||
- { name: test3 }
|
- { name: test3 }
|
||||||
- { name: test4 }
|
- { name: test4 }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue