def map_config_to_obj(module):
    config = get_config(module)
    configobj = NetworkConfig(indent=1, contents=config)

    match = re.findall(r'^interface (\S+)', config, re.M)
    if not match:
        return list()

    instances = list()

    for item in set(match):
        ipv4 = parse_config_argument(configobj, item, 'ip address')
        if ipv4:
            # eg. 192.168.2.10 255.255.255.0 -> 192.168.2.10/24
            address = ipv4.strip().split(' ')
            if len(address) == 2 and is_netmask(address[1]):
                ipv4 = '{0}/{1}'.format(address[0],
                                        to_text(to_masklen(address[1])))

        obj = {
            'name': item,
            'ipv4': ipv4,
            'ipv6': parse_config_argument(configobj, item, 'ipv6 address'),
            'state': 'present'
        }
        instances.append(obj)

    return instances
Esempio n. 2
0
def map_config_to_obj(module):
    config = get_config(module)
    configobj = NetworkConfig(indent=2, contents=config)

    return {
        'hostname': parse_hostname(config),
        'lookup_enabled': 'no ip domain-lookup' not in config,
        'domain_name': parse_domain_name(config),
        'domain_search': parse_domain_search(config),
        'name_servers': parse_name_servers(config),
    }
def map_config_to_obj(module):
    obj = []

    out = get_config(module, flags='| include ip route')
    for line in out.splitlines():
        # Split by whitespace but do not split quotes, needed for description
        splitted_line = findall(r'[^"\s]\S*|".+?"', line)
        route = {}
        prefix_with_mask = splitted_line[2]
        prefix = None
        mask = None
        iface = None
        nhop = None
        if validate_ip_address(prefix_with_mask) is True:
            my_net = ipaddress.ip_network(prefix_with_mask)
            prefix = str(my_net.network_address)
            mask = str(my_net.netmask)
            route.update({
                'prefix': prefix,
                'mask': mask,
                'admin_distance': '1'
            })
        if splitted_line[3] is not None:
            if validate_ip_address(splitted_line[3]) is False:
                iface = str(splitted_line[3])
                route.update(interface=iface)
                if validate_ip_address(splitted_line[4]) is True:
                    nhop = str(splitted_line[4])
                    route.update(next_hop=nhop)
                    if splitted_line[5].isdigit():
                        route.update(admin_distance=str(splitted_line[5]))
                elif splitted_line[4].isdigit():
                    route.update(admin_distance=str(splitted_line[4]))
                else:
                    if splitted_line[6] is not None and splitted_line[
                            6].isdigit():
                        route.update(admin_distance=str(splitted_line[6]))
            else:
                nhop = str(splitted_line[3])
                route.update(next_hop=nhop)
                if splitted_line[4].isdigit():
                    route.update(admin_distance=str(splitted_line[4]))

        index = 0
        for word in splitted_line:
            if word in ('tag', 'description'):
                route.update(word=splitted_line[index + 1])
            index = index + 1
        obj.append(route)

    return obj
def map_config_to_obj(module):
    objs = list()
    config = get_config(module)

    for line in config.split('\n'):
        l = line.strip()
        match = re.search(r'interface port-channel(\S+)', l, re.M)
        if match:
            obj = {}
            group = match.group(1)
            obj['group'] = group
            obj.update(get_channel(module, config, group))
            objs.append(obj)

    return objs
Esempio n. 5
0
def map_config_to_obj(module):
    obj = []
    dest_group = ('console', 'server', 'monitor', 'logfile')
    data = get_config(module, flags=['| include logging'])
    index = 0
    for line in data.split('\n'):
        logs = line.split()
        index = len(logs)
        if index == 0 or index == 1:
            continue
        if logs[0] != 'logging':
            continue
        if logs[1] == 'monitor' or logs[1] == 'console':
            obj.append({'dest': logs[1], 'level': logs[2]})
        elif logs[1] == 'logfile':
            level = '5'
            if index > 3 and logs[3].isdigit():
                level = logs[3]
            size = '10485760'
            if len(logs) > 4:
                size = logs[5]
            obj.append({
                'dest': logs[1],
                'name': logs[2],
                'size': size,
                'level': level
            })
        elif logs[1] == 'server':
            level = '5'
            facility = None

            if index > 3 and logs[3].isdigit():
                level = logs[3]
            if index > 3 and logs[3] == 'facility':
                facility = logs[4]
            if index > 4 and logs[4] == 'facility':
                facility = logs[5]
            obj.append({
                'dest': logs[1],
                'name': logs[2],
                'facility': facility,
                'level': level
            })
        else:
            continue
    return obj
Esempio n. 6
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'),
                         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']),
                         config=dict(),
                         backup=dict(type='bool', default=False),
                         backup_options=dict(type='dict', options=backup_spec),
                         comment=dict(default=DEFAULT_COMMIT_COMMENT),
                         admin=dict(type='bool', default=False))

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

    required_if = [('match', 'strict', ['lines']),
                   ('match', 'exact', ['lines']),
                   ('replace', 'block', ['lines']),
                   ('replace', 'config', ['src'])]

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

    warnings = list()
    check_args(module, warnings)

    result = dict(changed=False, warnings=warnings)

    if module.params['backup']:
        result['__backup__'] = get_config(module)

    run(module, result)

    module.exit_json(**result)
Esempio n. 7
0
def map_config_to_obj(module):
    config = get_config(module)
    configobj = NetworkConfig(indent=1, contents=config)

    match = re.findall(r'^interface (\S+)', config, re.M)
    if not match:
        return list()

    instances = list()

    for item in set(match):
        obj = {
            'name': item,
            'description': parse_config_argument(configobj, item, 'description'),
            'speed': parse_config_argument(configobj, item, 'speed'),
            'duplex': parse_config_argument(configobj, item, 'duplex'),
            'mtu': parse_config_argument(configobj, item, 'mtu'),
            'disable': True if parse_shutdown(configobj, item) else False,
            'state': 'present'
        }
        instances.append(obj)
    return instances
Esempio n. 8
0
def get_running_config(module):
    contents = module.params['config']
    if not contents:
        contents = get_config(module)
    return NetworkConfig(indent=1, contents=contents)