Пример #1
0
def main():
    """ main entry point for module execution
    """

    argument_spec = dict(
        src=dict(required=True),
        force=dict(default=False, type='bool'),
        include_defaults=dict(default=False, type='bool'),
        backup=dict(default=False, type='bool'),
        replace=dict(default=False, type='bool'),
        config=dict()
    )

    argument_spec.update(eapi.eapi_argument_spec)

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

    cls = get_ansible_module()

    module = cls(argument_spec=argument_spec,
                 mutually_exclusive=mutually_exclusive,
                 supports_check_mode=True)

    warnings = check_args(module)

    result = {'changed': False}
    if warnings:
        result['warnings'] = warnings

    src = module.params['src']
    candidate = NetworkConfig(contents=src, indent=3)

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

    if not module.params['force']:
        contents = get_current_config(module)
        configobj = NetworkConfig(contents=contents, indent=3)
        commands = candidate.difference(configobj)
        commands = dumps(commands, 'commands').split('\n')
        commands = [str(c).strip() for c in commands if c]
    else:
        commands = [c.strip() for c in str(candidate).split('\n')]

    # FIXME not implemented yet!!
    if replace:
        if module.params['transport'] == 'cli':
            module.fail_json(msg='config replace is only supported over eapi')
        commands = str(candidate).split('\n')

    if commands:
        commands = filter_exit(commands)
        commit = not module.check_mode
        load_config(commands, commit=commit)
        result['changed'] = True

    result['updates'] = commands

    module.exit_json(**result)
Пример #2
0
def get_config(module):
    contents = module.params['config']

    if not contents:
        contents = module.config.get_config()
        module.params['config'] = contents
        return NetworkConfig(indent=1, contents=contents[0])
    else:
        return NetworkConfig(indent=1, contents=contents)
Пример #3
0
def main():
    """ main entry point for module execution
    """

    argument_spec = dict(src=dict(required=True),
                         force=dict(default=False, type='bool'),
                         include_defaults=dict(default=False, type='bool'),
                         backup=dict(default=False, type='bool'),
                         replace=dict(default=False, type='bool'),
                         config=dict())

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

    module = NetworkModule(argument_spec=argument_spec,
                           mutually_exclusive=mutually_exclusive,
                           supports_check_mode=True)

    replace = module.params['replace']

    commands = list()
    running = None

    result = dict(changed=False)

    candidate = NetworkConfig(contents=module.params['src'], indent=3)

    if replace:
        if module.params['transport'] == 'cli':
            module.fail_json(msg='config replace is only supported over eapi')
        commands = str(candidate).split('\n')
    else:
        contents = get_config(module)
        if contents:
            running = NetworkConfig(contents=contents, indent=3)
            result['_backup'] = contents

        if not module.params['force']:
            commands = candidate.difference((running or list()))
            commands = dumps(commands, 'commands').split('\n')
            commands = [str(c) for c in commands if c]
        else:
            commands = str(candidate).split('\n')

    commands = filter_exit(commands)
    if commands:
        if not module.check_mode:
            response = module.config.load_config(commands,
                                                 replace=replace,
                                                 session='eos_template',
                                                 commit=True)

            module.cli('no configure session eos_template')
            result['responses'] = response
        result['changed'] = True

    result['updates'] = commands
    module.exit_json(**result)
Пример #4
0
def main():
    """ main entry point for module execution
    """

    argument_spec = dict(src=dict(required=True),
                         force=dict(default=False, type='bool'),
                         include_defaults=dict(default=False, type='bool'),
                         backup=dict(default=False, type='bool'),
                         replace=dict(default=False, type='bool'),
                         config=dict())

    argument_spec.update(eos_argument_spec)

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

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

    warnings = list()
    check_args(module, warnings)

    result = {'changed': False}
    if warnings:
        result['warnings'] = warnings

    src = module.params['src']
    candidate = NetworkConfig(contents=src, indent=3)

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

    if not module.params['force']:
        contents = get_current_config(module)
        configobj = NetworkConfig(contents=contents, indent=3)
        commands = candidate.difference(configobj)
        commands = dumps(commands, 'commands').split('\n')
        commands = [str(c).strip() for c in commands if c]
    else:
        commands = [c.strip() for c in str(candidate).split('\n')]

    #commands = str(candidate).split('\n')

    if commands:
        commands = filter_exit(commands)
        commit = not module.check_mode
        replace = module.params['replace'] or False
        load_config(module, commands, commit=commit, replace=replace)
        result['changed'] = True

    result['commands'] = commands
    result['updates'] = commands

    module.exit_json(**result)
