From ef7f3fcab69f8a9eb36cc044195eb36af578468f Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Fri, 2 Mar 2018 08:50:27 +0530 Subject: [PATCH] eos_static_route fix (#36903) Signed-off-by: Trishna Guha --- .../modules/network/eos/eos_static_route.py | 57 ++++++++++++++----- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/lib/ansible/modules/network/eos/eos_static_route.py b/lib/ansible/modules/network/eos/eos_static_route.py index fb7da6bffeb..013a6804db4 100644 --- a/lib/ansible/modules/network/eos/eos_static_route.py +++ b/lib/ansible/modules/network/eos/eos_static_route.py @@ -80,15 +80,33 @@ commands: - ip route 10.0.2.0/24 10.8.38.1 3 - no ip route 10.0.2.0/24 10.8.38.1 """ + +import re + from copy import deepcopy from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.network.common.utils import remove_default_spec -from ansible.module_utils.network.common.utils import validate_ip_address, validate_prefix -from ansible.module_utils.network.eos.eos import load_config, run_commands +from ansible.module_utils.network.common.utils import is_masklen, validate_ip_address +from ansible.module_utils.network.common.utils import remove_default_spec, validate_prefix +from ansible.module_utils.network.eos.eos import get_config, load_config from ansible.module_utils.network.eos.eos import eos_argument_spec, check_args +def is_address(value): + if value: + address = value.split('/') + if is_masklen(address[1]) and validate_ip_address(address[0]): + return True + return False + + +def is_hop(value): + if value: + if validate_ip_address(value): + return True + return False + + def map_obj_to_commands(updates, module): commands = list() want, have = updates @@ -137,20 +155,31 @@ def map_config_to_obj(module): objs = [] try: - out = run_commands(module, ['show ip route | json'])[0] + out = get_config(module, flags=['| include ip.route']) except IndexError: - out = {} + out = '' if out: - try: - vrfs = out['vrfs']['default']['routes'] - except (AttributeError, KeyError, TypeError): - vrfs = {} - if vrfs: - for address in vrfs: + lines = out.splitlines() + for line in lines: obj = {} - obj['address'] = address - obj['admin_distance'] = vrfs[address].get('preference') - obj['next_hop'] = vrfs[address].get('vias')[0].get('nexthopAddr') + add_match = re.search(r'ip route (\S+)', line, re.M) + if add_match: + address = add_match.group(1) + if is_address(address): + obj['address'] = address + + hop_match = re.search(r'ip route {0} (\S+)'.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 objs.append(obj) return objs