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
Ejemplo n.º 2
0
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 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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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 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
Ejemplo n.º 7
0
def get_existing(module):
    existing = {}
    config = str(get_config(module))

    value = get_value(config, module)
    if value:
        existing['ospf'] = value
    return existing
Ejemplo n.º 8
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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
Ejemplo n.º 14
0
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):
    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
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
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)
    }
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
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
Ejemplo n.º 21
0
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
Ejemplo n.º 22
0
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 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=[])

        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

            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
Ejemplo n.º 25
0
def map_config_to_obj(module):
    obj = []

    data = get_config(module, flags=[' all | section logging'])

    for line in data.split('\n'):
        if re.search(r'no (\S+)', line, re.M):
            state = 'absent'
        else:
            state = 'present'

        match = re.search(r'logging (\S+)', line, re.M)
        if state == 'present' and match:
            event_status = None
            name = None
            dest_level = None
            dest = None
            facility = None
            remote_server = None
            facility_link_status = None
            file_size = None
            facility_level = None

            if match.group(1) in DEST_GROUP:
                dest = match.group(1)

                name = parse_name(line, dest)
                remote_server = parse_remote_server(line, dest)
                dest_level = parse_dest_level(line, dest, name)

                if dest == 'server':
                    facility = parse_facility(line)

                facility_level = parse_facility_level(line, facility, dest)

                if dest == 'logfile':
                    file_size = parse_file_size(line, name, dest_level)

            elif match.group(1) == 'level':
                match_facility = re.search(r'logging level (\S+)', line, re.M)
                facility = match_facility.group(1)

                level = parse_facility_level(line, facility, dest)
                if level.isdigit():
                    facility_level = level
                else:
                    facility_link_status = parse_facility_link_status(
                        line, facility, level)

            elif match.group(1) == 'event' and state == 'present':
                event = parse_event(line)
                if event:
                    status = parse_event_status(line, event)
                    if status:
                        event_status = event + '-' + status
                else:
                    continue

            else:
                pass

            obj.append({
                'dest': dest,
                'remote_server': remote_server,
                'use_vrf': parse_use_vrf(line, dest),
                'name': name,
                'facility': facility,
                'dest_level': dest_level,
                'facility_level': facility_level,
                'interface': parse_interface(line),
                'facility_link_status': facility_link_status,
                'event': event_status,
                'file_size': file_size,
                'message': parse_message(line),
                'timestamp': parse_timestamp(line)
            })

    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,
                'use_vrf': None,
                'interface': None,
                'facility_link_status': None,
                'event': None,
                'file_size': None,
                'message': None,
                'timestamp': None
            })

    return obj
Ejemplo n.º 26
0
def main():
    """ main entry point for module execution
    """
    backup_spec = dict(filename=dict(), dir_path=dict(type='path'))
    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),
        backup_options=dict(type='dict', options=backup_spec),
        save_when=dict(choices=['always', 'never', 'modified', 'changed'],
                       default='never'),
        diff_against=dict(choices=['running', 'startup', 'intended']),
        diff_ignore_lines=dict(type='list'),
    )

    argument_spec.update(nxos_argument_spec)

    mutually_exclusive = [('lines', 'src', 'replace_src'), ('parents', 'src')]

    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()

    result = {'changed': False, 'warnings': warnings}

    config = None

    diff_ignore_lines = module.params['diff_ignore_lines']
    path = module.params['parents']
    connection = get_connection(module)
    contents = None
    flags = ['all'] if module.params['defaults'] else []
    replace_src = module.params['replace_src']
    if 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, flags=flags)
        config = NetworkConfig(indent=2, contents=contents)
        if module.params['backup']:
            result['__backup__'] = contents

    if any((module.params['src'], module.params['lines'], replace_src)):
        match = module.params['match']
        replace = module.params['replace']

        commit = not module.check_mode
        candidate = get_candidate(module)
        running = get_running_config(module, contents, flags=flags)
        if replace_src:
            commands = candidate.split('\n')
            result['commands'] = result['updates'] = commands
            if commit:
                load_config(module, commands, replace=replace_src)

            result['changed'] = True
        else:
            try:
                response = connection.get_diff(
                    candidate=candidate,
                    running=running,
                    diff_match=match,
                    diff_ignore_lines=diff_ignore_lines,
                    path=path,
                    diff_replace=replace)
            except ConnectionError as exc:
                module.fail_json(
                    msg=to_text(exc, errors='surrogate_then_replace'))

            config_diff = response['config_diff']
            if config_diff:
                commands = config_diff.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 commit:
                    load_config(module, commands, replace=replace_src)

                result['changed'] = True

    running_config = module.params['running_config']
    startup_config = None

    if module.params['save_when'] == 'always':
        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=2,
                                       contents=output[0],
                                       ignore_lines=diff_ignore_lines)
        startup_config = NetworkConfig(indent=2,
                                       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=2,
                                       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 = startup_config.config_text

        elif module.params['diff_against'] == 'intended':
            contents = module.params['intended_config']

        if contents is not None:
            base_config = NetworkConfig(indent=2,
                                        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)
Ejemplo n.º 27
0
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 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'),
    )

    argument_spec.update(nxos_argument_spec)

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)

    warnings = list()

    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)
Ejemplo n.º 29
0
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_config(self):
     global g_config
     if not g_config:
         g_config = get_config(self.module)
     return g_config