Пример #1
0
def main():
    """main entry point for module execution
    """
    argument_spec = dict(
        netconf_port=dict(type='int', default=830, aliases=['listens_on']),
        netconf_vrf=dict(aliases=['vrf']),
        state=dict(default='present', choices=['present', 'absent']),
    )
    argument_spec.update(iosxr_argument_spec)

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

    warnings = list()
    check_args(module, warnings)

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

    want = map_params_to_obj(module)
    have = map_config_to_obj(module)
    commands = map_obj_to_commands((want, have), module)
    result['commands'] = commands

    if commands:
        if not module.check_mode:
            diff = load_config(module, commands, result['warnings'], commit=True)
            if diff:
                if module._diff:
                    result['diff'] = {'prepared': diff}
            exec_command(module, 'exit')
        result['changed'] = True

    module.exit_json(**result)
Пример #2
0
def main():
    """ Main entry point for Ansible module execution
    """
    argument_spec = dict(hostname=dict(),
                         domain_name=dict(),
                         domain_search=dict(type='list'),
                         name_servers=dict(type='list'),
                         lookup_source=dict(),
                         lookup_enabled=dict(type='bool'),
                         state=dict(choices=['present', 'absent'],
                                    default='present'))

    argument_spec.update(iosxr_argument_spec)

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

    warnings = list()
    check_args(module, warnings)

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

    want = map_params_to_obj(module)
    have = map_config_to_obj(module)

    commands = map_obj_to_commands(want, have, module)
    result['commands'] = commands

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

    module.exit_json(**result)
Пример #3
0
def main():
    spec = dict(commands=dict(type='list', required=True),
                wait_for=dict(type='list', aliases=['waitfor']),
                match=dict(default='all', choices=['all', 'any']),
                retries=dict(default=10, type='int'),
                interval=dict(default=1, type='int'))

    spec.update(iosxr_argument_spec)

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

    warnings = list()
    check_args(module, warnings)

    commands = parse_commands(module, warnings)

    wait_for = module.params['wait_for'] or list()
    conditionals = [Conditional(c) for c in wait_for]

    retries = module.params['retries']
    interval = module.params['interval']
    match = module.params['match']

    while retries > 0:
        responses = run_commands(module, commands)

        for item in list(conditionals):
            if item(responses):
                if match == 'any':
                    conditionals = list()
                    break
                conditionals.remove(item)

        if not conditionals:
            break

        time.sleep(interval)
        retries -= 1

    if conditionals:
        failed_conditions = [item.raw for item in conditionals]
        msg = 'One or more conditional statements have not be satisfied'
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    result = {
        'changed': False,
        'stdout': responses,
        'warnings': warnings,
        'stdout_lines': list(to_lines(responses))
    }

    module.exit_json(**result)
Пример #4
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        dest=dict(
            type='str',
            choices=['on', 'hostnameprefix', 'console', 'monitor',
                     'buffered']),
        name=dict(type='str'),
        size=dict(type='int'),
        facility=dict(type='str', default='local7'),
        level=dict(type='str', default='debugging'),
        state=dict(default='present', choices=['present', 'absent']),
    )

    aggregate_spec = deepcopy(element_spec)

    # remove default in aggregate spec, to handle common arguments
    remove_default_spec(aggregate_spec)

    argument_spec = dict(aggregate=dict(type='list',
                                        elements='dict',
                                        options=aggregate_spec), )

    argument_spec.update(element_spec)
    argument_spec.update(iosxr_argument_spec)

    required_if = [('dest', 'hostnameprefix', ['name'])]

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

    warnings = list()
    check_args(module, warnings)

    result = {'changed': False}

    want = map_params_to_obj(module, required_if=required_if)
    have = map_config_to_obj(module)
    commands = map_obj_to_commands((want, have), module)

    result['commands'] = commands
    result['warnings'] = warnings

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

    module.exit_json(**result)