Пример #5
0
def main():

    argument_spec = dict(lines=dict(aliases=['commands'],
                                    required=True,
                                    type='list'),
                         before=dict(type='list'),
                         after=dict(type='list'),
                         match=dict(default='line',
                                    choices=['line', 'strict', 'exact']),
                         replace=dict(default='line',
                                      choices=['line', 'block']),
                         force=dict(default=False, type='bool'),
                         config=dict())

    argument_spec.update(asa_argument_spec)

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

    lines = module.params['lines']

    result = {'changed': False}

    candidate = NetworkConfig(indent=1)
    candidate.add(lines)

    acl_name = parse_acl_name(module)

    if not module.params['force']:
        contents = get_acl_config(module, acl_name)
        config = NetworkConfig(indent=1, contents=contents)

        commands = candidate.difference(config)
        commands = dumps(commands, 'commands').split('\n')
        commands = [str(c) for c in commands if c]
    else:
        commands = str(candidate).split('\n')

    if commands:
        if module.params['before']:
            commands[:0] = module.params['before']

        if module.params['after']:
            commands.extend(module.params['after'])

        if not module.check_mode:
            load_config(module, commands)

        result['changed'] = True

    result['updates'] = commands

    module.exit_json(**result)
Пример #6
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(
        src=dict(),
        force=dict(default=False, type='bool'),
        include_defaults=dict(default=True, type='bool'),
        backup=dict(default=False, type='bool'),
        config=dict(),
    )

    # Removed the use of provider arguments in 2.3 due to network_cli
    # connection plugin.  To be removed in 2.5
    argument_spec.update(_transitional_argument_spec())

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

    module = LocalAnsibleModule(argument_spec=argument_spec,
                                mutually_exclusive=mutually_exclusive,
                                supports_check_mode=True)

    warnings = check_args(module)

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

    candidate = NetworkConfig(contents=module.params['src'], indent=1)

    result = {'changed': False}

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

    if not module.params['force']:
        contents = get_current_config(module)
        configobj = NetworkConfig(contents=contents, indent=1)
        commands = candidate.difference(configobj)
        commands = dumps(commands, 'commands').split('\n')
        commands = [str(c).strip() for c in commands if c]
    else:
        commands = [c.strip() for c in str(candidate).split('\n')]

    if commands:
        if not module.check_mode:
            load_config(commands)
        result['changed'] = True

    result['updates'] = commands

    module.exit_json(**result)
Пример #7
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(
        src=dict(),
        force=dict(default=False, type='bool'),
        include_defaults=dict(default=True, type='bool'),
        backup=dict(default=False, type='bool'),
        config=dict(),
    )

    argument_spec.update(ios_cli.ios_cli_argument_spec)

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

    cls = get_ansible_module()
    module = cls(argument_spec=argument_spec,
                 mutually_exclusive=mutually_exclusive,
                 supports_check_mode=True)

    warnings = list()
    check_args(module, warnings)

    candidate = NetworkConfig(contents=module.params['src'], indent=1)

    result = {'changed': False}
    if warnings:
        result['warnings'] = warnings

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

    if not module.params['force']:
        contents = get_current_config(module)
        configobj = NetworkConfig(contents=contents, indent=1)
        commands = candidate.difference(configobj)
        commands = dumps(commands, 'commands').split('\n')
        commands = [str(c).strip() for c in commands if c]
    else:
        commands = [c.strip() for c in str(candidate).split('\n')]

    if commands:
        if not module.check_mode:
            load_config(module, commands)
        result['changed'] = True

    result['updates'] = commands

    module.exit_json(**result)
Пример #8
0
def main():

    argument_spec = dict(lines=dict(aliases=['commands'],
                                    required=True,
                                    type='list'),
                         before=dict(type='list'),
                         after=dict(type='list'),
                         match=dict(default='line',
                                    choices=['line', 'strict', 'exact']),
                         replace=dict(default='line',
                                      choices=['line', 'block']),
                         force=dict(default=False, type='bool'),
                         config=dict())

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

    lines = module.params['lines']

    before = module.params['before']
    after = module.params['after']

    match = module.params['match']
    replace = module.params['replace']

    candidate = NetworkConfig(indent=1)
    candidate.add(lines)

    module.filter = check_input_acl(lines, module)

    if not module.params['force']:
        contents = get_config(module)
        config = NetworkConfig(indent=1, contents=contents)
        commands = candidate.difference(config)
        commands = dumps(commands, 'commands').split('\n')
    else:
        commands = str(candidate).split('\n')

    if commands:
        if not module.check_mode:
            commands = [str(c) for c in commands if c]
            response = module.config(commands)
            result['responses'] = response
        result['changed'] = True

    result['updates'] = commands

    module.exit_json(**result)
