Exemplo n.º 1
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            host=dict(required=True),
            version=dict(required=True, choices=['2c', '3']),
            community=dict(required=False, default=False),
            username=dict(required=False),
            level=dict(required=False, choices=['authNoPriv', 'authPriv']),
            integrity=dict(required=False, choices=['md5', 'sha']),
            privacy=dict(required=False, choices=['des', '3des', 'aes', 'aes192', 'aes256']),
            authkey=dict(required=False),
            privkey=dict(required=False),
            interface_id=dict(required=False),
            interface_name=dict(required=False),
            portsecurity=dict(required=False, choices=['enabled', 'disabled']),
            max=dict(required=False),
            sticky=dict(required=False, choices=['enabled', 'disabled']),
            violation=dict(required=False, choices=['shutdown', 'restrict', 'protect']),
            aging_type=dict(required=False, choices=['absolute', 'inactivity']),
            aging_time=dict(required=False),
            aging_static=dict(required=False, choices=['enabled', 'disabled']),
            removeplaceholder=dict(required=False),
        ),
        mutually_exclusive=(['interface_id', 'interface_name'],),
        required_one_of=(
            ['interface_id', 'interface_name'],
            ['portsecurity', 'max', 'sticky', 'violation', 'aging_type','aging_time'],
        ),
        required_together=(
            ['username','level','integrity','authkey'],['privacy','privkey'],
        ),
        supports_check_mode=False)

    m_args = module.params

    if not has_nelsnmp:
        module.fail_json(msg='Missing required nelsnmp module (check docs)')

    # Verify that we receive a community when using snmp v2
    if m_args['version'] == "2c":
        if m_args['community'] == False:
            module.fail_json(msg='Community not set when using snmp version 2')

    if m_args['version'] == "3":
        if m_args['username'] == None:
            module.fail_json(msg='Username not set when using snmp version 3')

        if m_args['level'] == "authPriv" and m_args['privacy'] == None:
            module.fail_json(msg='Privacy algorithm not set when using authPriv')

    nelsnmp_args = {}
    for key in m_args:
        if key in NELSNMP_PARAMETERS and m_args[key] != None:
            nelsnmp_args[key] = m_args[key]

    try:
        dev = SnmpHandler(**nelsnmp_args)
    except Exception as err:
        module.fail_json(msg=str(err))

    has_changed = False

    if m_args['interface_name']:
        # Do this through cache in the future
        try:
            interface = False
            vartable = dev.getnext(o.ifDescr)

            for varbinds in vartable:
                for oid, val in varbinds:
                    if m_args['interface_name'] == val:
                        interface = oid.rsplit('.', 1)[-1]

            if interface == False:
                module.fail_json(msg='Unable to find interface')
        except Exception as err:
            module.fail_json(msg=str(err))

    # Check how to get the interface value
    if m_args['interface_id']:
        interface = m_args['interface_id']

    if m_args['portsecurity']:
        oid = o.cpsIfPortSecurityEnable + "." + str(interface)
        desired_state = TRUTHVALUE[m_args['portsecurity']]
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    if m_args['max']:
        oid = o.cpsIfMaxSecureMacAddr + "." + str(interface)
        desired_state = int(m_args['max'])
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    if m_args['sticky']:
        oid = o.cpsIfStickyEnable + "." + str(interface)
        desired_state = TRUTHVALUE[m_args['sticky']]
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    if m_args['violation']:
        oid = o.cpsIfViolationAction + "." + str(interface)
        desired_state = VIOLATION[m_args['violation']]
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    if m_args['aging_type']:
        oid = o.cpsIfSecureMacAddrAgingType + "." + str(interface)
        desired_state = AGING_TYPE[m_args['aging_type']]
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    if m_args['aging_time']:
        oid = o.cpsIfSecureMacAddrAgingTime + "." + str(interface)
        desired_state = int(m_args['aging_time'])
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    if m_args['aging_static']:
        oid = o.cpsIfStaticMacAddrAgingEnable + "." + str(interface)
        desired_state = TRUTHVALUE[m_args['aging_static']]
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    return_status = { 'changed': has_changed }

    module.exit_json(**return_status)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            host=dict(required=True),
            version=dict(required=True, choices=['2c', '3']),
            community=dict(required=False, default=False),
            username=dict(required=False),
            level=dict(required=False, choices=['authNoPriv', 'authPriv']),
            integrity=dict(required=False, choices=['md5', 'sha']),
            privacy=dict(required=False,
                         choices=['des', '3des', 'aes', 'aes192', 'aes256']),
            authkey=dict(required=False),
            privkey=dict(required=False),
            mode=dict(required=True,
                      choices=[
                          'access', 'trunk', 'desireable', 'auto',
                          'trunk-nonegotiate'
                      ]),
            interface_id=dict(required=False),
            interface_name=dict(required=False),
            access_vlan=dict(required=False),
            native_vlan=dict(required=False),
            removeplaceholder=dict(required=False),
        ),
        mutually_exclusive=(['interface_id', 'interface_name'], ),
        required_one_of=(['interface_id', 'interface_name'], ),
        required_together=(
            ['username', 'level', 'integrity', 'authkey'],
            ['privacy', 'privkey'],
        ),
        supports_check_mode=False)

    m_args = module.params

    if not has_nelsnmp:
        module.fail_json(msg='Missing required nelsnmp module (check docs)')

    # Verify that we receive a community when using snmp v2
    if m_args['version'] == "2c":
        if m_args['community'] == False:
            module.fail_json(msg='Community not set when using snmp version 2')

    if m_args['version'] == "3":
        if m_args['username'] == None:
            module.fail_json(msg='Username not set when using snmp version 3')

        if m_args['level'] == "authPriv" and m_args['privacy'] == None:
            module.fail_json(
                msg='Privacy algorithm not set when using authPriv')

    nelsnmp_args = {}
    for key in m_args:
        if key in NELSNMP_PARAMETERS and m_args[key] != None:
            nelsnmp_args[key] = m_args[key]

    try:
        dev = SnmpHandler(**nelsnmp_args)
    except Exception as err:
        module.fail_json(msg=str(err))

    #return_status = { 'changed': False }
    has_changed = False

    if m_args['interface_name']:
        # Do this through cache in the future
        try:
            interface = False
            vartable = dev.getnext(o.ifDescr)

            for varbinds in vartable:
                for oid, val in varbinds:
                    if m_args['interface_name'] == val:
                        interface = oid.rsplit('.', 1)[-1]

            if interface == False:
                module.fail_json(msg='Unable to find interface')
        except Exception as err:
            module.fail_json(msg=str(err))

    # Check how to get the interface value
    if m_args['interface_id']:
        interface = m_args['interface_id']

    if m_args['mode']:
        oid = o.vlanTrunkPortDynamicState + "." + str(interface)
        desired_state = PORT_MODE[m_args['mode']]
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    if m_args['access_vlan']:
        oid = o.vmVlan + "." + str(interface)
        desired_state = int(m_args['access_vlan'])
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    if m_args['native_vlan']:
        oid = o.vlanTrunkPortNativeVlan + "." + str(interface)
        desired_state = int(m_args['native_vlan'])
        changed = set_state(dev, oid, desired_state, module)
        has_changed = changed_status(changed, has_changed)

    return_status = {'changed': has_changed}

    module.exit_json(**return_status)