Пример #5
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(name=dict(),
                        description=dict(),
                        speed=dict(),
                        mtu=dict(),
                        duplex=dict(choices=['full', 'half']),
                        enabled=dict(default=True, type='bool'),
                        tx_rate=dict(),
                        rx_rate=dict(),
                        delay=dict(default=10, type='int'),
                        state=dict(default='present',
                                   choices=['present', 'absent', 'up',
                                            'down']))

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec['name'] = dict(required=True)

    # remove default in aggregate spec, to handle common arguments
    remove_default_spec(aggregate_spec)

    argument_spec = dict(aggregate=dict(type='list',
                                        elements='dict',
                                        options=aggregate_spec), )

    argument_spec.update(element_spec)
    argument_spec.update(iosxr_argument_spec)

    required_one_of = [['name', 'aggregate']]
    mutually_exclusive = [['name', 'aggregate']]

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

    warnings = list()
    check_args(module, warnings)

    result = {'changed': False}

    want = map_params_to_obj(module)
    have = map_config_to_obj(module)

    commands = map_obj_to_commands((want, have))

    result['commands'] = commands
    result['warnings'] = warnings

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

    failed_conditions = check_declarative_intent_params(module, want, result)

    if failed_conditions:
        msg = 'One or more conditional statements have not been satisfied'
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    module.exit_json(**result)
Пример #6
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(name=dict(),
                        configured_password=dict(no_log=True),
                        update_password=dict(default='always',
                                             choices=['on_create', 'always']),
                        public_key=dict(),
                        public_key_contents=dict(),
                        group=dict(aliases=['role']),
                        groups=dict(type='list', elements='dict'),
                        state=dict(default='present',
                                   choices=['present', 'absent']))
    aggregate_spec = deepcopy(element_spec)
    aggregate_spec['name'] = dict(required=True)

    # remove default in aggregate spec, to handle common arguments
    remove_default_spec(aggregate_spec)

    argument_spec = dict(aggregate=dict(type='list',
                                        elements='dict',
                                        options=aggregate_spec,
                                        aliases=['users', 'collection']),
                         purge=dict(type='bool', default=False))

    argument_spec.update(element_spec)
    argument_spec.update(iosxr_argument_spec)
    mutually_exclusive = [('name', 'aggregate'),
                          ('public_key', 'public_key_contents'),
                          ('group', 'groups')]

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

    if (module.params['public_key_contents'] or module.params['public_key']):
        if not HAS_B64:
            module.fail_json(
                msg='library base64 is required but does not appear to be '
                'installed. It can be installed using `pip install base64`')
        if not HAS_PARAMIKO:
            module.fail_json(
                msg='library paramiko is required but does not appear to be '
                'installed. It can be installed using `pip install paramiko`')

    warnings = list()
    if module.params['password'] and not module.params['configured_password']:
        warnings.append(
            'The "password" argument is used to authenticate the current connection. '
            + 'To set a user password use "configured_password" instead.')

    check_args(module, warnings)

    result = {'changed': False}

    want = map_params_to_obj(module)
    have = map_config_to_obj(module)

    commands = map_obj_to_commands((want, have), module)

    if module.params['purge']:
        want_users = [x['name'] for x in want]
        have_users = [x['name'] for x in have]
        for item in set(have_users).difference(want_users):
            if item != 'admin':
                commands.append('no username %s' % item)

    result['commands'] = commands
    result['warnings'] = warnings

    if 'no username admin' in commands:
        module.fail_json(msg='cannot delete the `admin` account')

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

    if module.params['state'] == 'present' and (
            module.params['public_key_contents']
            or module.params['public_key']):
        if not module.check_mode:
            key = convert_key_to_base64(module)
            copykeys = copy_key_to_node(module, key)
            if copykeys is False:
                warnings.append(
                    'Please set up your provider before running this playbook')

            if module.params['aggregate']:
                for user in module.params['aggregate']:
                    cmdtodo = "admin crypto key import authentication rsa username %s harddisk:/publickey_aggregate.b64" % (
                        user)
                    addremove = addremovekey(module, cmdtodo)
                    if addremove is False:
                        warnings.append(
                            'Please set up your provider before running this playbook'
                        )
            else:
                cmdtodo = "admin crypto key import authentication rsa username %s harddisk:/publickey_%s.b64" % (
                    module.params['name'], module.params['name'])
                addremove = addremovekey(module, cmdtodo)
                if addremove is False:
                    warnings.append(
                        'Please set up your provider before running this playbook'
                    )
    elif module.params['state'] == 'absent':
        if not module.check_mode:
            if module.params['aggregate']:
                for user in module.params['aggregate']:
                    cmdtodo = "admin crypto key zeroize authentication rsa username %s" % (
                        user)
                    addremove = addremovekey(module, cmdtodo)
                    if addremove is False:
                        warnings.append(
                            'Please set up your provider before running this playbook'
                        )
            else:
                cmdtodo = "admin crypto key zeroize authentication rsa username %s" % (
                    module.params['name'])
                addremove = addremovekey(module, cmdtodo)
                if addremove is False:
                    warnings.append(
                        'Please set up your provider before running this playbook'
                    )
    elif module.params['purge'] is True:
        if not module.check_mode:
            cmdtodo = "admin crypto key zeroize authentication rsa all"
            addremove = addremovekey(module, cmdtodo)
            if addremove is False:
                warnings.append(
                    'Please set up your provider before running this playbook')

    module.exit_json(**result)
Пример #7
0
def main():
    spec = dict(gather_subset=dict(default=['!config'], type='list'))

    spec.update(iosxr_argument_spec)

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

    warnings = list()
    check_args(module, warnings)

    gather_subset = module.params['gather_subset']

    runable_subsets = set()
    exclude_subsets = set()

    for subset in gather_subset:
        if subset == 'all':
            runable_subsets.update(VALID_SUBSETS)
            continue

        if subset.startswith('!'):
            subset = subset[1:]
            if subset == 'all':
                exclude_subsets.update(VALID_SUBSETS)
                continue
            exclude = True
        else:
            exclude = False

        if subset not in VALID_SUBSETS:
            module.fail_json(msg='Bad subset')

        if exclude:
            exclude_subsets.add(subset)
        else:
            runable_subsets.add(subset)

    if not runable_subsets:
        runable_subsets.update(VALID_SUBSETS)

    runable_subsets.difference_update(exclude_subsets)
    runable_subsets.add('default')

    facts = dict()
    facts['gather_subset'] = list(runable_subsets)

    instances = list()
    for key in runable_subsets:
        instances.append(FACT_SUBSETS[key]())

    try:
        for inst in instances:
            commands = inst.commands()
            responses = run_commands(module, commands)
            results = dict(zip(commands, responses))
            inst.populate(results)
            facts.update(inst.facts)
    except Exception:
        module.exit_json(out=module.from_json(results))

    ansible_facts = dict()
    for key, value in iteritems(facts):
        key = 'ansible_net_%s' % key
        ansible_facts[key] = value

    module.exit_json(ansible_facts=ansible_facts, warnings=warnings)