Пример #9
0
def run(module, result):
    match = module.params['match']
    replace = module.params['replace']

    candidate = get_candidate(module)

    if match != 'none' and replace != 'config':
        config_text = get_config(module)
        config = NetworkConfig(indent=3, contents=config_text)
        configobjs = candidate.difference(config, match=match, replace=replace)
    else:
        configobjs = candidate.items

    if configobjs:
        commands = dumps(configobjs, 'commands').split('\n')

        if module.params['lines']:
            if module.params['before']:
                commands[:0] = module.params['before']

            if module.params['after']:
                commands.extend(module.params['after'])

        result['commands'] = commands

        replace = module.params['replace'] == 'config'
        commit = not module.check_mode

        response = load_config(module, commands, replace=replace, commit=commit)
        if 'diff' in response:
            result['diff'] = {'prepared': response['diff']}
        if 'session' in response:
            result['session'] = response['session']

        result['changed'] = True
Пример #10
0
def run(module, result):
    match = module.params['match']
    replace = module.params['replace']
    replace_config = replace == 'config'
    path = module.params['parents']
    comment = module.params['comment']
    check_mode = module.check_mode

    candidate = get_candidate(module)

    if match != 'none' and replace != 'config':
        contents = get_running_config(module)
        configobj = NetworkConfig(contents=contents, indent=1)
        commands = candidate.difference(configobj, path=path, match=match,
                                          replace=replace)
    else:
        commands = candidate.items

    if commands:
        commands = dumps(commands, 'commands').split('\n')

        if any((module.params['lines'], module.params['src'])):
            if module.params['before']:
                commands[:0] = module.params['before']

            if module.params['after']:
                commands.extend(module.params['after'])

            result['commands'] = commands

        diff = load_config(module, commands, not check_mode,
                           replace_config, comment)
        if diff:
            result['diff'] = dict(prepared=diff)
            result['changed'] = True
Пример #11
0
def get_config(module):
    contents = module.params['config']
    if not contents:
        cmd = 'show running-config all section management api http-commands'
        contents = module.cli([cmd])
    config = NetworkConfig(indent=3, contents=contents[0])
    return config
Пример #12
0
def get_config(module):
    contents = module.params['config']
    if not contents:
        contents = str(module.config.get_config()).split('\n')
        module.params['config'] = contents
    contents = '\n'.join(contents)
    return NetworkConfig(contents=contents, device_os='junos')
Пример #13
0
def get_config(module):
    contents = module.params['running_config']
    if not contents:
        contents = module.cli('show running-config all')[0]
        module.params['running_config'] = contents

    return NetworkConfig(indent=1, contents=contents)
Пример #14
0
def get_running_config(module, config=None):
    contents = module.params['running_config']
    if not contents:
        if config:
            contents = config
        else:
            contents = get_config(module)
    return NetworkConfig(indent=1, contents=contents)
def get_candidate(module):
    candidate = NetworkConfig(indent=1)

    if module.params['src']:
        candidate.load(module.params['src'])
    elif module.params['lines']:
        candidate.add(module.params['lines'])
    return candidate
Пример #16
0
def get_candidate(module):
    candidate = NetworkConfig(indent=1)
    if module.params['src']:
        candidate.load(module.params['src'])
    elif module.params['lines']:
        parents = module.params['parents'] or list()
        candidate.add(module.params['lines'], parents=parents)
    return candidate
Пример #17
0
def get_config(module, result):
    contents = module.params['config']
    if not contents:
        defaults = module.params['defaults']
        contents = module.config.get_config(include_defaults=defaults)

    contents, banners = extract_banners(contents)
    return NetworkConfig(indent=1, contents=contents), banners
Пример #18
0
def get_config(module):
    """ get current configuration. """

    contents = module.params['config']
    if not contents:
        defaults = module.params['defaults']
        contents = module.config.get_config(include_defaults=defaults)
    return NetworkConfig(indent=2, contents=contents)
Пример #19
0
def get_running_config(module):
    contents = module.params['config']
    if not contents:
        flags = []
        if module.params['defaults']:
            flags.append('all')
        contents = get_config(module, flags=flags)
    return NetworkConfig(indent=2, contents=contents)
Пример #20
0
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)
Пример #21
0
def get_config(module, include_defaults=False):
    contents = module.params['running_config']
    if not contents:
        if not include_defaults:
            contents = module.config.get_config()
        else:
            contents = module.cli('show running-config all')[0]
        module.params['running_config'] = contents
    return NetworkConfig(indent=1, contents=contents)