Exemplo n.º 3
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            host=dict(required=True),
            version=dict(required=True, choices=['2c', '3']),
            community=dict(required=False, default=False),
            username=dict(required=False),
            level=dict(required=False, choices=['authNoPriv', 'authPriv']),
            integrity=dict(required=False, choices=['md5', 'sha']),
            port=dict(required=False, default=161, type='int'),
            privacy=dict(required=False,
                         choices=['des', '3des', 'aes', 'aes192', 'aes256']),
            authkey=dict(required=False),
            privkey=dict(required=False),
            oid=dict(required=True, type='list'),
            query_type=dict(choices=['get', 'getnext'], default='get')),
        required_together=(['username', 'level', 'integrity',
                            'authkey'], ['privacy', 'privkey']),
        supports_check_mode=False)

    m_args = module.params

    if not has_nelsnmp:
        module.fail_json(msg='Missing required nelsnmp module (check docs)')

    # Verify that we receive a community when using snmp v2
    if m_args['version'] == "2c":
        if m_args['community'] is False:
            module.fail_json(msg='Community not set when using snmp version 2')

    if m_args['version'] == "3":
        if m_args['username'] is None:
            module.fail_json(msg='Username not set when using snmp version 3')

        if m_args['level'] == "authPriv" and m_args['privacy'] is None:
            module.fail_json(
                msg='Privacy algorithm not set when using authPriv')

    nelsnmp_args = {}
    for key in m_args:
        if key in NELSNMP_PARAMETERS and m_args[key] is not None:
            nelsnmp_args[key] = m_args[key]

    try:
        dev = SnmpHandler(**nelsnmp_args)
    except Exception as err:
        module.fail_json(msg=str(err))

    results = {}

    if m_args['query_type'] == 'get':
        for oid in m_args['oid']:
            results[oid] = None
        try:
            varbinds = dev.get(*m_args['oid'])
        except Exception as err:
            module.fail_json(msg=str(err))
        for oid, value in varbinds:
            for desired_oid in m_args['oid']:
                if desired_oid in oid:
                    if isinstance(value, NoSuchObject):
                        results[desired_oid] = None
                    else:
                        results[desired_oid] = value
    else:
        try:
            vartable = dev.getnext(*m_args['oid'])
        except Exception as err:
            module.fail_json(msg=str(err))
        for varbinds in vartable:
            for oid, value in varbinds:
                results[oid] = value

    module.exit_json(**results)
