Esempio n. 1
0
def map_config_to_obj(module):
    objs = []
    output = run_commands(module, {'command': 'show vrf'})
    if output is not None:
        vrfText = output[0].strip()
        vrfList = vrfText.split('VRF')
        for vrfItem in vrfList:
            if 'FIB ID' in vrfItem:
                obj = dict()
                list_of_words = vrfItem.split()
                vrfName = list_of_words[0]
                obj['name'] = vrfName[:-1]
                obj['rd'] = list_of_words[list_of_words.index('RD') + 1]
                start = False
                obj['interfaces'] = []
                for intName in list_of_words:
                    if 'Interfaces' in intName:
                        start = True
                    if start is True:
                        if '!' not in intName and 'Interfaces' not in intName:
                            obj['interfaces'].append(intName.strip().lower())
                objs.append(obj)
    else:
        module.fail_json(msg='Could not fetch VRF details from device')
    return objs
def get_switchport(name, module):
    config = run_commands(module,
                          ['show interface {0} switchport'.format(name)])[0]
    mode = re.search(r'Switchport mode         : (?:.* )?(\w+)$', config, re.M)
    access = re.search(r'Configured Vlans        : (\d+)', config)
    native = re.search(r'Default/Native Vlan     : (\d+)', config)
    trunk = re.search(r'Enabled Vlans           : (.+)$', config, re.M)
    if mode:
        mode = mode.group(1)
    if access:
        access = access.group(1)
    if native:
        native = native.group(1)
    if trunk:
        trunk = trunk.group(1)
    if trunk == 'ALL':
        trunk = '1-4094'

    switchport_config = {
        "interface": name,
        "mode": mode,
        "access_vlan": access,
        "native_vlan": native,
        "trunk_vlans": trunk,
    }

    return switchport_config
def interface_is_portchannel(name, module):
    if get_interface_type(name) == 'ethernet':
        config = run_commands(module,
                              ['show run interface {0}'.format(name)])[0]
        if any(c in config for c in ['channel group', 'channel-group']):
            return True
    return False
Esempio n. 4
0
def is_switchport(name, module):
    intf_type = get_interface_type(name)

    if intf_type in ('ethernet', 'portchannel'):
        config = run_commands(
            module, ['show interface {0} switchport'.format(name)])[0]
        match = re.search(r'Switchport              : enabled', config)
        return bool(match)
    return False
def get_ethernet_range(module):
    output = run_commands(module, ['show interface brief'])[0].split('\n')
    maxport = None
    last_interface = None
    for line in output:
        if line.startswith('Ethernet1/'):
            last_interface = line.split(' ')[0]
    if last_interface is not None:
        eths = last_interface.split('/')
        maxport = eths[1]
    return maxport
Esempio n. 6
0
def main():
    spec = dict(
        # { command: <str>, prompt: <str>, response: <str> }
        commands=dict(type='list', required=True),

        wait_for=dict(type='list'),
        match=dict(default='all', choices=['all', 'any']),

        retries=dict(default=10, type='int'),
        interval=dict(default=1, type='int')
    )

    module = AnsibleModule(argument_spec=spec, supports_check_mode=True)
    result = {'changed': False}

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

    commands = module.params['commands']
    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 been satisfied'
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    result.update({
        'changed': False,
        'stdout': responses,
        'stdout_lines': list(to_lines(responses))
    })

    module.exit_json(**result)
def get_list_of_vlans(module):
    config = run_commands(module, ['show vlan'])[0]
    vlans = set()

    lines = config.strip().splitlines()
    for line in lines:
        line_parts = line.split()
        if line_parts:
            try:
                int(line_parts[0])
            except ValueError:
                continue
            vlans.add(line_parts[0])

    return list(vlans)
Esempio n. 8
0
def map_config_to_obj(module):
    out = run_commands(module, ['show user-account'])
    data = out[0]
    objects = list()
    datum = data.split('User')

    for item in datum:
        objects.append({
            'name': parse_username(item),
            'configured_password': parse_password(item),
            'sshkey': parse_sshkey(item),
            'roles': parse_roles(item),
            'state': 'present'
        })
    return objects
def map_config_to_obj(module):
    return parse_vlan_brief(run_commands(module, ['show vlan brief'])[0])
Esempio n. 10
0
 def run(self, cmd):
     return run_commands(self.module, cmd, check_rc=False)
Esempio n. 11
0
 def populate(self):
     self.responses = run_commands(self.module,
                                   self.COMMANDS,
                                   check_rc=False)