Пример #1
0
def load_running_config(running):
    running = running.strip().split('\n')
    running_obj = NetworkConfig(indent=4)

    # Transform running to running_obj
    index = 0
    while index < len(running):
        # If line is empty, ignore it
        if running[index] == '':
            index += 1
            continue
        parents = to_parents(running[index])
        children = list()
        # If this line is parents, find it's children
        if is_parents(running[index]):
            index += 1
            while index < len(running) and running[
                    index] != 'exit' and running[index] != '':
                children.append(running[index])
                index += 1
            if index < len(running) and running[index] != '':
                children.append(running[index])
        # Add parents and children into running_obj
        running_obj.add(children, parents)
        index += 1

    return running_obj
Пример #2
0
def main():

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

    argument_spec.update(asa_argument_spec)

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

    lines = module.params["lines"]

    result = {"changed": False}
    if len(lines) > 0:
        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)
            if commands and module.params["replace"] == "block":
                commands = str(candidate).split("\n")
            else:
                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)
Пример #3
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
Пример #4
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
Пример #5
0
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
Пример #6
0
def get_candidate_config(module):
    candidate = ''
    if module.params['src']:
        candidate = module.params['src']
    elif module.params['lines']:
        candidate_obj = NetworkConfig(indent=1)
        candidate_obj.add(module.params['lines'])
        candidate = dumps(candidate_obj, 'raw')
    return candidate
Пример #7
0
def get_candidate_config(module):
    candidate = ""
    if module.params["src"]:
        candidate = module.params["src"]
    elif module.params["lines"]:
        candidate_obj = NetworkConfig(indent=1)
        parents = module.params["parents"] or list()
        candidate_obj.add(module.params["lines"], parents=parents)
        candidate = dumps(candidate_obj, "raw")
    return candidate
def get_candidate(module):
    candidate = ''
    if module.params['src']:
        candidate = module.params['src']
    elif module.params['lines']:
        candidate_obj = NetworkConfig(indent=3)
        parents = module.params['parents'] or list()
        candidate_obj.add(module.params['lines'], parents=parents)
        candidate = dumps(candidate_obj, 'raw')
    return candidate
Пример #9
0
    def get_candidate(self):
        candidate = ''
        if self.want.src:
            candidate = self.want.src

        elif self.want.lines:
            candidate_obj = NetworkConfig(indent=1)
            parents = self.want.parents or list()
            candidate_obj.add(self.want.lines, parents=parents)
            candidate = dumps(candidate_obj, 'raw')
        return candidate
Пример #10
0
def get_sublevel_config(running_config, module):
    contents = list()
    current_config_contents = list()
    sublevel_config = NetworkConfig(indent=0)
    obj = running_config.get_object(module.params['parents'])
    if obj:
        contents = obj._children
    for c in contents:
        if isinstance(c, ConfigLine):
            current_config_contents.append(c.raw)
    sublevel_config.add(current_config_contents, module.params['parents'])
    return sublevel_config
Пример #11
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}
    if len(lines) > 0:
        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)
Пример #12
0
def get_candidate(module):
    candidate = NetworkConfig(indent=1)
    banners = {}

    if module.params['src']:
        src, banners = extract_banners(module.params['src'])
        candidate.load(src)

    elif module.params['lines']:
        parents = module.params['parents'] or list()
        candidate.add(module.params['lines'], parents=parents)

    return candidate, banners
Пример #13
0
def get_candidate_config(module):
    """ gets the set of commands to configure """
    candidate = ""
    candidate_obj = NetworkConfig(indent=1)
    if module.params["src"]:
        candidate_obj.loadfp(module.params["src"])

    elif module.params["lines"]:
        parents = module.params["parents"] or list()
        candidate_obj.add(module.params["lines"], parents=parents)

    candidate = dumps(candidate_obj, "raw")
    return candidate
def get_candidate(module):
    candidate = ""
    if module.params["src"]:
        if module.params["replace"] != "config":
            candidate = module.params["src"]
    if module.params["replace"] == "config":
        candidate = "config replace {0}".format(module.params["replace_src"])
    elif module.params["lines"]:
        candidate_obj = NetworkConfig(indent=2)
        parents = module.params["parents"] or list()
        candidate_obj.add(module.params["lines"], parents=parents)
        candidate = dumps(candidate_obj, "raw")
    return candidate
Пример #15
0
def get_candidate(module):
    candidate = ''
    if module.params['src']:
        if module.params['replace'] != 'config':
            candidate = module.params['src']
    if module.params['replace'] == 'config':
        candidate = 'config replace {0}'.format(module.params['replace_src'])
    elif module.params['lines']:
        candidate_obj = NetworkConfig(indent=2)
        parents = module.params['parents'] or list()
        candidate_obj.add(module.params['lines'], parents=parents)
        candidate = dumps(candidate_obj, 'raw')
    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()
        commands = module.params['lines'][0]
        if (isinstance(commands, dict)) and (isinstance(commands['command'], list)):
            candidate.add(commands['command'], parents=parents)
        elif (isinstance(commands, dict)) and (isinstance(commands['command'], str)):
            candidate.add([commands['command']], parents=parents)
        else:
            candidate.add(module.params['lines'], parents=parents)
    return candidate