Exemplo n.º 4
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            host=dict(required=True),
            version=dict(required=True, choices=['2c', '3']),
            community=dict(required=False, default=False),
            username=dict(required=False),
            level=dict(required=False, choices=['authNoPriv', 'authPriv']),
            integrity=dict(required=False, choices=['md5', 'sha']),
            privacy=dict(required=False, choices=['des', '3des', 'aes', 'aes192', 'aes256']),
            authkey=dict(required=False),
            privkey=dict(required=False),
            state=dict(required=True, choices=['absent', 'present']),
            vlan_id=dict(required=True),
            vlan_name=dict(required=False),
            removeplaceholder=dict(required=False)),
            required_together = ( ['username','level','integrity','authkey'],['privacy','privkey'],),
        supports_check_mode=False)

    m_args = module.params

    if not has_nelsnmp:
        module.fail_json(msg='Missing required nelsnmp module (check docs)')

    # Verify that we receive a community when using snmp v2
    if m_args['version'] == "2c":
        if m_args['community'] == False:
            module.fail_json(msg='Community not set when using snmp version 2')

    if m_args['version'] == "3":
        if m_args['username'] == None:
            module.fail_json(msg='Username not set when using snmp version 3')

        if m_args['level'] == "authPriv" and m_args['privacy'] == None:
            module.fail_json(msg='Privacy algorithm not set when using authPriv')

    nelsnmp_args = {}
    for key in m_args:
        if key in NELSNMP_PARAMETERS and m_args[key] != None:
            nelsnmp_args[key] = m_args[key]

    try:
        dev = SnmpHandler(**nelsnmp_args)
    except Exception as err:
        module.fail_json(msg=str(err))

    changed_false = { 'changed': False }
    changed_true = { 'changed': True }

    vlan_defined_name = False

    oids = []
    oids.append(o.vtpVlanState)
    if m_args['vlan_name']:
        oids.append(o.vtpVlanName)
        vlan_defined_name = m_args['vlan_name']
    exists_vlan_id = False
    exists_vlan_name = False
    try:
        vartable = dev.getnext(*oids)
    except Exception as err:
        module.fail_json(msg=str(err))

    for varbinds in vartable:
        for oid, val in varbinds:

            if o.vtpVlanState in oid:
                vlan_id = oid.rsplit('.', 1)[-1]
                if vlan_id == m_args['vlan_id']:
                    exists_vlan_id = True
            if o.vtpVlanName in oid:
                vlan_id = oid.rsplit('.', 1)[-1]
                if vlan_id == m_args['vlan_id']:
                    if m_args['vlan_name'] == val:
                        exists_vlan_name = True

    return_status = changed_true


    if m_args['state'] == "present":
        if m_args['vlan_name'] and exists_vlan_name:
            return_status = changed_false
            desired_state = True
        elif m_args['vlan_name'] and not exists_vlan_name:
            desired_state = False
        elif exists_vlan_id:
            return_status = changed_false
            desired_state = True
        else:
            desired_state = False




    if m_args['state'] == "absent":
        if exists_vlan_id:
            desired_state = False
        else:
            desired_state = True
            return_status = changed_false

    if not desired_state:
        vartable = dev.getnext(o.vtpVlanEditTable)
        if len(vartable) > 0:
            module.fail_json(msg='Other changes are being made to the vlan database')

        if m_args['state'] == "present":
            # Create vlan
            create_vlan(dev,m_args['vlan_id'],vlan_defined_name, module)
        else:
            # Remove vlan
            delete_vlan(dev, m_args['vlan_id'],module)



    module.exit_json(**return_status)