def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) asn_regex = re.compile(r".*router\sbgp\s(?P<existing_asn>\d+(\.\d+)?).*", re.S) match_asn = asn_regex.match(str(netcfg)) if match_asn: existing_asn = match_asn.group("existing_asn") parents = ["router bgp {0}".format(existing_asn)] if module.params["vrf"] != "default": parents.append("vrf {0}".format(module.params["vrf"])) parents.append("neighbor {0}".format(module.params["neighbor"])) parents.append("address-family {0} {1}".format(module.params["afi"], module.params["safi"])) config = netcfg.get_section(parents) if config: for arg in args: if arg not in ["asn", "vrf", "neighbor", "afi", "safi"]: existing[arg] = get_value(arg, config, module) existing["asn"] = existing_asn existing["neighbor"] = module.params["neighbor"] existing["vrf"] = module.params["vrf"] existing["afi"] = module.params["afi"] existing["safi"] = module.params["safi"] else: warnings.append( "The BGP process didn't exist but the task just created it.") return existing
def get_existing(module, args): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) interface_exist = check_interface(module, netcfg) if interface_exist: parents = ["interface {0}".format(interface_exist)] temp_config = netcfg.get_section(parents) if ("member vni {0} associate-vrf".format(module.params["vni"]) in temp_config): parents.append("member vni {0} associate-vrf".format( module.params["vni"])) config = netcfg.get_section(parents) elif "member vni {0}".format(module.params["vni"]) in temp_config: parents.append("member vni {0}".format(module.params["vni"])) config = netcfg.get_section(parents) else: config = {} if config: for arg in args: if arg not in ["interface", "vni"]: existing[arg] = get_value(arg, config, module) existing["interface"] = interface_exist existing["vni"] = module.params["vni"] return existing, interface_exist
def get_existing(module, args, gl): existing = {} config = str(get_config(module)) address = module.params["rp_address"] pim_address_re = r"ip pim rp-address (?P<value>.*)$" for line in re.findall(pim_address_re, config, re.M): values = line.split() if values[0] != address: continue if gl and "group-list" not in line: continue elif not gl and "group-list" in line: if "224.0.0.0/4" not in line: # ignore default group-list continue existing["bidir"] = existing.get("bidir") or "bidir" in line if len(values) > 2: value = values[2] if values[1] == "route-map": existing["route_map"] = value elif values[1] == "prefix-list": existing["prefix_list"] = value elif values[1] == "group-list": if value != "224.0.0.0/4": # ignore default group-list existing["group_list"] = value return existing
def has_lldp(module): output = get_config(module, ["| section lldp"]) is_lldp_enable = False if output and "feature lldp" in output: is_lldp_enable = True return is_lldp_enable
def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module, flags=['bgp all'])) asn_re = re.compile(r'.*router\sbgp\s(?P<existing_asn>\d+(\.\d+)?).*', re.S) asn_match = asn_re.match(str(netcfg)) if asn_match: existing_asn = asn_match.group('existing_asn') bgp_parent = 'router bgp {0}'.format(existing_asn) if module.params['vrf'] != 'default': parents = [bgp_parent, 'vrf {0}'.format(module.params['vrf'])] else: parents = [bgp_parent] config = netcfg.get_section(parents) if config: for arg in args: if arg != 'asn' and (module.params['vrf'] == 'default' or arg not in GLOBAL_PARAMS): existing[arg] = get_value(arg, config) existing['asn'] = existing_asn if module.params['vrf'] == 'default': existing['vrf'] = 'default' if not existing and module.params['vrf'] != 'default' and module.params['state'] == 'present': msg = ("VRF {0} doesn't exist.".format(module.params['vrf'])) warnings.append(msg) return existing
def get_pim_interface(module, interface): pim_interface = {} body = get_config(module, flags=['interface {0}'.format(interface)]) pim_interface['bfd'] = 'default' pim_interface['neighbor_type'] = None pim_interface['neighbor_policy'] = None pim_interface['jp_policy_in'] = None pim_interface['jp_policy_out'] = None pim_interface['jp_type_in'] = None pim_interface['jp_type_out'] = None pim_interface['jp_bidir'] = False pim_interface['isauth'] = False if body: all_lines = body.splitlines() for each in all_lines: if 'jp-policy' in each: policy_name = \ re.search(r'ip pim jp-policy(?: prefix-list)? (\S+)(?: \S+)?', each).group(1) if 'prefix-list' in each: ptype = 'prefix' else: ptype = 'routemap' if 'out' in each: pim_interface['jp_policy_out'] = policy_name pim_interface['jp_type_out'] = ptype elif 'in' in each: pim_interface['jp_policy_in'] = policy_name pim_interface['jp_type_in'] = ptype else: pim_interface['jp_policy_in'] = policy_name pim_interface['jp_policy_out'] = policy_name pim_interface['jp_bidir'] = True elif 'neighbor-policy' in each: pim_interface['neighbor_policy'] = \ re.search(r'ip pim neighbor-policy(?: prefix-list)? (\S+)', each).group(1) if 'prefix-list' in each: pim_interface['neighbor_type'] = 'prefix' else: pim_interface['neighbor_type'] = 'routemap' elif 'ah-md5' in each: pim_interface['isauth'] = True elif 'sparse-mode' in each: pim_interface['sparse'] = True elif 'bfd-instance' in each: m = re.search(r'ip pim bfd-instance(?P<disable> disable)?', each) if m: pim_interface['bfd'] = 'disable' if m.group('disable') else 'enable' elif 'border' in each: pim_interface['border'] = True elif 'hello-interval' in each: pim_interface['hello_interval'] = \ re.search(r'ip pim hello-interval (\d+)', body).group(1) elif 'dr-priority' in each: pim_interface['dr_prio'] = \ re.search(r'ip pim dr-priority (\d+)', body).group(1) return pim_interface
def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) asn_regex = re.compile(r'.*router\sbgp\s(?P<existing_asn>\d+(\.\d+)?).*', re.S) match_asn = asn_regex.match(str(netcfg)) if match_asn: existing_asn = match_asn.group('existing_asn') parents = ["router bgp {0}".format(existing_asn)] if module.params['vrf'] != 'default': parents.append('vrf {0}'.format(module.params['vrf'])) parents.append('neighbor {0}'.format(module.params['neighbor'])) config = netcfg.get_section(parents) if config: for arg in args: if arg not in ['asn', 'vrf', 'neighbor']: existing[arg] = get_value(arg, config) existing['asn'] = existing_asn existing['neighbor'] = module.params['neighbor'] existing['vrf'] = module.params['vrf'] else: warnings.append("The BGP process didn't exist but the task" " just created it.") return existing
def has_vrf(module, vrf): global _CONFIGURED_VRFS if _CONFIGURED_VRFS is not None: return vrf in _CONFIGURED_VRFS config = get_config(module) _CONFIGURED_VRFS = re.findall(r"vrf context (\S+)", config) return vrf in _CONFIGURED_VRFS
def get_existing(module, args, gl): existing = {} config = str(get_config(module)) address = module.params['rp_address'] pim_address_re = r'ip pim rp-address (?P<value>.*)$' for line in re.findall(pim_address_re, config, re.M): values = line.split() if values[0] != address: continue if gl and 'group-list' not in line: continue elif not gl and 'group-list' in line: if '224.0.0.0/4' not in line: # ignore default group-list continue existing['bidir'] = existing.get('bidir') or 'bidir' in line if len(values) > 2: value = values[2] if values[1] == 'route-map': existing['route_map'] = value elif values[1] == 'prefix-list': existing['prefix_list'] = value elif values[1] == 'group-list': if value != '224.0.0.0/4': # ignore default group-list existing['group_list'] = value return existing
def reconcile_candidate(module, candidate, prefix, want): state, vrf = want['state'], want['vrf'] if vrf == 'default': parents = [] flags = " | include '^ip route'" else: parents = ['vrf context {0}'.format(vrf)] flags = " | section '{0}' | include '^ ip route'".format(parents[0]) # Find existing routes in this vrf/default netcfg = CustomNetworkConfig(indent=2, contents=get_config(module, flags=[flags])) routes = str(netcfg).split('\n') # strip whitespace from route strings routes = [i.strip() for i in routes] prefix_and_nh = 'ip route {0} {1}'.format(prefix, want['next_hop']) existing = [i for i in routes if i.startswith(prefix_and_nh)] proposed = set_route_command(prefix, want, module) commands = [] if state == 'absent' and existing: commands = ['no ' + existing[0]] elif state == 'present' and proposed not in routes: if existing: commands = ['no ' + existing[0]] commands.append(proposed) if commands: candidate.add(commands, parents=parents)
def get_existing(module, args): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) parents = ['router ospf {0}'.format(module.params['ospf'])] if module.params['vrf'] != 'default': parents.append('vrf {0}'.format(module.params['vrf'])) config = netcfg.get_section(parents) for arg in args: if arg not in ['ospf', 'vrf']: existing[arg] = PARAM_TO_DEFAULT_KEYMAP.get(arg) if config: if module.params['vrf'] == 'default': splitted_config = config.splitlines() vrf_index = False for index in range(0, len(splitted_config) - 1): if 'vrf' in splitted_config[index].strip(): vrf_index = index break if vrf_index: config = '\n'.join(splitted_config[0:vrf_index]) splitted_config = config.splitlines() for line in splitted_config: if 'passive' in line: existing['passive_interface'] = True elif 'router-id' in line: existing['router_id'] = re.search(r'router-id (\S+)', line).group(1) elif 'metric' in line: existing['default_metric'] = re.search(r'default-metric (\S+)', line).group(1) elif 'adjacency' in line: log = re.search(r'log-adjacency-changes(?: (\S+))?', line).group(1) if log: existing['log_adjacency'] = log else: existing['log_adjacency'] = 'log' elif 'auto' in line: cost = re.search(r'auto-cost reference-bandwidth (\d+) (\S+)', line).group(1) if 'Gbps' in line: cost = int(cost) * 1000 existing['auto_cost'] = str(cost) elif 'bfd' in line: existing['bfd'] = 'enable' elif 'timers throttle lsa' in line: tmp = re.search(r'timers throttle lsa (\S+) (\S+) (\S+)', line) existing['timer_throttle_lsa_start'] = tmp.group(1) existing['timer_throttle_lsa_hold'] = tmp.group(2) existing['timer_throttle_lsa_max'] = tmp.group(3) elif 'timers throttle spf' in line: tmp = re.search(r'timers throttle spf (\S+) (\S+) (\S+)', line) existing['timer_throttle_spf_start'] = tmp.group(1) existing['timer_throttle_spf_hold'] = tmp.group(2) existing['timer_throttle_spf_max'] = tmp.group(3) existing['vrf'] = module.params['vrf'] existing['ospf'] = module.params['ospf'] return existing
def get_existing(module): existing = {} config = str(get_config(module)) value = get_value(config, module) if value: existing["ospf"] = value return existing
def get_running_config(module, config=None, flags=None): contents = module.params["running_config"] if not contents: if config: contents = config else: contents = get_config(module, flags=flags) return contents
def peer_link_exists(module): found = False run = get_config(module, flags=["vpc"]) vpc_list = run.split("\n") for each in vpc_list: if "peer-link" in each: found = True return found
def peer_link_exists(module): found = False run = get_config(module, flags=['vpc']) vpc_list = run.split('\n') for each in vpc_list: if 'peer-link' in each: found = True return found
def parse_min_links(module, group): min_links = None flags = ['| section interface.port-channel{0}'.format(group)] config = get_config(module, flags=flags) match = re.search(r'lacp min-links (\S+)', config, re.M) if match: min_links = match.group(1) return min_links
def get_vpc(module): body = run_commands(module, ["show vpc | json"])[0] if body: domain = str(body["vpc-domain-id"]) else: body = run_commands(module, ["show run vpc | inc domain"])[0] if body: domain = body.split()[2] else: domain = "not configured" vpc = {} if domain != "not configured": run = get_config(module, flags=["vpc all"]) if run: vpc["domain"] = domain for key in PARAM_TO_DEFAULT_KEYMAP.keys(): vpc[key] = PARAM_TO_DEFAULT_KEYMAP.get(key) vpc["auto_recovery"] = get_auto_recovery_default(module) vpc_list = run.split("\n") for each in vpc_list: if "role priority" in each: line = each.split() vpc["role_priority"] = line[-1] if "system-priority" in each: line = each.split() vpc["system_priority"] = line[-1] if re.search(r"delay restore \d+", each): line = each.split() vpc["delay_restore"] = line[-1] if "delay restore interface-vlan" in each: line = each.split() vpc["delay_restore_interface_vlan"] = line[-1] if "delay restore orphan-port" in each: line = each.split() vpc["delay_restore_orphan_port"] = line[-1] if "auto-recovery" in each: vpc["auto_recovery"] = False if "no " in each else True line = each.split() vpc["auto_recovery_reload_delay"] = line[-1] if "peer-gateway" in each: vpc["peer_gw"] = False if "no " in each else True if "peer-keepalive destination" in each: # destination is reqd; src & vrf are optional m = re.search( r"destination (?P<pkl_dest>[\d.]+)" r"(?:.* source (?P<pkl_src>[\d.]+))*" r"(?:.* vrf (?P<pkl_vrf>\S+))*", each, ) if m: for pkl in m.groupdict().keys(): if m.group(pkl): vpc[pkl] = m.group(pkl) return vpc
def parse_mode(module, m): mode = None flags = ['| section interface.{0}'.format(m)] config = get_config(module, flags=flags) match = re.search(r'channel-group [0-9]+ (force )?mode (\S+)', config, re.M) if match: mode = match.group(2) return mode
def get_vpc(module): body = run_commands(module, ['show vpc | json'])[0] if body: domain = str(body['vpc-domain-id']) else: body = run_commands(module, ['show run vpc | inc domain'])[0] if body: domain = body.split()[2] else: domain = 'not configured' vpc = {} if domain != 'not configured': run = get_config(module, flags=['vpc all']) if run: vpc['domain'] = domain for key in PARAM_TO_DEFAULT_KEYMAP.keys(): vpc[key] = PARAM_TO_DEFAULT_KEYMAP.get(key) vpc['auto_recovery'] = get_auto_recovery_default(module) vpc_list = run.split('\n') for each in vpc_list: if 'role priority' in each: line = each.split() vpc['role_priority'] = line[-1] if 'system-priority' in each: line = each.split() vpc['system_priority'] = line[-1] if re.search(r'delay restore \d+', each): line = each.split() vpc['delay_restore'] = line[-1] if 'delay restore interface-vlan' in each: line = each.split() vpc['delay_restore_interface_vlan'] = line[-1] if 'delay restore orphan-port' in each: line = each.split() vpc['delay_restore_orphan_port'] = line[-1] if 'auto-recovery' in each: vpc['auto_recovery'] = False if 'no ' in each else True line = each.split() vpc['auto_recovery_reload_delay'] = line[-1] if 'peer-gateway' in each: vpc['peer_gw'] = False if 'no ' in each else True if 'peer-keepalive destination' in each: # destination is reqd; src & vrf are optional m = re.search( r'destination (?P<pkl_dest>[\d.]+)' r'(?:.* source (?P<pkl_src>[\d.]+))*' r'(?:.* vrf (?P<pkl_vrf>\S+))*', each) if m: for pkl in m.groupdict().keys(): if m.group(pkl): vpc[pkl] = m.group(pkl) return vpc
def map_config_to_obj(module): objs = list() output = None command = ["show vlan brief | json"] output = run_commands(module, command, check_rc="retry_json")[0] if output: netcfg = CustomNetworkConfig(indent=2, contents=get_config(module, flags=["all"])) if isinstance(output, dict): vlans = None try: vlans = output["TABLE_vlanbriefxbrief"]["ROW_vlanbriefxbrief"] except KeyError: return objs if vlans: if isinstance(vlans, list): for vlan in vlans: obj = parse_vlan_options(module, netcfg, output, vlan) objs.append(obj) elif isinstance(vlans, dict): obj = parse_vlan_options(module, netcfg, output, vlans) objs.append(obj) else: vlans = list() splitted_line = re.split(r"\n(\d+)|\n{2}", output.strip()) for line in splitted_line: if not line: continue if len(line) > 0: line = line.strip() if line[0].isdigit(): match = re.search(r"^(\d+)$", line, re.M) if match: v = match.group(1) pos1 = splitted_line.index(v) pos2 = pos1 + 1 # fmt: off vlaninfo = "".join(splitted_line[pos1:pos2 + 1]) # fmt: on vlans.append(vlaninfo) if vlans: objs = parse_vlan_non_structured(module, netcfg, vlans) else: return objs return objs
def get_existing(module, args): existing = {} config = str(get_config(module)) for arg in args: command = PARAM_TO_COMMAND_KEYMAP[arg] has_command = re.findall(r"(?:{0}\s)(?P<value>.*)$".format(command), config, re.M) value = "" if has_command: value = has_command[0] existing[arg] = value return existing
def get_existing(module, args): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) if module.params['interface'].startswith('loopback') or module.params['interface'].startswith('port-channel'): parents = ['interface {0}'.format(module.params['interface'])] else: parents = ['interface {0}'.format(module.params['interface'].capitalize())] config = netcfg.get_section(parents) if 'ospf' in config: for arg in args: if arg not in ['interface']: existing[arg] = get_value(arg, config, module) existing['interface'] = module.params['interface'] return existing
def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) asn_regex = re.compile( r".*router\sbgp\s(?P<existing_asn>\d+(\.\d+)?).*", re.DOTALL ) match_asn = asn_regex.match(str(netcfg)) if match_asn: existing_asn = match_asn.group("existing_asn") parents = ["router bgp {0}".format(existing_asn)] if module.params["vrf"] != "default": parents.append("vrf {0}".format(module.params["vrf"])) parents.append( "address-family {0} {1}".format( module.params["afi"], module.params["safi"] ) ) config = netcfg.get_section(parents) if config: for arg in args: if arg not in ["asn", "afi", "safi", "vrf"]: gv = get_value(arg, config, module) if gv: existing[arg] = gv else: if ( arg != "client_to_client" and arg in PARAM_TO_DEFAULT_KEYMAP.keys() ): existing[arg] = PARAM_TO_DEFAULT_KEYMAP.get(arg) else: existing[arg] = gv existing["asn"] = existing_asn existing["afi"] = module.params["afi"] existing["safi"] = module.params["safi"] existing["vrf"] = module.params["vrf"] else: warnings.append( "The BGP process {0} didn't exist but the task just created it.".format( module.params["asn"] ) ) return existing
def get_existing(module): existing = [] netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) if module.params["mode"] == "maintenance": parents = ["configure maintenance profile maintenance-mode"] else: parents = ["configure maintenance profile normal-mode"] config = netcfg.get_section(parents) if config: existing = config.splitlines() existing = [cmd.strip() for cmd in existing] existing.pop(0) return existing
def get_available_features(feature, module): available_features = {} feature_regex = r"(?P<feature>\S+)\s+\d+\s+(?P<state>.*)" command = {"command": "show feature", "output": "text"} try: body = run_commands(module, [command])[0] split_body = body.splitlines() except (KeyError, IndexError): return {} for line in split_body: try: match_feature = re.match(feature_regex, line, re.DOTALL) feature_group = match_feature.groupdict() feature = feature_group["feature"] state = feature_group["state"] except AttributeError: feature = "" state = "" if feature and state: if "enabled" in state: state = "enabled" if feature not in available_features: available_features[feature] = state else: if ( available_features[feature] == "disabled" and state == "enabled" ): available_features[feature] = state # certain configurable features do not # show up in the output of "show feature" # but appear in running-config when set run_cfg = get_config(module, flags=["| include ^feature"]) for item in re.findall(r"feature\s(.*)", run_cfg): if item not in available_features: available_features[item] = "enabled" if "fabric forwarding" not in available_features: available_features["fabric forwarding"] = "disabled" return available_features
def get_existing(module, args): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) if module.params["interface"].startswith("loopback") or module.params[ "interface"].startswith("port-channel"): parents = ["interface {0}".format(module.params["interface"])] else: parents = [ "interface {0}".format(module.params["interface"].capitalize()) ] config = netcfg.get_section(parents) if "ospf" in config: for arg in args: if arg not in ["interface"]: existing[arg] = get_value(arg, config, module) existing["interface"] = module.params["interface"] return existing
def get_existing(module, args): existing = {} config = str(get_config(module)) for arg in args: if 'ssm_range' in arg: # <value> may be 'n.n.n.n/s', 'none', or 'default' m = re.search(r'ssm range (?P<value>(?:[\s\d.\/]+|none|default))?$', config, re.M) if m: # Remove rsvd SSM range value = m.group('value').replace('232.0.0.0/8', '') existing[arg] = value.split() elif 'bfd' in arg and 'ip pim bfd' in config: existing[arg] = 'enable' return existing
def map_config_to_obj(module): config = get_config(module) configobj = NetworkConfig(indent=2, contents=config) vrf_config = {} vrfs = re.findall(r'^vrf context (\S+)$', config, re.M) for vrf in vrfs: config_data = configobj.get_block_config(path=['vrf context %s' % vrf]) vrf_config[vrf] = config_data return { 'hostname': parse_hostname(config), 'domain_lookup': 'no ip domain-lookup' not in config, 'domain_name': parse_domain_name(config, vrf_config), 'domain_search': parse_domain_search(config, vrf_config), 'name_servers': parse_name_servers(config, vrf_config, vrfs), 'system_mtu': parse_system_mtu(config) }
def map_config_to_obj(module): config = get_config(module) configobj = NetworkConfig(indent=2, contents=config) vrf_config = {} vrfs = re.findall(r"^vrf context (\S+)$", config, re.M) for vrf in vrfs: config_data = configobj.get_block_config(path=["vrf context %s" % vrf]) vrf_config[vrf] = config_data return { "hostname": parse_hostname(config), "domain_lookup": "no ip domain-lookup" not in config, "domain_name": parse_domain_name(config, vrf_config), "domain_search": parse_domain_search(config, vrf_config), "name_servers": parse_name_servers(config, vrf_config, vrfs), "system_mtu": parse_system_mtu(config), }
def get_existing(module, args, warnings): existing = {} netcfg = CustomNetworkConfig( indent=2, contents=get_config(module, flags=["bgp all"]) ) asn_re = re.compile( r".*router\sbgp\s(?P<existing_asn>\d+(\.\d+)?).*", re.S ) asn_match = asn_re.match(str(netcfg)) if asn_match: existing_asn = asn_match.group("existing_asn") bgp_parent = "router bgp {0}".format(existing_asn) if module.params["vrf"] != "default": parents = [bgp_parent, "vrf {0}".format(module.params["vrf"])] else: parents = [bgp_parent] config = netcfg.get_section(parents) if config: for arg in args: if arg != "asn" and ( module.params["vrf"] == "default" or arg not in GLOBAL_PARAMS ): existing[arg] = get_value(arg, config) existing["asn"] = existing_asn if module.params["vrf"] == "default": existing["vrf"] = "default" if ( not existing and module.params["vrf"] != "default" and module.params["state"] == "present" ): msg = "VRF {0} doesn't exist.".format(module.params["vrf"]) warnings.append(msg) return existing