From 6610d678f80fe2008fb798b7511349e65c16e24a Mon Sep 17 00:00:00 2001 From: Todd Bowman Date: Wed, 1 May 2019 08:55:09 -0600 Subject: [PATCH] Add vrf option to static routes (#55298) * added vrf to static route * fix pylint errors * version added 2.9 for vrf * removed undocmented return values * Update README.rst --- .../modules/network/eos/eos_static_route.py | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/network/eos/eos_static_route.py b/lib/ansible/modules/network/eos/eos_static_route.py index b717ac5bab3..f2372d04b62 100644 --- a/lib/ansible/modules/network/eos/eos_static_route.py +++ b/lib/ansible/modules/network/eos/eos_static_route.py @@ -33,6 +33,11 @@ options: description: - Next hop IP of the static route. required: true + vrf: + description: + - VRF for static route. + default: default + version_added: 2.9 admin_distance: description: - Admin distance of the static route. @@ -115,13 +120,20 @@ def map_obj_to_commands(updates, module): address = w['address'] next_hop = w['next_hop'] admin_distance = w['admin_distance'] + vrf = w['vrf'] state = w['state'] del w['state'] if state == 'absent' and w in have: - commands.append('no ip route %s %s' % (address, next_hop)) + if vrf == 'default': + commands.append('no ip route %s %s' % (address, next_hop)) + else: + commands.append('no ip route vrf %s %s %s' % (vrf, address, next_hop)) elif state == 'present' and w not in have: - commands.append('ip route %s %s %d' % (address, next_hop, admin_distance)) + if vrf == 'default': + commands.append('ip route %s %s %d' % (address, next_hop, admin_distance)) + else: + commands.append('ip route vrf %s %s %s %d' % (vrf, address, next_hop, admin_distance)) return commands @@ -145,7 +157,8 @@ def map_params_to_obj(module, required_together=None): 'address': module.params['address'].strip(), 'next_hop': module.params['next_hop'].strip(), 'admin_distance': module.params['admin_distance'], - 'state': module.params['state'] + 'state': module.params['state'], + 'vrf': module.params['vrf'] }) return obj @@ -164,22 +177,41 @@ def map_config_to_obj(module): obj = {} add_match = re.search(r'ip route ([\d\./]+)', line, re.M) if add_match: + obj['vrf'] = 'default' address = add_match.group(1) if is_address(address): obj['address'] = address - hop_match = re.search(r'ip route {0} ([\d\./]+)'.format(address), line, re.M) if hop_match: hop = hop_match.group(1) if is_hop(hop): obj['next_hop'] = hop - dist_match = re.search(r'ip route {0} {1} (\d+)'.format(address, hop), line, re.M) if dist_match: distance = dist_match.group(1) obj['admin_distance'] = int(distance) else: obj['admin_distance'] = 1 + + vrf_match = re.search(r'ip route vrf ([\w]+) ([\d\./]+)', line, re.M) + if vrf_match: + vrf = vrf_match.group(1) + obj['vrf'] = vrf + address = vrf_match.group(2) + if is_address(address): + obj['address'] = address + hop_vrf_match = re.search(r'ip route vrf {0} {1} ([\d\./]+)'.format(vrf, address), line, re.M) + if hop_vrf_match: + hop = hop_vrf_match.group(1) + if is_hop(hop): + obj['next_hop'] = hop + dist_vrf_match = re.search(r'ip route vrf {0} {1} {2} (\d+)'.format(vrf, address, hop), line, re.M) + if dist_vrf_match: + distance = dist_vrf_match.group(1) + obj['admin_distance'] = int(distance) + else: + obj['admin_distance'] = 1 + objs.append(obj) return objs @@ -191,6 +223,7 @@ def main(): element_spec = dict( address=dict(type='str', aliases=['prefix']), next_hop=dict(type='str'), + vrf=dict(type='str', default='default'), admin_distance=dict(default=1, type='int'), state=dict(default='present', choices=['present', 'absent']) )