Exemple #1
0
def map_config_to_obj(module):

    obj = list()
    obj_dict = dict()

    group_name = module.params["name"]
    protocol = module.params["protocol"]

    sh_run_group_name = get_config(
        module, flags=["object-group | include {0}".format(group_name)])
    run_group_name = Parser(sh_run_group_name, protocol).parse_obj_grp_name()

    obj_dict["have_name"] = run_group_name

    if run_group_name:
        if run_group_name[0] is not False:
            obj_dict["have_group_type"] = "port-object"
            obj_dict["have_protocol"] = run_group_name[0]
        elif "network" in run_group_name[2]:
            obj_dict["have_group_type"] = "network-object"
        elif "service" in run_group_name[2] and run_group_name[0] is False:
            obj_dict["have_group_type"] = "service-object"
        else:
            obj_dict["have_group_type"] = None

    sh_run_group_type = get_config(
        module, flags=["object-group id {0}".format(group_name)])

    have_description = Parser(sh_run_group_type, protocol).parse_description()
    obj_dict["have_description"] = have_description

    have_host_ip = Parser(sh_run_group_type, protocol).parse_host()
    obj_dict["have_host_ip"] = have_host_ip

    have_group_object = Parser(sh_run_group_type,
                               protocol).parse_group_object()
    obj_dict["have_group_object"] = have_group_object

    have_ip_mask = Parser(sh_run_group_type, protocol).parse_address()
    obj_dict["have_ip_mask"] = have_ip_mask

    have_port_range = Parser(sh_run_group_type, protocol).parse_port_range()
    obj_dict["have_port_range"] = have_port_range

    have_port_eq = Parser(sh_run_group_type, protocol).parse_port_eq()
    obj_dict["have_port_eq"] = have_port_eq

    have_service_cfg = Parser(sh_run_group_type, protocol).parse_service_cfg()

    if have_service_cfg:
        have_lines = list()
        for i in have_service_cfg:
            have_lines.append(i.rstrip(" "))
        obj_dict["have_service_cfg"] = have_lines
    elif have_service_cfg is None:
        obj_dict["have_service_cfg"] = have_service_cfg

    obj.append(obj_dict)

    return obj
Exemple #2
0
def get_acl_config(module, acl_name):
    contents = module.params["config"]
    if not contents:
        contents = get_config(module)

    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))
Exemple #3
0
def get_acl_config(module, acl_name):
    contents = module.params['config']
    if not contents:
        contents = get_config(module)

    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))
Exemple #4
0
def main():
    """ main entry point for module execution
    """
    backup_spec = dict(filename=dict(), dir_path=dict(type="path"))
    argument_spec = dict(
        src=dict(type="path"),
        lines=dict(aliases=["commands"], type="list"),
        parents=dict(type="list"),
        before=dict(type="list"),
        after=dict(type="list"),
        match=dict(default="line", choices=["line", "strict", "exact",
                                            "none"]),
        replace=dict(default="line", choices=["line", "block"]),
        backup_options=dict(type="dict", options=backup_spec),
        config=dict(),
        defaults=dict(type="bool", default=False),
        passwords=dict(type="bool", default=False),
        backup=dict(type="bool", default=False),
        save=dict(type="bool", default=False),
    )

    argument_spec.update(asa_argument_spec)

    mutually_exclusive = [
        ("lines", "src"),
        ("parents", "src"),
        ("defaults", "passwords"),
    ]

    required_if = [
        ("match", "strict", ["lines"]),
        ("match", "exact", ["lines"]),
        ("replace", "block", ["lines"]),
    ]

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

    result = {"changed": False}

    check_args(module)

    config = None

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

    run(module, result)

    module.exit_json(**result)
