def main():
    argument_spec = dict(
        ip=dict(type='str', required=True, aliases=['hostname']),
        username=dict(type='str', required=True),
        password=dict(type='str', required=True, no_log=True),
        port=dict(type='str'),
        secure=dict(type='str'),
        proxy=dict(type='str'),
        objects=dict(type='list'),
        json_config_file=dict(type='str'),
        state=dict(type='str',
                   choices=['present', 'absent'],
                   default='present'),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_one_of=[
            ['objects', 'json_config_file'],
        ],
        mutually_exclusive=[
            ['objects', 'json_config_file'],
        ],
    )
    imc = ImcConnection(module)
    imc.result = {}
    imc.login()

    err = False
    # note that all objects specified in the object list report a single result (including a single changed).
    imc.result['changed'] = False
    try:
        if module.params.get('objects'):
            objects = module.params['objects']
        else:
            # either objects or json_config_file will be specified, so if there is no objects option use a config file
            with open(module.params['json_config_file']) as f:
                objects = json.load(f)['objects']

        for managed_object in objects:
            traverse_objects(module, imc, managed_object)

    except Exception as e:
        err = True
        imc.result['msg'] = "setup error: %s " % str(e)

    finally:
        imc.logout()

    if err:
        module.fail_json(**imc.result)
    module.exit_json(**imc.result)
Esempio n. 2
0
def main():
    argument_spec = dict(
        ip=dict(type='str', required=True, aliases=['hostname']),
        username=dict(type='str', required=True),
        password=dict(type='str', required=True, no_log=True),
        port=dict(type='str'),
        secure=dict(type='str'),
        proxy=dict(type='str'),
        class_ids=dict(type='str'),
        distinguished_names=dict(type='str'),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        mutually_exclusive=[
            ['class_ids', 'distinguished_names'],
        ],
    )

    # imcModule verifies imcmsdk is present and exits on failure.
    # Imports are below for imc object creation.
    imc = ImcConnection(module)
    imc.result = {}
    imc.login()

    query_result = {}
    import epdb
    # epdb.serve()
    try:
        if module.params['class_ids']:
            class_ids = [
                x.strip() for x in module.params['class_ids'].split(',')
            ]
            for class_id in class_ids:
                query_result[class_id] = []
                imc_mos = retrieve_class_id(class_id, imc)
                if imc_mos:
                    for imc_mo in imc_mos:
                        query_result[class_id].append(make_mo_dict(imc_mo))

            imc.result['objects'] = query_result

        elif module.params['distinguished_names']:
            distinguished_names = [
                x.strip()
                for x in module.params['distinguished_names'].split(',')
            ]
            for distinguished_name in distinguished_names:
                query_result[distinguished_name] = {}
                imc_mo = retrieve_distinguished_name(distinguished_name, imc)

                if imc_mo:
                    query_result[distinguished_name] = make_mo_dict(imc_mo)

            imc.result['objects'] = query_result

    except Exception as e:
        imc.result['msg'] = "setup error: %s " % str(e)
        module.fail_json(**imc.result)

    finally:
        imc.logout()

    module.exit_json(**imc.result)