Пример #22
0
def get_running_config(module):
    contents = module.params['config']
    if not contents:
        flags = []
        if module.params['defaults']:
            flags.append(get_defaults_flag(module))
        contents = get_config(module, flags=flags)
    contents, banners = extract_banners(contents)
    return NetworkConfig(indent=1, contents=contents), banners
Пример #23
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(
        src=dict(),
        force=dict(default=False, type='bool'),
        include_defaults=dict(default=True, type='bool'),
        backup=dict(default=False, type='bool'),
        config=dict(),
    )

    argument_spec.update(nxos_argument_spec)

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

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

    result = dict(changed=False)

    candidate = NetworkConfig(contents=module.params['src'], indent=2)

    contents = get_current_config(module)
    if contents:
        config = NetworkConfig(contents=contents, indent=2)
        result['__backup__'] = str(contents)

    if not module.params['force']:
        commands = candidate.difference(config)
        commands = dumps(commands, 'commands').split('\n')
        commands = [str(c) for c in commands if c]
    else:
        commands = str(candidate).split('\n')

    if commands:
        if not module.check_mode:
            load_config(module, commands)
        result['changed'] = True

    result['updates'] = commands
    result['commands'] = commands

    module.exit_json(**result)
Пример #24
0
def get_running_config(module, current_config=None):
    contents = module.params['running_config']
    if not contents:
        if not module.params['defaults'] and current_config:
            contents, banners = extract_banners(current_config.config_text)
        else:
            flags = get_defaults_flag(module) if module.params['defaults'] else []
            contents = get_config(module, flags=flags)
    contents, banners = extract_banners(contents)
    return NetworkConfig(indent=1, contents=contents), banners
Пример #25
0
def main():
    """ main entry point for module execution
    """

    argument_spec = dict(
        src=dict(),
        force=dict(default=False, type='bool'),
        backup=dict(default=False, type='bool'),
        config=dict(),
    )

    mutually_exclusive = [('config', 'backup'), ('config', 'force')]

    module = NetworkModule(argument_spec=argument_spec,
                           mutually_exclusive=mutually_exclusive,
                           supports_check_mode=True)

    result = dict(changed=False)

    candidate = NetworkConfig(contents=module.params['src'], indent=1)

    contents = get_config(module)

    if contents:
        config = NetworkConfig(contents=contents[0], indent=1)
        result['_backup'] = contents[0]

    commands = list()
    if not module.params['force']:
        commands = dumps(candidate.difference(config), 'commands')
    else:
        commands = str(candidate)

    if commands:
        commands = commands.split('\n')
        if not module.check_mode:
            response = module.config(commands)
            result['responses'] = response
        result['changed'] = True

    result['updates'] = commands
    module.exit_json(**result)
Пример #26
0
def get_config(module):
    contents = module.params['config']
    if not contents:
        try:
            contents = module.cli(['show running-config nxapi all'])[0]
        except NetworkError:
            contents = None
    config = NetworkConfig(indent=2)
    if contents:
        config.load(contents)
    return config
Пример #27
0
def get_config(module):
    contents = module.params['config']
    if not contents:
        if module.params['defaults']:
            include = 'defaults'
        elif module.params['passwords']:
            include = 'passwords'
        else:
            include = None
        contents = module.config.get_config(include=include)
    return NetworkConfig(indent=1, contents=contents)
Пример #28
0
def load(module, commands, result):
    candidate = NetworkConfig(indent=2, contents='\n'.join(commands))
    config = get_config(module)
    configobjs = candidate.difference(config)

    if configobjs:
        commands = dumps(configobjs, 'commands').split('\n')
        result['updates'] = commands
        if not module.check_mode:
            load_config(module, commands, result)
        result['changed'] = True
Пример #29
0
def get_config(module, acl_name):
    contents = module.params['config']
    if not contents:
        contents = module.config.get_config()

    filtered_config = list()
    for item in contents.split('\n'):
        if item.startswith('access-list %s' % acl_name):
            filtered_config.append(item)

    return NetworkConfig(indent=1, contents='\n'.join(filtered_config))
Пример #30
0
def load(module, instance, commands, result):
    candidate = NetworkConfig(indent=3)
    candidate.add(commands, parents=['management api http-commands'])

    config = get_config(module)
    configobjs = candidate.difference(config)

    if configobjs:
        commands = dumps(configobjs, 'commands').split('\n')
        result['updates'] = commands
        load_config(module, instance, commands, result)