Example #1
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(
        banner=dict(
            required=True,
            choices=["login", "motd", "exec", "incoming", "slip-ppp"],
        ),
        text=dict(),
        state=dict(default="present", choices=["present", "absent"]),
    )
    argument_spec.update(ios_argument_spec)
    required_if = [("state", "present", ("text", ))]
    module = AnsibleModule(
        argument_spec=argument_spec,
        required_if=required_if,
        supports_check_mode=True,
    )
    warnings = list()
    result = {"changed": False}
    if warnings:
        result["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["changed"] = True
    module.exit_json(**result)
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(state=dict(
        default="present",
        choices=["present", "absent", "enabled", "disabled"],
    ))
    argument_spec.update(ios_argument_spec)
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)
    warnings = list()
    result = {"changed": False}
    if warnings:
        result["warnings"] = warnings
    HAS_LLDP = has_lldp(module)
    commands = []
    if module.params["state"] == "absent" and HAS_LLDP:
        commands.append("no lldp run")
    elif module.params["state"] == "present" and not HAS_LLDP:
        commands.append("lldp run")
    result["commands"] = commands
    if commands:
        if not module.check_mode:
            load_config(module, commands)
        result["changed"] = True
    module.exit_json(**result)
Example #3
0
def main():
    """ Main entry point for Ansible module execution
    """
    argument_spec = dict(
        hostname=dict(),
        domain_name=dict(type="list", elements="raw"),
        domain_search=dict(type="list", elements="raw"),
        name_servers=dict(type="list", elements="raw"),
        lookup_source=dict(),
        lookup_enabled=dict(type="bool"),
        state=dict(choices=["present", "absent"], default="present"),
    )
    argument_spec.update(ios_argument_spec)
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True)
    result = {"changed": False}
    warnings = list()
    result["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["changed"] = True
    module.exit_json(**result)
Example #4
0
def main():
    argument_spec = dict(
        server=dict(),
        source_int=dict(),
        acl=dict(),
        logging=dict(type="bool", default=False),
        auth=dict(type="bool", default=False),
        auth_key=dict(),
        key_id=dict(),
        state=dict(choices=["absent", "present"], default="present"),
    )
    argument_spec.update(ios_argument_spec)
    module = AnsibleModule(
        argument_spec=argument_spec, supports_check_mode=True
    )
    result = {"changed": False}
    warnings = list()
    if warnings:
        result["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["changed"] = True
    module.exit_json(**result)
Example #5
0
def main():
    """ Main entry point for Ansible module execution
    """
    argument_spec = dict(hostname=dict(),
                         domain_name=dict(type='list'),
                         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(ios_argument_spec)

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

    result = {'changed': False}

    warnings = list()
    result['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['changed'] = True

    module.exit_json(**result)
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(
        vrfs=dict(type="list"),
        name=dict(),
        description=dict(),
        rd=dict(),
        route_export=dict(type="list"),
        route_import=dict(type="list"),
        route_both=dict(type="list"),
        route_export_ipv4=dict(type="list"),
        route_import_ipv4=dict(type="list"),
        route_both_ipv4=dict(type="list"),
        route_export_ipv6=dict(type="list"),
        route_import_ipv6=dict(type="list"),
        route_both_ipv6=dict(type="list"),
        interfaces=dict(type="list"),
        associated_interfaces=dict(type="list"),
        delay=dict(default=10, type="int"),
        purge=dict(type="bool", default=False),
        state=dict(default="present", choices=["present", "absent"]),
    )

    argument_spec.update(ios_argument_spec)

    mutually_exclusive = [("name", "vrfs")]
    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=mutually_exclusive,
        supports_check_mode=True,
    )

    result = {"changed": False}

    warnings = list()
    result["warnings"] = warnings

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

    if module.params["purge"]:
        want_vrfs = [x["name"] for x in want]
        have_vrfs = [x["name"] for x in have]
        for item in set(have_vrfs).difference(want_vrfs):
            cmd = "no vrf definition %s" % item
            if cmd not in commands:
                commands.append(cmd)

    result["commands"] = commands

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

    check_declarative_intent_params(want, module, result)

    module.exit_json(**result)
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        dest=dict(
            type='str',
            choices=['on', 'host', 'console', 'monitor', 'buffered', 'trap']),
        name=dict(type='str'),
        size=dict(type='int'),
        facility=dict(type='str'),
        level=dict(type='str',
                   default='debugging',
                   choices=[
                       'emergencies', 'alerts', 'critical', 'errors',
                       'warnings', 'notifications', 'informational',
                       '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(ios_argument_spec)

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

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

    device_info = get_capabilities(module)
    os_version = device_info['device_info']['network_os_version']

    warnings = list()

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

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

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

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

    module.exit_json(**result)
Example #8
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        group=dict(type="int"),
        mode=dict(choices=["active", "on", "passive", "auto", "desirable"]),
        members=dict(type="list"),
        state=dict(default="present", choices=["present", "absent"]),
    )

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec["group"] = dict(required=True)

    required_one_of = [["group", "aggregate"]]
    required_together = [["members", "mode"]]
    mutually_exclusive = [["group", "aggregate"]]

    # 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,
            required_together=required_together,
        ),
        purge=dict(default=False, type="bool"),
    )

    argument_spec.update(element_spec)
    argument_spec.update(ios_argument_spec)

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

    warnings = list()
    result = {"changed": False}
    if warnings:
        result["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["changed"] = True

    module.exit_json(**result)
Example #9
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(vrfs=dict(type='list'),
                         name=dict(),
                         description=dict(),
                         rd=dict(),
                         route_export=dict(type='list'),
                         route_import=dict(type='list'),
                         route_both=dict(type='list'),
                         route_export_ipv4=dict(type='list'),
                         route_import_ipv4=dict(type='list'),
                         route_both_ipv4=dict(type='list'),
                         route_export_ipv6=dict(type='list'),
                         route_import_ipv6=dict(type='list'),
                         route_both_ipv6=dict(type='list'),
                         interfaces=dict(type='list'),
                         associated_interfaces=dict(type='list'),
                         delay=dict(default=10, type='int'),
                         purge=dict(type='bool', default=False),
                         state=dict(default='present',
                                    choices=['present', 'absent']))

    argument_spec.update(ios_argument_spec)

    mutually_exclusive = [('name', 'vrfs')]
    module = AnsibleModule(argument_spec=argument_spec,
                           mutually_exclusive=mutually_exclusive,
                           supports_check_mode=True)

    result = {'changed': False}

    warnings = list()
    result['warnings'] = warnings

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

    if module.params['purge']:
        want_vrfs = [x['name'] for x in want]
        have_vrfs = [x['name'] for x in have]
        for item in set(have_vrfs).difference(want_vrfs):
            cmd = 'no vrf definition %s' % item
            if cmd not in commands:
                commands.append(cmd)

    result['commands'] = commands

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

    check_declarative_intent_params(want, module, result)

    module.exit_json(**result)
Example #10
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        prefix=dict(type="str"),
        mask=dict(type="str"),
        next_hop=dict(type="str"),
        vrf=dict(type="str"),
        interface=dict(type="str"),
        name=dict(type="str", aliases=["description"]),
        admin_distance=dict(type="str"),
        track=dict(type="str"),
        tag=dict(tag="str"),
        state=dict(default="present", choices=["present", "absent"]),
    )

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec["prefix"] = 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(ios_argument_spec)

    required_one_of = [["aggregate", "prefix"]]
    required_together = [["prefix", "mask"]]
    mutually_exclusive = [["aggregate", "prefix"]]

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

    warnings = list()

    result = {"changed": False}
    if warnings:
        result["warnings"] = warnings
    want = map_params_to_obj(module, required_together=required_together)
    have = map_config_to_obj(module)

    commands = map_obj_to_commands(want, have)
    result["commands"] = commands

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

        result["changed"] = True

    module.exit_json(**result)
Example #11
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        dest=dict(
            type="str",
            choices=["on", "host", "console", "monitor", "buffered", "trap"],
        ),
        name=dict(type="str"),
        size=dict(type="int"),
        facility=dict(type="str"),
        level=dict(
            type="str",
            default="debugging",
            choices=[
                "emergencies",
                "alerts",
                "critical",
                "errors",
                "warnings",
                "notifications",
                "informational",
                "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(ios_argument_spec)
    required_if = [("dest", "host", ["name"])]
    module = AnsibleModule(
        argument_spec=argument_spec,
        required_if=required_if,
        supports_check_mode=True,
    )
    device_info = get_capabilities(module)
    os_version = device_info["device_info"]["network_os_version"]
    warnings = list()
    result = {"changed": False}
    if warnings:
        result["warnings"] = warnings
    want = map_params_to_obj(module, required_if=required_if)
    have = map_config_to_obj(module)
    commands = map_obj_to_commands((want, have), module, os_version)
    result["commands"] = commands
    if commands:
        if not module.check_mode:
            load_config(module, commands)
        result["changed"] = True
    module.exit_json(**result)
Example #12
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        vlan_id=dict(type="int"),
        name=dict(),
        interfaces=dict(type="list"),
        associated_interfaces=dict(type="list"),
        delay=dict(default=10, type="int"),
        state=dict(
            default="present",
            choices=["present", "absent", "active", "suspend"],
        ),
    )

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec["vlan_id"] = 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),
        purge=dict(default=False, type="bool"),
    )

    argument_spec.update(element_spec)
    argument_spec.update(ios_argument_spec)

    required_one_of = [["vlan_id", "aggregate"]]
    mutually_exclusive = [["vlan_id", "aggregate"]]

    module = AnsibleModule(
        argument_spec=argument_spec,
        required_one_of=required_one_of,
        mutually_exclusive=mutually_exclusive,
        supports_check_mode=True,
    )
    warnings = list()
    result = {"changed": False}
    if warnings:
        result["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["changed"] = True

    check_declarative_intent_params(want, module, result)

    module.exit_json(**result)
Example #13
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        prefix=dict(type='str'),
        mask=dict(type='str'),
        next_hop=dict(type='str'),
        vrf=dict(type='str'),
        interface=dict(type='str'),
        name=dict(type='str', aliases=['description']),
        admin_distance=dict(type='str'),
        track=dict(type='str'),
        tag=dict(tag='str'),
        state=dict(default='present', choices=['present', 'absent'])
    )

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec['prefix'] = 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(ios_argument_spec)

    required_one_of = [['aggregate', 'prefix']]
    required_together = [['prefix', 'mask']]
    mutually_exclusive = [['aggregate', 'prefix']]

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

    warnings = list()

    result = {'changed': False}
    if warnings:
        result['warnings'] = warnings
    want = map_params_to_obj(module, required_together=required_together)
    have = map_config_to_obj(module)

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

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

        result['changed'] = True

    module.exit_json(**result)
Example #14
0
def main():
    """main entry point for module execution"""
    neighbors_spec = dict(host=dict(), port=dict())
    element_spec = dict(
        name=dict(),
        description=dict(),
        speed=dict(),
        mtu=dict(),
        duplex=dict(choices=["full", "half", "auto"]),
        enabled=dict(default=True, type="bool"),
        tx_rate=dict(),
        rx_rate=dict(),
        neighbors=dict(type="list", elements="dict", options=neighbors_spec),
        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(ios_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()
    result = {"changed": False}
    if warnings:
        result["warnings"] = warnings
    want = map_params_to_obj(module)
    have = map_config_to_obj(module)
    commands = map_obj_to_commands((want, have))
    result["commands"] = commands
    if commands:
        if not module.check_mode:
            load_config(module, commands)
        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)
Example #15
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        group=dict(type='int'),
        mode=dict(choices=['active', 'on', 'passive', 'auto', 'desirable']),
        members=dict(type='list'),
        state=dict(default='present',
                   choices=['present', 'absent'])
    )

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

    required_one_of = [['group', 'aggregate']]
    required_together = [['members', 'mode']]
    mutually_exclusive = [['group', 'aggregate']]

    # 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,
                       required_together=required_together),
        purge=dict(default=False, type='bool')
    )

    argument_spec.update(element_spec)
    argument_spec.update(ios_argument_spec)

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

    warnings = list()
    result = {'changed': False}
    if warnings:
        result['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['changed'] = True

    module.exit_json(**result)
Example #16
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        vlan_id=dict(type='int'),
        name=dict(),
        interfaces=dict(type='list'),
        associated_interfaces=dict(type='list'),
        delay=dict(default=10, type='int'),
        state=dict(default='present',
                   choices=['present', 'absent', 'active', 'suspend'])
    )

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec['vlan_id'] = 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),
        purge=dict(default=False, type='bool')
    )

    argument_spec.update(element_spec)
    argument_spec.update(ios_argument_spec)

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

    module = AnsibleModule(argument_spec=argument_spec,
                           required_one_of=required_one_of,
                           mutually_exclusive=mutually_exclusive,
                           supports_check_mode=True)
    warnings = list()
    result = {'changed': False}
    if warnings:
        result['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['changed'] = True

    check_declarative_intent_params(want, module, result)

    module.exit_json(**result)
Example #17
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        name=dict(),
        ipv4=dict(),
        ipv6=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)
    )

    argument_spec.update(element_spec)
    argument_spec.update(ios_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()

    result = {"changed": False}

    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:
            resp = load_config(module, commands)
            warnings.extend((out for out in resp if out))

        result["changed"] = True

    if warnings:
        result["warnings"] = warnings

    module.exit_json(**result)
Example #18
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(
        state=dict(default='present',
                   choices=['present', 'absent',
                            'enabled', 'disabled'])
    )

    argument_spec.update(ios_argument_spec)

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

    warnings = list()

    result = {'changed': False}

    if warnings:
        result['warnings'] = warnings

    HAS_LLDP = has_lldp(module)

    commands = []

    if module.params['state'] == 'absent' and HAS_LLDP:
        commands.append('no lldp run')
    elif module.params['state'] == 'present' and not HAS_LLDP:
        commands.append('lldp run')

    result['commands'] = commands

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

        result['changed'] = True

    module.exit_json(**result)
Example #19
0
def main():
    """ main entry point for module execution
    """
    argument_spec = dict(banner=dict(
        required=True,
        choices=['login', 'motd', 'exec', 'incoming', 'slip-ppp']),
                         text=dict(),
                         state=dict(default='present',
                                    choices=['present', 'absent']))

    argument_spec.update(ios_argument_spec)

    required_if = [('state', 'present', ('text', ))]

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

    warnings = list()

    result = {'changed': False}
    if warnings:
        result['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['changed'] = True

    module.exit_json(**result)
Example #20
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(name=dict(type='str', aliases=['interface']),
                        mode=dict(choices=['access', 'trunk']),
                        access_vlan=dict(type='str'),
                        native_vlan=dict(type='str'),
                        trunk_vlans=dict(type='str'),
                        trunk_allowed_vlans=dict(type='str'),
                        state=dict(
                            choices=['absent', 'present', 'unconfigured'],
                            default='present'))

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

    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[['access_vlan', 'trunk_vlans'],
                            ['access_vlan', 'native_vlan'],
                            ['access_vlan', 'trunk_allowed_vlans']],
        supports_check_mode=True)

    warnings = list()
    commands = []
    result = {'changed': False, 'warnings': warnings}

    want = map_params_to_obj(module)
    for w in want:
        name = w['name']
        mode = w['mode']
        access_vlan = w['access_vlan']
        state = w['state']
        trunk_vlans = w['trunk_vlans']
        native_vlan = w['native_vlan']
        trunk_allowed_vlans = w['trunk_allowed_vlans']

        args = dict(name=name,
                    mode=mode,
                    access_vlan=access_vlan,
                    native_vlan=native_vlan,
                    trunk_vlans=trunk_vlans,
                    trunk_allowed_vlans=trunk_allowed_vlans)

        proposed = dict((k, v) for k, v in args.items() if v is not None)

        name = name.lower()

        if mode == 'access' and state == 'present' and not access_vlan:
            module.fail_json(
                msg=
                'access_vlan param is required when mode=access && state=present'
            )

        if mode == 'trunk' and access_vlan:
            module.fail_json(
                msg='access_vlan param not supported when using mode=trunk')

        if not is_switchport(name, module):
            module.fail_json(
                msg='Ensure interface is configured to be a L2'
                '\nport first before using this module. You can use'
                '\nthe ios_interface module for this.')

        if interface_is_portchannel(name, module):
            module.fail_json(msg='Cannot change L2 config on physical '
                             '\nport because it is in a portchannel. '
                             '\nYou should update the portchannel config.')

        # existing will never be null for Eth intfs as there is always a default
        existing = get_switchport(name, module)

        # Safeguard check
        # If there isn't an existing, something is wrong per previous comment
        if not existing:
            module.fail_json(
                msg='Make sure you are using the FULL interface name')

        if trunk_vlans or trunk_allowed_vlans:
            if trunk_vlans:
                trunk_vlans_list = vlan_range_to_list(trunk_vlans)
            elif trunk_allowed_vlans:
                trunk_vlans_list = vlan_range_to_list(trunk_allowed_vlans)
                proposed['allowed'] = True

            existing_trunks_list = vlan_range_to_list(
                (existing['trunk_vlans']))

            existing['trunk_vlans_list'] = existing_trunks_list
            proposed['trunk_vlans_list'] = trunk_vlans_list

        current_vlans = get_list_of_vlans(module)

        if state == 'present':
            if access_vlan and access_vlan not in current_vlans:
                module.fail_json(
                    msg='You are trying to configure a VLAN'
                    ' on an interface that\ndoes not exist on the '
                    ' switch yet!',
                    vlan=access_vlan)
            elif native_vlan and native_vlan not in current_vlans:
                module.fail_json(
                    msg='You are trying to configure a VLAN'
                    ' on an interface that\ndoes not exist on the '
                    ' switch yet!',
                    vlan=native_vlan)
            else:
                command = get_switchport_config_commands(
                    name, existing, proposed, module)
                commands.append(command)
        elif state == 'unconfigured':
            is_default = is_switchport_default(existing)
            if not is_default:
                command = default_switchport_config(name)
                commands.append(command)
        elif state == 'absent':
            command = remove_switchport_config_commands(
                name, existing, proposed, module)
            commands.append(command)

        if trunk_vlans or trunk_allowed_vlans:
            existing.pop('trunk_vlans_list')
            proposed.pop('trunk_vlans_list')

    cmds = flatten_list(commands)
    if cmds:
        if module.check_mode:
            module.exit_json(changed=True, commands=cmds)
        else:
            result['changed'] = True
            load_config(module, cmds)
            if 'configure' in cmds:
                cmds.pop(0)

    result['commands'] = cmds

    module.exit_json(**result)
Example #21
0
def main():
    """ main entry point for module execution
    """
    hashed_password_spec = dict(
        type=dict(type="int", required=True),
        value=dict(no_log=True, required=True),
    )
    element_spec = dict(
        name=dict(),
        configured_password=dict(no_log=True),
        hashed_password=dict(
            no_log=True, type="dict", options=hashed_password_spec
        ),
        nopassword=dict(type="bool"),
        update_password=dict(
            default="always", choices=["on_create", "always"]
        ),
        password_type=dict(default="secret", choices=["secret", "password"]),
        privilege=dict(type="int"),
        view=dict(aliases=["role"]),
        sshkey=dict(type="list", elements="str", no_log=False),
        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(ios_argument_spec)
    mutually_exclusive = [
        ("name", "aggregate"),
        ("nopassword", "hashed_password", "configured_password"),
    ]
    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=mutually_exclusive,
        supports_check_mode=True,
    )
    warnings = list()
    result = {"changed": False, "warnings": warnings}
    want = map_params_to_obj(module)
    have = map_config_to_obj(module)
    commands = map_obj_to_commands(update_objects(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(user_del_cmd(item))
    result["commands"] = commands
    if commands:
        if not module.check_mode:
            load_config(module, commands)
        result["changed"] = True
    module.exit_json(**result)
Example #22
0
def main():
    """ main entry point for module execution
    """
    element_spec = dict(
        name=dict(type="str", aliases=["interface"]),
        mode=dict(choices=["access", "trunk"]),
        access_vlan=dict(type="str"),
        native_vlan=dict(type="str"),
        trunk_vlans=dict(type="str"),
        trunk_allowed_vlans=dict(type="str"),
        state=dict(choices=["absent", "present", "unconfigured"],
                   default="present"),
    )
    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(ios_argument_spec)
    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[
            ["access_vlan", "trunk_vlans"],
            ["access_vlan", "native_vlan"],
            ["access_vlan", "trunk_allowed_vlans"],
        ],
        supports_check_mode=True,
    )
    warnings = list()
    commands = []
    result = {"changed": False, "warnings": warnings}
    want = map_params_to_obj(module)
    for w in want:
        name = w["name"]
        mode = w["mode"]
        access_vlan = w["access_vlan"]
        state = w["state"]
        trunk_vlans = w["trunk_vlans"]
        native_vlan = w["native_vlan"]
        trunk_allowed_vlans = w["trunk_allowed_vlans"]
        args = dict(
            name=name,
            mode=mode,
            access_vlan=access_vlan,
            native_vlan=native_vlan,
            trunk_vlans=trunk_vlans,
            trunk_allowed_vlans=trunk_allowed_vlans,
        )
        proposed = dict((k, v) for k, v in args.items() if v is not None)
        name = name.lower()
        if mode == "access" and state == "present" and not access_vlan:
            module.fail_json(
                msg=
                "access_vlan param is required when mode=access && state=present"
            )
        if mode == "trunk" and access_vlan:
            module.fail_json(
                msg="access_vlan param not supported when using mode=trunk")
        if not is_switchport(name, module):
            module.fail_json(msg="""Ensure interface is configured to be a L2
port first before using this module. You can use
the ios_interface module for this.""")
        if interface_is_portchannel(name, module):
            module.fail_json(msg="""Cannot change L2 config on physical
port because it is in a portchannel.
You should update the portchannel config.""")
        # existing will never be null for Eth intfs as there is always a default
        existing = get_switchport(name, module)
        # Safeguard check
        # If there isn't an existing, something is wrong per previous comment
        if not existing:
            module.fail_json(
                msg="Make sure you are using the FULL interface name")
        if trunk_vlans or trunk_allowed_vlans:
            if trunk_vlans:
                trunk_vlans_list = vlan_range_to_list(trunk_vlans)
            elif trunk_allowed_vlans:
                trunk_vlans_list = vlan_range_to_list(trunk_allowed_vlans)
                proposed["allowed"] = True
            existing_trunks_list = vlan_range_to_list(existing["trunk_vlans"])
            existing["trunk_vlans_list"] = existing_trunks_list
            proposed["trunk_vlans_list"] = trunk_vlans_list
        current_vlans = get_list_of_vlans(module)
        if state == "present":
            if access_vlan and access_vlan not in current_vlans:
                module.fail_json(
                    msg=
                    """You are trying to configure a VLAN on an interface that
does not exist on the  switch yet!""",
                    vlan=access_vlan,
                )
            elif native_vlan and native_vlan not in current_vlans:
                module.fail_json(
                    msg=
                    """You are trying to configure a VLAN on an interface that
does not exist on the  switch yet!""",
                    vlan=native_vlan,
                )
            else:
                command = get_switchport_config_commands(
                    name, existing, proposed, module)
                commands.append(command)
        elif state == "unconfigured":
            is_default = is_switchport_default(existing)
            if not is_default:
                command = default_switchport_config(name)
                commands.append(command)
        elif state == "absent":
            command = remove_switchport_config_commands(
                name, existing, proposed, module)
            commands.append(command)
        if trunk_vlans or trunk_allowed_vlans:
            existing.pop("trunk_vlans_list")
            proposed.pop("trunk_vlans_list")
    cmds = flatten_list(commands)
    if cmds:
        if module.check_mode:
            module.exit_json(changed=True, commands=cmds)
        else:
            result["changed"] = True
            load_config(module, cmds)
            if "configure" in cmds:
                cmds.pop(0)
    result["commands"] = cmds
    module.exit_json(**result)
Example #23
0
def main():
    """ main entry point for module execution
    """
    neighbors_spec = dict(host=dict(), port=dict())

    element_spec = dict(name=dict(),
                        description=dict(),
                        speed=dict(),
                        mtu=dict(),
                        duplex=dict(choices=['full', 'half', 'auto']),
                        enabled=dict(default=True, type='bool'),
                        tx_rate=dict(),
                        rx_rate=dict(),
                        neighbors=dict(type='list',
                                       elements='dict',
                                       options=neighbors_spec),
                        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(ios_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()

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

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

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

    if commands:
        if not module.check_mode:
            load_config(module, commands)
        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)
Example #24
0
def main():
    """ main entry point for module execution
    """
    hashed_password_spec = dict(type=dict(type='int', required=True),
                                value=dict(no_log=True, required=True))

    element_spec = dict(name=dict(),
                        configured_password=dict(no_log=True),
                        hashed_password=dict(no_log=True,
                                             type='dict',
                                             options=hashed_password_spec),
                        nopassword=dict(type='bool'),
                        update_password=dict(default='always',
                                             choices=['on_create', 'always']),
                        password_type=dict(default='secret',
                                           choices=['secret', 'password']),
                        privilege=dict(type='int'),
                        view=dict(aliases=['role']),
                        sshkey=dict(type='list'),
                        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(ios_argument_spec)

    mutually_exclusive = [('name', 'aggregate'),
                          ('nopassword', 'hashed_password',
                           'configured_password')]

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

    warnings = list()
    result = {'changed': False, 'warnings': warnings}

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

    commands = map_obj_to_commands(update_objects(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(user_del_cmd(item))

    result['commands'] = commands

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

    module.exit_json(**result)