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 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 map_config_to_obj(module): obj = [] data = get_config(module, flags=['| section logging']) for line in data.split('\n'): match = re.search(r'logging (\S+)', line, re.M) if match: if match.group(1) in DEST_GROUP: dest = match.group(1) facility = None elif match.group(1) == 'level': match_facility = re.search(r'logging level (\S+)', line, re.M) facility = match_facility.group(1) dest = None else: dest = None facility = None obj.append({'dest': dest, 'name': parse_name(line, dest), 'facility': facility, 'dest_level': parse_dest_level(line, dest, parse_name(line, dest)), 'facility_level': parse_facility_level(line, facility)}) return obj
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, 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, 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 main(): argument_spec = dict( vrf=dict(required=True), afi=dict(required=True, choices=['ipv4', 'ipv6']), route_target_both_auto_evpn=dict(required=False, type='bool'), state=dict(choices=['present', 'absent'], default='present'), safi=dict(choices=['unicast', 'multicast'], removed_in_version="2.4"), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False, 'warnings': warnings} config_text = get_config(module) config = NetworkConfig(indent=2, contents=config_text) path = ['vrf context %s' % module.params['vrf'], 'address-family %s unicast' % module.params['afi']] try: current = config.get_block_config(path) except ValueError: current = None commands = list() if current and module.params['state'] == 'absent': commands.append('no address-family %s unicast' % module.params['afi']) elif module.params['state'] == 'present': if current: have = 'route-target both auto evpn' in current if module.params['route_target_both_auto_evpn'] is not None: want = bool(module.params['route_target_both_auto_evpn']) if want and not have: commands.append('address-family %s unicast' % module.params['afi']) commands.append('route-target both auto evpn') elif have and not want: commands.append('address-family %s unicast' % module.params['afi']) commands.append('no route-target both auto evpn') else: commands.append('address-family %s unicast' % module.params['afi']) if module.params['route_target_both_auto_evpn']: commands.append('route-target both auto evpn') if commands: commands.insert(0, 'vrf context %s' % module.params['vrf']) if not module.check_mode: load_config(module, commands) result['changed'] = True result['commands'] = commands module.exit_json(**result)
def get_existing(module): existing = {} config = str(get_config(module)) value = get_value(config, module) if value: existing['ospf'] = value return existing
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 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 *= 1000 existing['auto_cost'] = str(cost) 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_pim_interface(module, interface): pim_interface = {} body = get_config(module, flags=['interface {0}'.format(interface)]) 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 '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_pim_interface(module, interface): pim_interface = {} body = get_config(module, flags=['interface {0}'.format(interface)]) 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 '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_running_config(module, config=None): contents = module.params['running_config'] if not contents: if not module.params['defaults'] and config: contents = config else: flags = ['all'] contents = get_config(module, flags=flags) return NetworkConfig(indent=2, contents=contents, ignore_lines=module.params['diff_ignore_lines'])
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: line = each.split() vpc['pkl_dest'] = line[2] vpc['pkl_vrf'] = 'management' if 'source' in each: vpc['pkl_src'] = line[4] if 'vrf' in each: vpc['pkl_vrf'] = line[6] else: if 'vrf' in each: vpc['pkl_vrf'] = line[4] 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 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): existing = {} config = str(get_config(module)) for arg in args: command = PARAM_TO_COMMAND_KEYMAP[arg] has_command = re.search(r'^{0}\s(?P<value>.*)$'.format(command), config, re.M) value = '' if has_command: value = has_command.group('value') 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 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 vlaninfo = ''.join(splitted_line[pos1:pos2 + 1]) vlans.append(vlaninfo) if vlans: objs = parse_vlan_non_structured(module, netcfg, vlans) else: return objs return objs
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 port-channel{0}'.format(module.params['group'])] config = netcfg.get_section(parents) if config: existing['min_links'] = get_value('min_links', config, module) existing.update(get_portchannel(module, netcfg=netcfg)) return existing, interface_exist
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 port-channel{0}'.format(module.params['group'])] config = netcfg.get_section(parents) if config: existing['min_links'] = get_value('min_links', config, module) existing.update(get_portchannel(module, netcfg=netcfg)) return existing, interface_exist
def get_existing(module, args): existing = {} config = str(get_config(module)) for arg in args: command = PARAM_TO_COMMAND_KEYMAP[arg] has_command = re.search(r'^{0}\s(?P<value>.*)$'.format(command), config, re.M) value = '' if has_command: value = has_command.group('value') if value == '232.0.0.0/8': value = '' # remove the reserved value existing[arg] = value.split() return existing
def get_existing(module, args): existing = {} config = str(get_config(module)) for arg in args: command = PARAM_TO_COMMAND_KEYMAP[arg] has_command = re.search(r'^{0}\s(?P<value>.*)$'.format(command), config, re.M) value = '' if has_command: value = has_command.group('value') if value == '232.0.0.0/8': value = '' # remove the reserved value existing[arg] = value.split() 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_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 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): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module, flags=['all'])) interface_string = 'interface {0}'.format(module.params['interface'].lower()) parents = [interface_string] config = netcfg.get_section(parents) if config: for arg in args: existing[arg] = get_value(arg, config, module) existing['interface'] = module.params['interface'].lower() else: if interface_string in str(netcfg): existing['interface'] = module.params['interface'].lower() for arg in args: existing[arg] = '' 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 reconcile_candidate(module, candidate, prefix, w): netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) state = w['state'] set_command = set_route_command(prefix, w) remove_command = remove_route_command(prefix, w) parents = [] commands = [] yrc = remove_command.replace('no ', '') if w['vrf'] == 'default': netcfg = str(netcfg).split('\n') ncfg = [] for line in netcfg: # remove ip route commands of non-default vrfs from # the running config just in case the same commands # exist in default and non-default vrfs if ' ip route' not in line: ncfg.append(line) if any(yrc in s for s in ncfg) and state == 'absent': commands = [remove_command] elif set_command not in ncfg and state == 'present': if any(yrc in s for s in ncfg): commands = [remove_command, set_command] else: commands = [set_command] else: parents = ['vrf context {0}'.format(w['vrf'])] config = netcfg.get_section(parents) if not isinstance(config, list): config = config.split('\n') config = [line.strip() for line in config] if any(yrc in s for s in config) and state == 'absent': commands = [remove_command] elif set_command not in config and state == 'present': if any(yrc in s for s in config): commands = [remove_command, set_command] else: commands = [set_command] if commands: candidate.add(commands, parents=parents)
def reconcile_candidate(module, candidate, prefix, w): netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) state = w['state'] set_command = set_route_command(prefix, w) remove_command = remove_route_command(prefix, w) parents = [] commands = [] yrc = remove_command.replace('no ', '') if w['vrf'] == 'default': netcfg = str(netcfg).split('\n') ncfg = [] for line in netcfg: # remove ip route commands of non-default vrfs from # the running config just in case the same commands # exist in default and non-default vrfs if ' ip route' not in line: ncfg.append(line) if any(yrc in s for s in ncfg) and state == 'absent': commands = [remove_command] elif set_command not in ncfg and state == 'present': if any(yrc in s for s in ncfg): commands = [remove_command, set_command] else: commands = [set_command] else: parents = ['vrf context {0}'.format(w['vrf'])] config = netcfg.get_section(parents) if not isinstance(config, list): config = config.split('\n') config = [line.strip() for line in config] if any(yrc in s for s in config) and state == 'absent': commands = [remove_command] elif set_command not in config and state == 'present': if any(yrc in s for s in config): commands = [remove_command, set_command] else: commands = [set_command] if commands: candidate.add(commands, parents=parents)
def get_vpc(module): body = run_commands(module, ['show vpc | json'])[0] domain = str(body['vpc-domain-id']) vpc = {} if domain != 'not configured': run = get_config(module, flags=['vpc']) 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 'delay restore' in each: line = each.split() vpc['delay_restore'] = line[-1] if 'no auto-recovery' in each: vpc['auto_recovery'] = False elif 'auto-recovery' in each: vpc['auto_recovery'] = True if 'peer-gateway' in each: vpc['peer_gw'] = True if 'peer-keepalive destination' in each: line = each.split() vpc['pkl_dest'] = line[2] vpc['pkl_vrf'] = 'management' if 'source' in each: vpc['pkl_src'] = line[4] if 'vrf' in each: vpc['pkl_vrf'] = line[6] else: if 'vrf' in each: vpc['pkl_vrf'] = line[4] return vpc
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 map_config_to_obj(module): obj = [] data = get_config(module, flags=['| section logging']) for line in data.split('\n'): match = re.search(r'logging (\S+)', line, re.M) if match: if match.group(1) in DEST_GROUP: dest = match.group(1) facility = None if dest == 'server': facility = parse_facility(line) elif match.group(1) == 'level': match_facility = re.search(r'logging level (\S+)', line, re.M) facility = match_facility.group(1) dest = None else: dest = None facility = None obj.append({ 'dest': dest, 'remote_server': parse_remote_server(line, dest), 'name': parse_name(line, dest), 'facility': facility, 'dest_level': parse_dest_level(line, dest, parse_name(line, dest)), 'facility_level': parse_facility_level(line, facility) }) return obj
def get_existing(module, args): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) parents = ['evpn', 'vni {0} l2'.format(module.params['vni'])] config = netcfg.get_section(parents) if config: for arg in args: if arg != 'vni': if arg == 'route_distinguisher': existing[arg] = get_value(arg, config, module) else: existing[arg] = get_route_target_value(arg, config, module) existing_fix = dict((k, v) for k, v in existing.items() if v) if not existing_fix: existing = existing_fix existing['vni'] = module.params['vni'] return existing
def get_existing(module, args): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) parents = ['evpn', 'vni {0} l2'.format(module.params['vni'])] config = netcfg.get_section(parents) if config: for arg in args: if arg != 'vni': if arg == 'route_distinguisher': existing[arg] = get_value(arg, config, module) else: existing[arg] = get_route_target_value(arg, config, module) existing_fix = dict((k, v) for k, v in existing.items() if v) if existing_fix: existing['vni'] = module.params['vni'] else: existing = existing_fix return existing
def get_portchannel_mode(interface, protocol, module, netcfg): if protocol != 'LACP': mode = 'on' else: netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) parents = ['interface {0}'.format(interface.capitalize())] body = netcfg.get_section(parents) mode_list = body.split('\n') for line in mode_list: this_line = line.strip() if this_line.startswith('channel-group'): find = this_line if 'mode' in find: if 'passive' in find: mode = 'passive' elif 'active' in find: mode = 'active' return mode
def get_existing(module, args): existing = {} netcfg = CustomNetworkConfig(indent=2, contents=get_config(module, flags=['all'])) interface_string = 'interface {0}'.format( module.params['interface'].lower()) parents = [interface_string] config = netcfg.get_section(parents) if config: for arg in args: existing[arg] = get_value(arg, config, module) existing['interface'] = module.params['interface'].lower() else: if interface_string in str(netcfg): existing['interface'] = module.params['interface'].lower() for arg in args: existing[arg] = '' return existing
def get_portchannel_mode(interface, protocol, module, netcfg): if protocol != 'LACP': mode = 'on' else: netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) parents = ['interface {0}'.format(interface.capitalize())] body = netcfg.get_section(parents) mode_list = body.split('\n') for line in mode_list: this_line = line.strip() if this_line.startswith('channel-group'): find = this_line if 'mode' in find: if 'passive' in find: mode = 'passive' elif 'active' in find: mode = 'active' return mode
def main(): argument_spec = dict( nv_overlay_evpn=dict(required=True, type='bool'), # deprecated in Ans2.3 include_defaults=dict(), config=dict(), save=dict()) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) result = {'changed': False} warnings = list() check_args(module, warnings) if warnings: result['warnings'] = warnings config = get_config(module) commands = list() if module.params['nv_overlay_evpn'] is True: if 'nv overlay evpn' not in config: commands.append('nv overlay evpn') elif 'nv overlay evpn' in config: commands.append('no nv overlay evpn') if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True result['commands'] = commands module.exit_json(**result)
def main(): argument_spec = dict( nv_overlay_evpn=dict(required=True, type='bool'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) result = {'changed': False} warnings = list() if warnings: result['warnings'] = warnings config = get_config(module) commands = list() info = get_capabilities(module).get('device_info', {}) os_platform = info.get('network_os_platform', '') if '3K' in os_platform: module.fail_json(msg='This module is not supported on Nexus 3000 series') if module.params['nv_overlay_evpn'] is True: if 'nv overlay evpn' not in config: commands.append('nv overlay evpn') elif 'nv overlay evpn' in config: commands.append('no nv overlay evpn') if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True result['commands'] = commands module.exit_json(**result)
def main(): argument_spec = dict(nv_overlay_evpn=dict(required=True, type='bool'), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) result = {'changed': False} warnings = list() if warnings: result['warnings'] = warnings config = get_config(module) commands = list() info = get_capabilities(module).get('device_info', {}) os_platform = info.get('network_os_platform', '') if '3K' in os_platform: module.fail_json( msg='This module is not supported on Nexus 3000 series') if module.params['nv_overlay_evpn'] is True: if 'nv overlay evpn' not in config: commands.append('nv overlay evpn') elif 'nv overlay evpn' in config: commands.append('no nv overlay evpn') if commands: if not module.check_mode: load_config(module, commands) result['changed'] = True result['commands'] = commands module.exit_json(**result)
def get_existing(module, args): 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 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': existing['group_list'] = value return existing
def map_config_to_obj(want, module): objs = list() netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) for w in want: parents = ['interface {0}'.format(w['name'])] config = netcfg.get_section(parents) obj = dict(name=None, ipv4=None, ipv6=None) if config: match_name = re.findall(r'interface (\S+)', config, re.M) if match_name: obj['name'] = match_name[0] match_ipv4 = re.findall(r'ip address (\S+)', config, re.M) if match_ipv4: obj['ipv4'] = match_ipv4[0] match_ipv6 = re.findall(r'ipv6 address (\S+)', config, re.M) if match_ipv6: obj['ipv6'] = match_ipv6[0] objs.append(obj) return objs
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 map_config_to_obj(want, module): objs = list() netcfg = CustomNetworkConfig(indent=2, contents=get_config(module)) for w in want: parents = ['interface {0}'.format(w['name'])] config = netcfg.get_section(parents) obj = dict(name=None, ipv4=None, ipv6=None) if config: match_name = re.findall(r'interface (\S+)', config, re.M) if match_name: obj['name'] = normalize_interface(match_name[0]) match_ipv4 = re.findall(r'ip address (\S+)', config, re.M) if match_ipv4: obj['ipv4'] = match_ipv4[0] match_ipv6 = re.findall(r'ipv6 address (\S+)', config, re.M) if match_ipv6: obj['ipv6'] = match_ipv6[0] objs.append(obj) return objs
def get_pim_interface(module, interface): pim_interface = {} command = 'show ip pim interface {0}'.format(interface) body = execute_show_command(command, module, text=True) if body: if 'not running' not in body[0]: body = execute_show_command(command, module) # Some nxos platforms have the TABLE_vrf/ROW_vrf key and some don't try: get_data = body[0]['TABLE_vrf']['ROW_vrf']['TABLE_iod']['ROW_iod'] except (KeyError, AttributeError, TypeError, IndexError): try: get_data = body[0]['TABLE_iod']['ROW_iod'] except (KeyError, AttributeError, TypeError, IndexError): return pim_interface if isinstance(get_data.get('dr-priority'), list): pim_interface['dr_prio'] = get_data.get('dr-priority')[0] else: pim_interface['dr_prio'] = str(get_data.get('dr-priority')) hello_interval = get_data.get('hello-interval-sec') if hello_interval: hello_interval_msec = int(get_data.get('hello-interval-sec')) * 1000 pim_interface['hello_interval'] = str(hello_interval_msec) border = get_data.get('is-border') border = border.lower() if border else border if border == 'true': pim_interface['border'] = True elif border == 'false': pim_interface['border'] = False isauth = get_data.get('isauth-config') isauth = isauth.lower() if isauth else isauth if isauth == 'true': pim_interface['isauth'] = True elif isauth == 'false': pim_interface['isauth'] = False pim_interface['neighbor_policy'] = get_data.get('nbr-policy-name') if pim_interface['neighbor_policy'] == 'none configured': pim_interface['neighbor_policy'] = None jp_in_policy = get_data.get('jp-in-policy-name') pim_interface['jp_policy_in'] = jp_in_policy if jp_in_policy == 'none configured': pim_interface['jp_policy_in'] = None pim_interface['jp_policy_out'] = get_jp_policy_out(module, interface) body = get_config(module, flags=['interface {0}'.format(interface)]) jp_configs = [] neigh = None if body: all_lines = body.splitlines() for each in all_lines: if 'jp-policy' in each: jp_configs.append(str(each.strip())) elif 'neighbor-policy' in each: neigh = str(each) elif 'sparse-mode' in each: pim_interface['sparse'] = True pim_interface['neighbor_type'] = None neigh_type = None if neigh: if 'prefix-list' in neigh: neigh_type = 'prefix' else: neigh_type = 'routemap' pim_interface['neighbor_type'] = neigh_type len_existing = len(jp_configs) list_of_prefix_type = len([x for x in jp_configs if 'prefix-list' in x]) jp_type_in = None jp_type_out = None jp_bidir = False if len_existing == 1: # determine type last_word = jp_configs[0].split(' ')[-1] if last_word == 'in': if list_of_prefix_type: jp_type_in = 'prefix' else: jp_type_in = 'routemap' elif last_word == 'out': if list_of_prefix_type: jp_type_out = 'prefix' else: jp_type_out = 'routemap' else: jp_bidir = True if list_of_prefix_type: jp_type_in = 'prefix' jp_type_out = 'routemap' else: jp_type_in = 'routemap' jp_type_out = 'routemap' else: for each in jp_configs: last_word = each.split(' ')[-1] if last_word == 'in': if 'prefix-list' in each: jp_type_in = 'prefix' else: jp_type_in = 'routemap' elif last_word == 'out': if 'prefix-list' in each: jp_type_out = 'prefix' else: jp_type_out = 'routemap' pim_interface['jp_type_in'] = jp_type_in pim_interface['jp_type_out'] = jp_type_out pim_interface['jp_bidir'] = jp_bidir return pim_interface
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(type='path'), replace_src=dict(), lines=dict(aliases=['commands'], type='list'), parents=dict(type='list'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact', 'none']), replace=dict(default='line', choices=['line', 'block', 'config']), running_config=dict(aliases=['config']), intended_config=dict(), defaults=dict(type='bool', default=False), backup=dict(type='bool', default=False), save_when=dict(choices=['always', 'never', 'modified', 'changed'], default='never'), diff_against=dict(choices=['running', 'startup', 'intended']), diff_ignore_lines=dict(type='list'), # save is deprecated as of ans2.4, use save_when instead save=dict(default=False, type='bool', removed_in_version='2.8'), # force argument deprecated in ans2.2 force=dict(default=False, type='bool', removed_in_version='2.6')) argument_spec.update(nxos_argument_spec) mutually_exclusive = [('lines', 'src', 'replace_src'), ('parents', 'src'), ('save', 'save_when')] required_if = [('match', 'strict', ['lines']), ('match', 'exact', ['lines']), ('replace', 'block', ['lines']), ('replace', 'config', ['replace_src']), ('diff_against', 'intended', ['intended_config'])] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, required_if=required_if, supports_check_mode=True) warnings = list() nxos_check_args(module, warnings) result = {'changed': False, 'warnings': warnings} config = None try: info = get_capabilities(module) api = info.get('network_api') device_info = info.get('device_info', {}) os_platform = device_info.get('network_os_platform', '') except ConnectionError: api = '' os_platform = '' if api == 'cliconf' and module.params['replace'] == 'config': if '9K' not in os_platform: module.fail_json( msg= 'replace: config is supported only on Nexus 9K series switches' ) if module.params['replace_src']: if module.params['replace'] != 'config': module.fail_json( msg='replace: config is required with replace_src') if module.params['backup'] or (module._diff and module.params['diff_against'] == 'running'): contents = get_config(module) config = NetworkConfig(indent=2, contents=contents) if module.params['backup']: result['__backup__'] = contents if any((module.params['src'], module.params['lines'], module.params['replace_src'])): match = module.params['match'] replace = module.params['replace'] candidate = get_candidate(module) if match != 'none' and replace != 'config': config = get_running_config(module, config) path = module.params['parents'] configobjs = candidate.difference(config, match=match, replace=replace, path=path) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands').split('\n') if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['commands'] = commands result['updates'] = commands if not module.check_mode: load_config(module, commands) result['changed'] = True running_config = module.params['running_config'] startup_config = None diff_ignore_lines = module.params['diff_ignore_lines'] if module.params['save_when'] == 'always' or module.params['save']: save_config(module, result) elif module.params['save_when'] == 'modified': output = execute_show_commands( module, ['show running-config', 'show startup-config']) running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines) startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines) if running_config.sha1 != startup_config.sha1: save_config(module, result) elif module.params['save_when'] == 'changed' and result['changed']: save_config(module, result) if module._diff: if not running_config: output = execute_show_commands(module, 'show running-config') contents = output[0] else: contents = running_config # recreate the object in order to process diff_ignore_lines running_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) if module.params['diff_against'] == 'running': if module.check_mode: module.warn( "unable to perform diff against running-config due to check mode" ) contents = None else: contents = config.config_text elif module.params['diff_against'] == 'startup': if not startup_config: output = execute_show_commands(module, 'show startup-config') contents = output[0] else: contents = output[0] contents = startup_config.config_text elif module.params['diff_against'] == 'intended': contents = module.params['intended_config'] if contents is not None: base_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) if running_config.sha1 != base_config.sha1: if module.params['diff_against'] == 'intended': before = running_config after = base_config elif module.params['diff_against'] in ('startup', 'running'): before = base_config after = running_config result.update({ 'changed': True, 'diff': { 'before': str(before), 'after': str(after) } }) module.exit_json(**result)
def populate(self): super(Config, self).populate() self.facts['config'] = get_config(self.module)
def map_config_to_obj(module): obj = [] data = get_config(module, flags=['| section logging']) for line in data.split('\n'): match = re.search(r'logging (\S+)', line, re.M) if match: if match.group(1) in DEST_GROUP: dest = match.group(1) facility = None if dest == 'server': facility = parse_facility(line) elif match.group(1) == 'level': match_facility = re.search(r'logging level (\S+)', line, re.M) facility = match_facility.group(1) dest = None else: dest = None facility = None obj.append({ 'dest': dest, 'remote_server': parse_remote_server(line, dest), 'name': parse_name(line, dest), 'facility': facility, 'dest_level': parse_dest_level(line, dest, parse_name(line, dest)), 'facility_level': parse_facility_level(line, facility) }) cmd = [{ 'command': 'show logging | section enabled | section console', 'output': 'text' }, { 'command': 'show logging | section enabled | section monitor', 'output': 'text' }] default_data = run_commands(module, cmd) for line in default_data: flag = False match = re.search( r'Logging (\w+):(?:\s+) (?:\w+) (?:\W)Severity: (\w+)', str(line), re.M) if match: if match.group(1) == 'console' and match.group(2) == 'critical': dest_level = '2' flag = True elif match.group(1) == 'monitor' and match.group( 2) == 'notifications': dest_level = '5' flag = True if flag: obj.append({ 'dest': match.group(1), 'remote_server': None, 'name': None, 'facility': None, 'dest_level': dest_level, 'facility_level': None }) return obj
def populate(self): super(Config, self).populate() self.facts['config'] = get_config(self.module)