def map_config_to_obj(module):
    obj = []
    output = run_commands(module, ['show interfaces ethernet'])
    lines = re.split(r'\ne', output[0])[1:]

    if len(lines) > 0:
        for line in lines:
            splitted_line = line.split()

            if len(splitted_line) > 0:
                ipv4 = []
                ipv6 = []

                if splitted_line[0].lower().startswith('th'):
                    name = 'e' + splitted_line[0].lower()

                for i in splitted_line[1:]:
                    if (('.' in i or ':' in i) and '/' in i):
                        value = i.split(r'\n')[0]
                        if is_ipv4(value):
                            ipv4.append(value)
                        elif is_ipv6(value):
                            ipv6.append(value)

                obj.append({'name': name, 'ipv4': ipv4, 'ipv6': ipv6})

    return obj
Exemple #2
0
def map_config_to_obj(module):
    obj = []
    output = run_commands(module, ['show interfaces ethernet'])
    lines = output[0].splitlines()

    if len(lines) > 3:
        for line in lines[3:]:
            splitted_line = line.split()

            if len(splitted_line) > 1:
                name = splitted_line[0]
                address = splitted_line[1]

                if address == '-':
                    address = None

                if address is not None and ':' not in address:
                    obj.append({'name': name,
                                'ipv4': address,
                                'ipv6': None})
                else:
                    obj.append({'name': name,
                                'ipv6': address,
                                'ipv4': None})
            else:
                obj[-1]['ipv6'] = splitted_line[0]

    return obj
def map_config_to_obj(module):
    objs = []
    interfaces = list()

    output = run_commands(module, 'show interfaces')
    lines = output[0].strip().splitlines()[3:]

    for l in lines:
        splitted_line = re.split(r'\s{2,}', l.strip())
        obj = {}

        eth = splitted_line[0].strip("'")
        if eth.startswith('eth'):
            obj['interfaces'] = []
            if '.' in eth:
                interface = eth.split('.')[0]
                obj['interfaces'].append(interface)
                obj['vlan_id'] = eth.split('.')[-1]
            else:
                obj['interfaces'].append(eth)
                obj['vlan_id'] = None

            if splitted_line[1].strip("'") != '-':
                obj['address'] = splitted_line[1].strip("'")

            if len(splitted_line) > 3:
                obj['name'] = splitted_line[3].strip("'")
            obj['state'] = 'present'
            objs.append(obj)

    return objs
Exemple #4
0
def map_config_to_obj(module):
    obj = []
    output = run_commands(module, ['show interfaces bonding slaves'])
    lines = output[0].splitlines()

    if len(lines) > 1:
        for line in lines[1:]:
            splitted_line = line.split()

            name = splitted_line[0]
            mode = splitted_line[1]
            state = splitted_line[2]

            if len(splitted_line) > 4:
                members = splitted_line[4:]
            else:
                members = []

            obj.append({
                'name': name,
                'mode': mode,
                'members': members,
                'state': state
            })

    return obj
Exemple #5
0
def map_config_to_obj(module):
    obj = []
    output = run_commands(module, ['show interfaces ethernet'])
    lines = output[0].splitlines()

    if len(lines) > 3:
        for line in lines[3:]:
            splitted_line = line.split()

            if len(splitted_line) > 1:
                name = splitted_line[0]
                address = splitted_line[1]

                if address == '-':
                    address = None

                if address is not None and ':' not in address:
                    obj.append({'name': name,
                                'ipv4': address,
                                'ipv6': None})
                else:
                    obj.append({'name': name,
                                'ipv6': address,
                                'ipv4': None})
            else:
                obj[-1]['ipv6'] = splitted_line[0]

    return obj
Exemple #6
0
def main():
    argument_spec = dict(
        src=dict(type='path'),
        lines=dict(type='list'),

        match=dict(default='line', choices=['line', 'none']),

        comment=dict(default=DEFAULT_COMMENT),

        config=dict(),

        backup=dict(type='bool', default=False),
        save=dict(type='bool', default=False),
    )

    argument_spec.update(vyos_argument_spec)

    mutually_exclusive = [('lines', 'src')]

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

    warnings = list()
    check_args(module, warnings)

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

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

    if any((module.params['src'], module.params['lines'])):
        run(module, result)

    if module.params['save']:
        diff = run_commands(module, commands=['configure', 'compare saved'])[1]
        if diff != '[edit]':
            run_commands(module, commands=['save'])
            result['changed'] = True
        run_commands(module, commands=['exit'])

    module.exit_json(**result)
Exemple #7
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(vyos_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()
    try:
        conditionals = [Conditional(c) for c in wait_for]
    except AttributeError:
        exc = get_exception()
        module.fail_json(msg=str(exc))

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

    for _ in range(retries):
        responses = run_commands(module, commands)

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

            if not conditionals:
                break

            time.sleep(interval)

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

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

    module.exit_json(**result)
Exemple #8
0
def main():
    argument_spec = dict(
        src=dict(type='path'),
        lines=dict(type='list'),

        match=dict(default='line', choices=['line', 'none']),

        comment=dict(default=DEFAULT_COMMENT),

        config=dict(),

        backup=dict(type='bool', default=False),
        save=dict(type='bool', default=False),
    )

    argument_spec.update(vyos_argument_spec)

    mutually_exclusive = [('lines', 'src')]

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

    warnings = list()
    check_args(module, warnings)

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

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

    if any((module.params['src'], module.params['lines'])):
        run(module, result)

    if module.params['save']:
        if not module.check_mode:
            run_commands(module, ['save'])
        result['changed'] = True

    module.exit_json(**result)
Exemple #9
0
def map_config_to_obj(module):
    obj = []
    output = run_commands(module, ['show interfaces bonding slaves'])
    lines = output[0].splitlines()

    if len(lines) > 1:
        for line in lines[1:]:
            splitted_line = line.split()

            name = splitted_line[0]
            mode = splitted_line[1]
            state = splitted_line[2]

            if len(splitted_line) > 4:
                members = splitted_line[4:]
            else:
                members = []

            obj.append({'name': name,
                        'mode': mode,
                        'members': members,
                        'state': state})

    return obj
Exemple #10
0
 def populate(self):
     self.responses = run_commands(self.module, list(self.COMMANDS))
Exemple #11
0
 def populate(self):
     self.responses = run_commands(self.module, list(self.COMMANDS))