Exemple #5
0
def run(module, result):
    match = module.params["match"]
    replace = module.params["replace"]
    path = module.params["parents"]

    candidate = get_candidate(module)
    if match != "none":
        contents = module.params["config"]
        if not contents:
            contents = get_config(module)
        config = NetworkConfig(indent=1, contents=contents)
        configobjs = candidate.difference(config,
                                          path=path,
                                          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["updates"] = commands

        # send the configuration commands to the device and merge
        # them with the current running config
        if not module.check_mode:
            load_config(module, commands)
        result["changed"] = True

    if module.params["save"]:
        module.warn(
            "module param save is deprecated, please use newer and updated param save_when instead which is released with more functionality!"
        )
        save_config(module, result)
    if module.params["save_when"] == "always":
        save_config(module, result)
    elif module.params["save_when"] == "modified":
        running_config_checksum = run_commands(
            module, "show running-config | include checksum:")
        startup_config_checksum = run_commands(
            module, "show startup-config | include checksum:")
        if running_config_checksum != startup_config_checksum:
            save_config(module, result)
    elif module.params["save_when"] == "changed" and result["changed"]:
        save_config(module, result)
Exemple #6
0
def main():
    """ main entry point for module execution
    """
    backup_spec = dict(filename=dict(), dir_path=dict(type='path'))
    argument_spec = dict(
        src=dict(type='path'),
        lines=dict(aliases=['commands'], type='list'),
        parents=dict(type='list'),
        before=dict(type='list'),
        after=dict(type='list'),
        match=dict(default='line', choices=['line', 'strict', 'exact',
                                            'none']),
        replace=dict(default='line', choices=['line', 'block']),
        backup_options=dict(type='dict', options=backup_spec),
        config=dict(),
        defaults=dict(type='bool', default=False),
        passwords=dict(type='bool', default=False),
        backup=dict(type='bool', default=False),
        save=dict(type='bool', default=False),
    )

    argument_spec.update(asa_argument_spec)

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

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

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

    result = {'changed': False}

    check_args(module)

    config = None

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

    run(module, result)

    module.exit_json(**result)
Exemple #7
0
def run(module, result):
    match = module.params['match']
    replace = module.params['replace']
    path = module.params['parents']

    candidate = get_candidate(module)
    if match != 'none':
        contents = module.params['config']
        if not contents:
            contents = get_config(module)
        config = NetworkConfig(indent=1, contents=contents)
        configobjs = candidate.difference(config,
                                          path=path,
                                          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['updates'] = commands

        # send the configuration commands to the device and merge
        # them with the current running config
        if not module.check_mode:
            load_config(module, commands)
        result['changed'] = True

    if module.params['save']:
        if not module.check_mode:
            run_commands(module, 'write mem')
        result['changed'] = True
Exemple #8
0
def run(module, result):
    match = module.params["match"]
    replace = module.params["replace"]
    path = module.params["parents"]

    candidate = get_candidate(module)
    if match != "none":
        contents = module.params["config"]
        if not contents:
            contents = get_config(module)
        config = NetworkConfig(indent=1, contents=contents)
        configobjs = candidate.difference(config,
                                          path=path,
                                          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["updates"] = commands

        # send the configuration commands to the device and merge
        # them with the current running config
        if not module.check_mode:
            load_config(module, commands)
        result["changed"] = True

    if module.params["save"]:
        if not module.check_mode:
            run_commands(module, "write mem")
        result["changed"] = True
Exemple #9
0
def map_config_to_obj(module):

    obj = list()
    obj_dict = dict()

    group_type = module.params['group_type']
    group_name = module.params['name']
    protocol = module.params['protocol']

    sh_run_group_name = get_config(
        module, flags=['object-group | include {0}'.format(group_name)])
    run_group_name = Parser(sh_run_group_name, protocol).parse_obj_grp_name()

    obj_dict['have_name'] = run_group_name

    if run_group_name:
        if run_group_name[0] is not False:
            obj_dict['have_group_type'] = "port-object"
            obj_dict['have_protocol'] = run_group_name[0]
        elif 'network' in run_group_name[2]:
            obj_dict['have_group_type'] = "network-object"
        elif 'service' in run_group_name[2] and run_group_name[0] is False:
            obj_dict['have_group_type'] = "service-object"
        else:
            obj_dict['have_group_type'] = None

    sh_run_group_type = get_config(
        module, flags=['object-group id {0}'.format(group_name)])

    have_description = Parser(sh_run_group_type, protocol).parse_description()
    obj_dict['have_description'] = have_description

    have_host_ip = Parser(sh_run_group_type, protocol).parse_host()
    obj_dict['have_host_ip'] = have_host_ip

    have_group_object = Parser(sh_run_group_type,
                               protocol).parse_group_object()
    obj_dict['have_group_object'] = have_group_object

    have_ip_mask = Parser(sh_run_group_type, protocol).parse_address()
    obj_dict['have_ip_mask'] = have_ip_mask

    have_port_range = Parser(sh_run_group_type, protocol).parse_port_range()
    obj_dict['have_port_range'] = have_port_range

    have_port_eq = Parser(sh_run_group_type, protocol).parse_port_eq()
    obj_dict['have_port_eq'] = have_port_eq

    have_service_cfg = Parser(sh_run_group_type, protocol).parse_service_cfg()

    if have_service_cfg:
        have_lines = list()
        for i in have_service_cfg:
            have_lines.append(i.rstrip(' '))
        obj_dict['have_service_cfg'] = have_lines
    elif have_service_cfg is None:
        obj_dict['have_service_cfg'] = have_service_cfg

    obj.append(obj_dict)

    return obj