Example #1
0
def map_config_to_obj(module):
    data = get_config(module, flags=['username'])
    users = data.strip().rstrip('!').split('!')

    if not users:
        return list()

    instances = list()

    for user in users:
        user_config = user.strip().splitlines()

        name = user_config[0].strip().split()[1]
        group = None

        if len(user_config) > 1:
            group_or_secret = user_config[1].strip().split()
            if group_or_secret[0] == 'group':
                group = group_or_secret[1]

        obj = {
            'name': name,
            'state': 'present',
            'configured_password': None,
            'group': group
        }
        instances.append(obj)

    return instances
Example #2
0
def map_config_to_obj(module):
    data = get_config(module, flags=['interface'])
    interfaces = data.strip().rstrip('!').split('!')

    if not interfaces:
        return list()

    instances = list()

    for interface in interfaces:
        intf_config = interface.strip().splitlines()

        name = intf_config[0].strip().split()[1]

        if name == 'preconfigure':
            name = intf_config[0].strip().split()[2]

        obj = {
            'name': name,
            'description': parse_config_argument(intf_config, 'description'),
            'speed': parse_config_argument(intf_config, 'speed'),
            'duplex': parse_config_argument(intf_config, 'duplex'),
            'mtu': parse_config_argument(intf_config, 'mtu'),
            'disable': True if parse_shutdown(intf_config) else False,
            'state': 'present'
        }
        instances.append(obj)
    return instances
Example #3
0
def map_config_to_obj(module):
    config = get_config(module)
    return {
        'hostname': parse_hostname(config),
        'domain_name': parse_domain_name(config),
        'domain_search': re.findall('^domain list (\S+)', config, re.M),
        'lookup_source': parse_lookup_source(config),
        'lookup_enabled': 'domain lookup disable' not in config,
        'name_servers': re.findall('^domain name-server (\S+)', config, re.M)
    }
Example #4
0
def main():
    """main entry point for module execution
    """
    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']),

        # this argument is deprecated in favor of setting match: none
        # it will be removed in a future version
        force=dict(default=False, type='bool'),

        config=dict(),
        backup=dict(type='bool', default=False),
        comment=dict(default=DEFAULT_COMMIT_COMMENT),
        admin=dict(type='bool', default=False)
    )

    argument_spec.update(iosxr_argument_spec)

    mutually_exclusive = [('lines', '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)

    if module.params['force'] is True:
        module.params['match'] = 'none'

    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)
Example #5
0
def main():
    """main entry point for module execution
    """
    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']),

        # this argument is deprecated in favor of setting match: none
        # it will be removed in a future version
        force=dict(default=False, type='bool'),

        config=dict(),
        backup=dict(type='bool', default=False),
        comment=dict(default=DEFAULT_COMMIT_COMMENT),
    )

    argument_spec.update(iosxr_argument_spec)

    mutually_exclusive = [('lines', '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)

    if module.params['force'] is True:
        module.params['match'] = 'none'

    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)
Example #6
0
def map_config_to_obj(module):
    obj = {'state': 'absent'}

    netconf_config = get_config(module, flags=['netconf-yang agent'])

    ssh_config = get_config(module, flags=['ssh server'])
    ssh_config = [
        config_line
        for config_line in (line.strip() for line in ssh_config.splitlines())
        if config_line
    ]
    obj['netconf_vrf'] = []
    for config in ssh_config:
        if 'netconf port' in config:
            obj.update({'netconf_port': parse_port(config)})
        if 'netconf vrf' in config:
            obj['netconf_vrf'].append(parse_vrf(config))
    if 'ssh' in netconf_config or 'netconf_port' in obj or obj['netconf_vrf']:
        obj.update({'state': 'present'})

    if 'ssh' in netconf_config and 'netconf_port' not in obj:
        obj.update({'netconf_port': 830})

    return obj
Example #7
0
def map_config_to_obj(module):
    flags = 'banner %s' % module.params['banner']
    output = get_config(module, flags=[flags])

    match = re.search(r'banner (\S+) (.*)', output, re.DOTALL)
    if match:
        text = match.group(2).strip("'")
    else:
        text = None

    obj = {'banner': module.params['banner'], 'state': 'absent'}

    if output:
        obj['text'] = text
        obj['state'] = 'present'

    return obj
Example #8
0
def map_config_to_obj(module):
    flags = 'banner %s' % module.params['banner']
    output = get_config(module, flags=[flags])

    match = re.search(r'banner (\S+) (.*)', output, re.DOTALL)
    if match:
        text = match.group(2).strip("'")
    else:
        text = None

    obj = {'banner': module.params['banner'], 'state': 'absent'}

    if output:
        obj['text'] = text
        obj['state'] = 'present'

    return obj
Example #9
0
def map_config_to_obj(module):

    obj = []
    dest_group = ('console', 'hostnameprefix', 'monitor', 'buffered', 'on')

    data = get_config(module, flags=['logging'])
    lines = data.split("\n")

    for line in lines:
        match = re.search(r'logging (\S+)', line, re.M)
        if match:
            if match.group(1) in dest_group:
                dest = match.group(1)
                obj.append({
                    'dest': dest,
                    'name': parse_name(line, dest),
                    'size': parse_size(line, dest),
                    'facility': parse_facility(line),
                    'level': parse_level(line, dest)
                })

    return obj
Example #10
0
def map_config_to_obj(module):
    data = get_config(module, flags=['username'])

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

    instances = list()

    for user in set(match):
        regex = r'username %s .+$' % user
        cfg = re.findall(regex, data, re.M)
        cfg = '\n'.join(cfg)
        obj = {
            'name': user,
            'state': 'present',
            'password': None,
            'group': parse_group(cfg)
        }
        instances.append(obj)

    return instances
Example #11
0
def map_config_to_obj(module):

    obj = []
    dest_group = ('console', 'hostnameprefix', 'monitor', 'buffered', 'on')

    data = get_config(module, flags=['logging'])
    lines = data.split("\n")

    for line in lines:
        match = re.search(r'logging (\S+)', line, re.M)
        if match:
            if match.group(1) in dest_group:
                dest = match.group(1)
                obj.append({
                    'dest': dest,
                    'name': parse_name(line, dest),
                    'size': parse_size(line, dest),
                    'facility': parse_facility(line),
                    'level': parse_level(line, dest)
                })

    return obj
Example #12
0
def get_running_config(module):
    contents = module.params['config']
    if not contents:
        contents = get_config(module)
    return NetworkConfig(indent=1, contents=contents)
Example #13
0
def get_running_config(module):
    contents = module.params['config']
    if not contents:
        contents = get_config(module)
    return NetworkConfig(indent=1, contents=contents)