コード例 #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']),
            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)
コード例 #2
0
def test_return_value_types(query_data):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    varbinds = dev.get('1')
    for oid, value in varbinds:
        assert value == query_data.value
コード例 #3
0
def test_return_value_types(query_data):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    varbinds = dev.get('1')
    for oid, value in varbinds:
        assert value == query_data.value