示例#1
0
def main():
    object_dict = dict(
        module=dict(type='str', required=True),
        class_name=dict(type='str', aliases=['class'], required=True),
        properties=dict(type='dict', required=True),
        children=dict(type='list'),
    )
    argument_spec = ucs_argument_spec
    argument_spec.update(
        objects=dict(type='list', elements='dict', options=object_dict, required=True),
        state=dict(type='str', choices=['present', 'absent'], default='present'),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )

    if not HAS_IMPORT_MODULE:
        module.fail_json(msg='import_module is required for this module')
    ucs = UCSModule(module)

    ucs.result['err'] = False
    # note that all objects specified in the object list report a single result (including a single changed).
    ucs.result['changed'] = False

    for managed_object in module.params['objects']:
        traverse_objects(module, ucs, managed_object)

    if ucs.result['err']:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#2
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        class_ids=dict(type='str'),
        distinguished_names=dict(type='str'),
        delegate_to=dict(type='str', default='localhost'),
    )

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

    # UCSModule verifies ucsmsdk is present and exits on failure.
    # Imports are below for UCS object creation.
    ucs = UCSModule(module)
    err = False
    query_result = {}

    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] = []
                ucs_mos = retrieve_class_id(class_id, ucs)
                if ucs_mos:
                    for ucs_mo in ucs_mos:
                        query_result[class_id].append(make_mo_dict(ucs_mo))

            ucs.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] = {}
                ucs_mo = retrieve_distinguished_name(distinguished_name, ucs)

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

            ucs.result['objects'] = query_result

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

    if err:
        module.fail_json(**ucs.result)

    module.exit_json(**ucs.result)
示例#3
0
def main():
    vnic = dict(
        name=dict(type='str', required=True),
        vnic_template=dict(type='str', required=True),
        adapter_policy=dict(type='str', default=''),
        order=dict(type='str', default='unspecified'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )
    iscsi_vnic = dict(
        name=dict(type='str', required=True),
        overlay_vnic=dict(type='str', default=''),
        iscsi_adapter_policy=dict(type='str', default=''),
        mac_address=dict(type='str', default='derived'),
        vlan_name=dict(type='str', default='default'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )

    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        description=dict(type='str', aliases=['descr'], default=''),
        vnic_list=dict(type='list', elements='dict', options=vnic),
        iscsi_vnic_list=dict(type='list', elements='dict', options=iscsi_vnic),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    ucs = UCSModule(module)
    # UCSModule creation above verifies ucsmsdk is present and exits on failure.
    # Additional imports are done below or in called functions.

    ucs.result['changed'] = False
    props_match = False
    # dn is <org_dn>/lan-conn-pol-<name>
    dn = module.params['org_dn'] + '/lan-conn-pol-' + module.params['name']

    mo = ucs.login_handle.query_dn(dn)
    if mo:
        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if not module.check_mode:
                ucs.login_handle.remove_mo(mo)
                ucs.login_handle.commit()
            ucs.result['changed'] = True
        else:  # state == 'present'
            props_match = check_lan_connecivity_props(ucs, module, mo, dn)

    if module.params['state'] == 'present' and not props_match:
        configure_lan_connectivity(ucs, module, dn)

    module.exit_json(**ucs.result)
def main():
    from ansible.module_utils.basic import AnsibleModule
    from ansible.module_utils.remote_management.ucs import UCSModule, ucs_argument_spec
    argument_spec = ucs_argument_spec
    argument_spec.update(
        sp_name=dict(required=True, type='str'),
        vnics=dict(required=True, type='list'),
        org_dn=dict(required=False, type='str', default='org-root'),
    )
    module = AnsibleModule(argument_spec, supports_check_mode=True)
    ucs = UCSModule(module)

    err = False
    changed = False

    try:
        sp_dn = dn = module.params['org_dn'] + "/ls-" + module.params['sp_name']
        sp = ucs.login_handle.query_dn(dn)
        if not sp:
            raise ValueError("SP '%s' does not exist" % sp_dn)

        for vnic in module.params['vnics']:
            vnic_mo = get_vnic(
                ucs, (get_vnic_dn(sp_dn, vnic['transport'], vnic['name'])))

            if vnic['state'] != 'absent' and not vnic_mo:
                raise ValueError(
                    "vNIC '%s' is not assigned to service profile '%s'" %
                    (vnic['name'], sp_dn))

            if vnic_mo:
                if not matches_existing_vnic_order(vnic, vnic_mo):
                    changed = True
                    break

        if changed and not module.check_mode:
            for vnic in module.params['vnics']:
                vnic_mo = get_vnic(
                    ucs, (get_vnic_dn(sp_dn, vnic['transport'], vnic['name'])))
                if vnic['state'] == 'absent' and vnic_mo:
                    remove_vnic_assignment_order(ucs, vnic, sp)
                elif not vnic_mo:

                    update_vnic_assignment_order(ucs, vnic, sp)
                elif not matches_existing_vnic_order(vnic, vnic_mo):
                    update_vnic_assignment_order(ucs, vnic, sp)

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#5
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(name=dict(type='str'),
                         descr=dict(type='str', default=''),
                         state=dict(type='str',
                                    default='present',
                                    choices=['present', 'absent']))

    module = AnsibleModule(argument_spec, supports_check_mode=True)
    ucs = UCSModule(module)

    from ucsmsdk.mometa.lstorage.LstorageProfile import LstorageProfile
    err = False
    changed = False
    mo_exists = False

    try:
        dn_base = 'org-root'
        mo = LstorageProfile(parent_mo_or_dn=dn_base,
                             name=module.params['name'],
                             descr=module.params['descr'])

        dn = dn_base + '/profile-' + module.params['name']
        existing_mo = ucs.login_handle.query_dn(dn)
        if existing_mo:
            # check top-level mo props
            kwargs = dict(descr=module.params['descr'])
            kwargs['name'] = module.params['name']
            if existing_mo.check_prop_match(**kwargs):
                mo_exists = True

        if module.params['state'] == 'absent':
            if mo_exists:
                if not module.check_mode:
                    # delete mo if dn already exist
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if not mo_exists:
                if not module.check_mode:
                    # create mo if dn does not already exist
                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#6
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(redundancy=dict(
        type='str',
        required=False,
        default='n+1',
        choices=['n+1', 'non-redundant', 'grid']), )

    module = AnsibleModule(argument_spec, )
    ucs = UCSModule(module)
    err = False
    ''' 
    print("********Inputs start********")
    print("redundancy=",module.params['redundancy'])
    print("********Inputs end*********")
    '''

    # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below.
    from ucsmsdk.mometa.compute.ComputePsuPolicy import ComputePsuPolicy
    changed = False
    try:
        props_match = False
        dn_base = 'org-root'
        dn = dn_base + '/psu-policy'
        mo = ucs.login_handle.query_dn(dn)
        #print(mo)
        kwargs = dict(redundancy=module.params['redundancy'])
        if (mo.check_prop_match(**kwargs)):
            props_match = True

        if not props_match:
            #print("mo props do not match")
            mo = ComputePsuPolicy(
                parent_mo_or_dn=dn_base,
                redundancy=module.params['redundancy'],
            )
            ucs.login_handle.add_mo(mo, True)
            ucs.login_handle.commit()
            changed = True

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

    ucs.result['changed'] = changed

    if err:
        module.fail_json(**ucs.result)

    module.exit_json(**ucs.result)
示例#7
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        style=dict(type='str', required=False, default='manual-per-blade', choices=['manual-per-blade', 'intelligent-policy-driven']),
    )
    
    module = AnsibleModule(argument_spec,)
    ucs = UCSModule(module)
    err = False 

    '''  
    print("********Inputs start********")
    print("style=",module.params['style'])
    print("********Inputs end*********")
    '''
    
    # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below.
    from ucsmsdk.mometa.power.PowerMgmtPolicy import PowerMgmtPolicy
    changed = False
    try:
        props_match = False
        dn_base = 'org-root'
        dn = dn_base+'/pwr-mgmt-policy'
        mo = ucs.login_handle.query_dn(dn)
        #print(mo)
        kwargs = dict(style=module.params['style'])
        if (mo.check_prop_match(**kwargs)):
            props_match = True

        if not props_match:
            #print("mo props do not match")
            mo = PowerMgmtPolicy(
                    parent_mo_or_dn=dn_base,
                    style=module.params['style'],
            )
            ucs.login_handle.add_mo(mo, True)
            ucs.login_handle.commit()
            changed = True

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

    ucs.result['changed'] = changed

    if err:
        module.fail_json(**ucs.result)

    module.exit_json(**ucs.result)
示例#8
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        fabric=dict(type='str', default='common', choices=['common', 'A', 'B']),
        pattern=dict(type='str'),
        vlanid=dict(type='str')
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_one_of=[['pattern', 'vlanid']]
    )

    ucs = UCSModule(module)

    filtls = ['(cloud,"ethlan")']
    if module.params['fabric'] != 'common':
        filtls.append('(switch_id,"' + module.params['fabric'] + '")')
    if module.params['vlanid']:
        filtls.append('(id,"' + module.params['vlanid'] + '")')
    else:
        filtls.append('(name,"' + module.params['pattern'] + '")')

    object_dict = ucs.login_handle.query_classid("fabricVlan", filter_str=' and '.join(filtls))

    if object_dict is None:
        module.fail_json(msg="Failed to query vlan objects")

    vlnlist = []
    for ob in object_dict:
        vlnlist.append(dict(name=ob.name, id=ob.id))

    module.exit_json(changed=False,
                     vlan_list=vlnlist)
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        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'],
        ],
    )
    ucs = UCSModule(module)

    err = False
    # note that all objects specified in the object list report a single result (including a single changed).
    ucs.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, ucs, managed_object)

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

    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
def main():
    object_dict = dict(
        module=dict(type='str', required=True),
        class_name=dict(type='str', aliases=['class'], required=True),
        properties=dict(type='dict', required=True),
        children=dict(type='list'),
    )
    argument_spec = ucs_argument_spec
    argument_spec.update(
        objects=dict(type='list',
                     elements='dict',
                     options=object_dict,
                     required=True),
        state=dict(type='str',
                   choices=['present', 'absent'],
                   default='present'),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )

    if not HAS_IMPORT_MODULE:
        module.fail_json(msg='import_module is required for this module')
    ucs = UCSModule(module)

    # note that all objects specified in the object list report a single result (including a single changed).
    ucs.result['changed'] = False

    for managed_object in module.params['objects']:
        traverse_objects(module, ucs, managed_object)
        # single commit for object and any children
        if not module.check_mode and ucs.result['changed']:
            try:
                ucs.login_handle.commit()
            except Exception as e:
                # generic Exception because UCSM can throw a variety of exceptions
                ucs.result['msg'] = "setup error: %s " % str(e)
                module.fail_json(**ucs.result)

    module.exit_json(**ucs.result)
示例#11
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        name=dict(type='str'),
        vsan_id=dict(type='str'),
        vlan_id=dict(type='str'),
        fc_zoning=dict(type='str', default='disabled', choices=['disabled', 'enabled']),
        fabric=dict(type='str', default='common', choices=['common', 'A', 'B']),
        state=dict(type='str', default='present', choices=['present', 'absent']),
        vsan_list=dict(type='list'),
    )

    # Note that use of vsan_list is an experimental feature which allows multiple resource updates with a single UCSM connection.
    # Support for vsan_list may change or be removed once persistent UCS connections are supported.
    # Either vsan_list or name/vsan_id/vlan_id is required (user can specify either a list or single resource).

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_one_of=[
            ['vsan_list', 'name']
        ],
        mutually_exclusive=[
            ['vsan_list', 'name']
        ],
    )
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.fabric.FabricVsan import FabricVsan

    changed = False
    try:
        # Only documented use is a single resource, but to also support experimental
        # feature allowing multiple updates all params are converted to a vsan_list below.

        if module.params['vsan_list']:
            # directly use the list (single resource and list are mutually exclusive
            vsan_list = module.params['vsan_list']
        else:
            # single resource specified, create list from the current params
            vsan_list = [module.params]
        for vsan in vsan_list:
            mo_exists = False
            props_match = False
            # set default params.  Done here to set values for lists which can't be done in the argument_spec
            if not vsan.get('fc_zoning'):
                vsan['fc_zoning'] = 'disabled'
            if not vsan.get('fabric'):
                vsan['fabric'] = 'common'
            # dn is fabric/san/net-<name> for common vsans or fabric/san/[A or B]/net-<name> for A or B
            dn_base = 'fabric/san'
            if vsan['fabric'] != 'common':
                dn_base += '/' + vsan['fabric']
            dn = dn_base + '/net-' + vsan['name']

            mo = ucs.login_handle.query_dn(dn)
            if mo:
                mo_exists = True

            if module.params['state'] == 'absent':
                # mo must exist but all properties do not have to match
                if mo_exists:
                    if not module.check_mode:
                        ucs.login_handle.remove_mo(mo)
                        ucs.login_handle.commit()
                    changed = True
            else:
                if mo_exists:
                    # check top-level mo props
                    kwargs = dict(id=vsan['vsan_id'])
                    kwargs['fcoe_vlan'] = vsan['vlan_id']
                    kwargs['zoning_state'] = vsan['fc_zoning']
                    if (mo.check_prop_match(**kwargs)):
                        props_match = True

                if not props_match:
                    if not module.check_mode:
                        # create if mo does not already exist
                        mo = FabricVsan(
                            parent_mo_or_dn=dn_base,
                            name=vsan['name'],
                            id=vsan['vsan_id'],
                            fcoe_vlan=vsan['vlan_id'],
                            zoning_state=vsan['fc_zoning'],
                        )

                        ucs.login_handle.add_mo(mo, True)
                        ucs.login_handle.commit()
                    changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        bios_policy=dict(type='str', default=''),
        boot_policy=dict(type='str', default='default'),
        description=dict(type='str', aliases=['descr'], default=''),
        mgmt_ip_pool=dict(type='str', default='ext-mgmt'),
        graphics_card_policy=dict(type='str', default=''),
        host_firmware_package=dict(type='str', default=''),
        uuid_pool=dict(type='str', default='default'),
        kvm_mgmt_policy=dict(type='str', default=''),
        local_disk_policy=dict(type='str', default=''),
        maintenance_policy=dict(type='str', default=''),
        ipmi_access_profile=dict(type='str', default=''),
        power_control_policy=dict(type='str', default='default'),
        power_sync_policy=dict(type='str', default=''),
        scrub_policy=dict(type='str', default=''),
        sol_policy=dict(type='str', default=''),
        threshold_policy=dict(type='str', default='default'),
        template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']),
        user_label=dict(type='str', default=''),
        vmedia_policy=dict(type='str', default=''),
        storage_profile=dict(type='str', default=''),
        lan_connectivity_policy=dict(type='str', default=''),
        iqn_pool=dict(type='str', default=''),
        san_connectivity_policy=dict(type='str', default=''),
        server_pool=dict(type='str', default=''),
        server_pool_qualification=dict(type='str', default=''),
        power_state=dict(type='str', default='up', choices=['up', 'down']),
        mgmt_interface_mode=dict(type='str', default='', choices=['', 'in-band']),
        mgmt_vnet_name=dict(type='str', default=''),
        mgmt_inband_pool_name=dict(type='str', default=''),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    ucs = UCSModule(module)
    # UCSModule creation above verifies ucsmsdk is present and exits on failure.
    # Additional imports are done below or in called functions.

    ucs.result['changed'] = False
    props_match = False
    # dn is <org_dn>/ls-<name>
    dn = module.params['org_dn'] + '/ls-' + module.params['name']

    mo = ucs.login_handle.query_dn(dn)
    if mo:
        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if not module.check_mode:
                ucs.login_handle.remove_mo(mo)
                ucs.login_handle.commit()
            ucs.result['changed'] = True
        else:  # state == 'present'
            props_match = check_serivce_profile_templates_props(ucs, module, mo, dn)

    if module.params['state'] == 'present' and not props_match:
        configure_service_profile_template(ucs, module)

    module.exit_json(**ucs.result)
示例#13
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        description=dict(type='str', aliases=['descr'], default=''),
        fabric=dict(type='str', default='A', choices=['A', 'B', 'A-B', 'B-A']),
        redundancy_type=dict(type='str',
                             default='none',
                             choices=['none', 'primary', 'secondary']),
        target=dict(type='str', default='adapter', choices=['adapter', 'vm']),
        template_type=dict(type='str',
                           default='initial-template',
                           choices=['initial-template', 'updating-template']),
        vlans_list=dict(type='list'),
        cdn_source=dict(type='str',
                        default='vnic-name',
                        choices=['vnic-name', 'user-defined']),
        cdn_name=dict(type='str', default=''),
        mtu=dict(type='str', default='1500'),
        mac_pool=dict(type='str', default=''),
        qos_policy=dict(type='str', default=''),
        network_control_policy=dict(type='str', default=''),
        pin_group=dict(type='str', default=''),
        stats_policy=dict(type='str', default='default'),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    ucs = UCSModule(module)

    err = False

    # UCSModule creation above verifies ucsmsdk is present and exits on failure.  Additional imports are done below.
    from ucsmsdk.mometa.vnic.VnicLanConnTempl import VnicLanConnTempl
    from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/lan-conn-templ-<name>
        dn = module.params['org_dn'] + '/lan-conn-templ-' + module.params[
            'name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            # set default params for lists which can't be done in the argument_spec
            if module.params.get('vlans_list'):
                for vlan in module.params['vlans_list']:
                    if not vlan.get('native'):
                        vlan['native'] = 'no'
            # for target 'adapter', change to internal UCS Manager spelling 'adaptor'
            if module.params['target'] == 'adapter':
                module.params['target'] = 'adaptor'
            if mo_exists:
                # check top-level mo props
                kwargs = dict(descr=module.params['description'])
                kwargs['switch_id'] = module.params['fabric']
                kwargs['redundancy_pair_type'] = module.params[
                    'redundancy_type']
                kwargs['target'] = module.params['target']
                kwargs['templ_type'] = module.params['template_type']
                kwargs['cdn_source'] = module.params['cdn_source']
                kwargs['admin_cdn_name'] = module.params['cdn_name']
                kwargs['mtu'] = module.params['mtu']
                kwargs['ident_pool_name'] = module.params['mac_pool']
                kwargs['qos_policy_name'] = module.params['qos_policy']
                kwargs['nw_ctrl_policy_name'] = module.params[
                    'network_control_policy']
                kwargs['pin_to_group_name'] = module.params['pin_group']
                kwargs['stats_policy_name'] = module.params['stats_policy']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    if not module.params.get('vlans_list'):
                        props_match = True
                    else:
                        # check vlan props
                        for vlan in module.params['vlans_list']:
                            child_dn = dn + '/if-' + vlan['name']
                            mo_1 = ucs.login_handle.query_dn(child_dn)
                            if mo_1:
                                kwargs = dict(default_net=vlan['native'])
                                if (mo_1.check_prop_match(**kwargs)):
                                    props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = VnicLanConnTempl(
                        parent_mo_or_dn=module.params['org_dn'],
                        name=module.params['name'],
                        descr=module.params['description'],
                        switch_id=module.params['fabric'],
                        redundancy_pair_type=module.params['redundancy_type'],
                        target=module.params['target'],
                        templ_type=module.params['template_type'],
                        cdn_source=module.params['cdn_source'],
                        admin_cdn_name=module.params['cdn_name'],
                        mtu=module.params['mtu'],
                        ident_pool_name=module.params['mac_pool'],
                        qos_policy_name=module.params['qos_policy'],
                        nw_ctrl_policy_name=module.
                        params['network_control_policy'],
                        pin_to_group_name=module.params['pin_group'],
                        stats_policy_name=module.params['stats_policy'],
                    )

                    if module.params.get('vlans_list'):
                        for vlan in module.params['vlans_list']:
                            mo_1 = VnicEtherIf(
                                parent_mo_or_dn=mo,
                                name=vlan['name'],
                                default_net=vlan['native'],
                            )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#14
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        name=dict(type='str', required=True),
        multicast_policy=dict(type='str', default=''),
        fabric=dict(type='str', default='common', choices=['common', 'A',
                                                           'B']),
        id=dict(type='str'),
        sharing=dict(type='str',
                     default='none',
                     choices=['none', 'primary', 'isolated', 'community']),
        native=dict(type='str', default='no', choices=['yes', 'no']),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'present', ['id']],
        ],
    )
    ucs = UCSModule(module)

    err = False

    # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below.
    from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is fabric/lan/net-<name> for common vlans or fabric/lan/[A or B]/net-<name> for A or B
        dn_base = 'fabric/lan'
        if module.params['fabric'] != 'common':
            dn_base += '/' + module.params['fabric']
        dn = dn_base + '/net-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(id=module.params['id'])
                kwargs['default_net'] = module.params['native']
                kwargs['sharing'] = module.params['sharing']
                kwargs['mcast_policy_name'] = module.params['multicast_policy']
                if (mo.check_prop_match(**kwargs)):
                    props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = FabricVlan(
                        parent_mo_or_dn=dn_base,
                        name=module.params['name'],
                        id=module.params['id'],
                        default_net=module.params['native'],
                        sharing=module.params['sharing'],
                        mcast_policy_name=module.params['multicast_policy'],
                    )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#15
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        descr=dict(type='str', default=''),
        preserve_config=dict(type='str', default='no', choices=['yes', 'no']),
        ownership=dict(type='str',
                       default='unassigned',
                       choices=[
                           'unassigned', 'dedicated', 'shared',
                           'chassis-global-spare'
                       ]),
        #drive_path=dict(type='str', default='PATH-BOTH', choices=['PATH-BOTH', 'PATH-0', 'PATH-1']),
        server_id=dict(type='str', default='', choices=['1', '2']),
        controller_id=dict(type='str', default='', choices=['1', '2']),
        controller_type=dict(type='str', default='SAS'),
        slot_range=dict(type='str'),
        state=dict(default='present',
                   choices=['present', 'absent'],
                   type='str'),
    )
    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    # UCSModule verifies ucsmsdk is present and exits on failure.  Imports are below ucs object creation.
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.lstorage.LstorageControllerRef import LstorageControllerRef
    from ucsmsdk.mometa.lstorage.LstorageDiskSlot import LstorageDiskSlot
    from ucsmsdk.mometa.lstorage.LstorageDiskZoningPolicy import LstorageDiskZoningPolicy

    #slot_range = list(parse_range_list['slot_range'])

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/disk-zoning-policy-<name>
        dn = module.params['org_dn'] + '/disk-zoning-policy-' + module.params[
            'name']
        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(name=module.params['name'])
                if (mo.check_prop_match(**kwargs)):
                    props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = LstorageDiskZoningPolicy(
                        parent_mo_or_dn=module.params['org_dn'],
                        name=module.params['name'],
                        descr=module.params['descr'],
                        preserve_config=module.params['preserve_config'],
                    )

                    for i in list(parse_range_list(
                            module.params['slot_range'])):
                        mo_1 = LstorageDiskSlot(
                            parent_mo_or_dn=mo,
                            id=str(i),
                            ownership=module.params['ownership'],
                            #drive_path=module.params['drive_path'],
                        )
                        if module.params['ownership'] == 'dedicated':
                            mo_2 = LstorageControllerRef(
                                parent_mo_or_dn=mo_1,
                                controller_id=module.params['controller_id'],
                                server_id=module.params['server_id'],
                                controller_type=module.
                                params['controller_type'],
                            )

                        ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#16
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        description=dict(type='str', aliases=['descr'], default=''),
        fabric=dict(type='str', default='A', choices=['A', 'B', 'A-B', 'B-A']),
        redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']),
        peer_redundancy_template=dict(type='str', aliases=['peer_redundancy_templ'], default=''),
        target=dict(type='str', default='adapter', choices=['adapter', 'vm']),
        template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']),
        vlans_list=dict(type='list'),
        cdn_source=dict(type='str', default='vnic-name', choices=['vnic-name', 'user-defined']),
        cdn_name=dict(type='str', default=''),
        mtu=dict(type='str', default='1500'),
        mac_pool=dict(type='str', default=''),
        qos_policy=dict(type='str', default=''),
        network_control_policy=dict(type='str', default=''),
        pin_group=dict(type='str', default=''),
        stats_policy=dict(type='str', default='default'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_if=[
            ['cdn_source', 'user-defined', ['cdn_name']],
        ],
    )
    ucs = UCSModule(module)

    err = False

    # UCSModule creation above verifies ucsmsdk is present and exits on failure.  Additional imports are done below.
    from ucsmsdk.mometa.vnic.VnicLanConnTempl import VnicLanConnTempl
    from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/lan-conn-templ-<name>
        dn = module.params['org_dn'] + '/lan-conn-templ-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            # set default params for lists which can't be done in the argument_spec
            if module.params.get('vlans_list'):
                for vlan in module.params['vlans_list']:
                    if not vlan.get('native'):
                        vlan['native'] = 'no'
            # for target 'adapter', change to internal UCS Manager spelling 'adaptor'
            if module.params['target'] == 'adapter':
                module.params['target'] = 'adaptor'
            if mo_exists:
                # check top-level mo props
                kwargs = dict(descr=module.params['description'])
                kwargs['switch_id'] = module.params['fabric']
                kwargs['redundancy_pair_type'] = module.params['redundancy_type']
                kwargs['peer_redundancy_templ_name'] = module.params['peer_redundancy_template']
                kwargs['ident_pool_name'] = module.params['mac_pool']
                # do not check shared props if this is a secondary template
                if module.params['redundancy_type'] != 'secondary':
                    kwargs['target'] = module.params['target']
                    kwargs['templ_type'] = module.params['template_type']
                    kwargs['cdn_source'] = module.params['cdn_source']
                    kwargs['admin_cdn_name'] = module.params['cdn_name']
                    kwargs['mtu'] = module.params['mtu']
                    kwargs['qos_policy_name'] = module.params['qos_policy']
                    kwargs['nw_ctrl_policy_name'] = module.params['network_control_policy']
                    kwargs['pin_to_group_name'] = module.params['pin_group']
                    kwargs['stats_policy_name'] = module.params['stats_policy']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    if not module.params.get('vlans_list'):
                        props_match = True
                    else:
                        # check vlan props
                        for vlan in module.params['vlans_list']:
                            child_dn = dn + '/if-' + vlan['name']
                            mo_1 = ucs.login_handle.query_dn(child_dn)
                            if mo_1:
                                kwargs = dict(default_net=vlan['native'])
                                if (mo_1.check_prop_match(**kwargs)):
                                    props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    # secondary template only sets non shared props
                    if module.params['redundancy_type'] == 'secondary':
                        mo = VnicLanConnTempl(
                            parent_mo_or_dn=module.params['org_dn'],
                            name=module.params['name'],
                            descr=module.params['description'],
                            switch_id=module.params['fabric'],
                            redundancy_pair_type=module.params['redundancy_type'],
                            peer_redundancy_templ_name=module.params['peer_redundancy_template'],
                            ident_pool_name=module.params['mac_pool'],
                        )
                    else:
                        mo = VnicLanConnTempl(
                            parent_mo_or_dn=module.params['org_dn'],
                            name=module.params['name'],
                            descr=module.params['description'],
                            switch_id=module.params['fabric'],
                            redundancy_pair_type=module.params['redundancy_type'],
                            peer_redundancy_templ_name=module.params['peer_redundancy_templ'],
                            target=module.params['target'],
                            templ_type=module.params['template_type'],
                            cdn_source=module.params['cdn_source'],
                            admin_cdn_name=module.params['cdn_name'],
                            mtu=module.params['mtu'],
                            ident_pool_name=module.params['mac_pool'],
                            qos_policy_name=module.params['qos_policy'],
                            nw_ctrl_policy_name=module.params['network_control_policy'],
                            pin_to_group_name=module.params['pin_group'],
                            stats_policy_name=module.params['stats_policy'],
                        )

                    if module.params.get('vlans_list'):
                        for vlan in module.params['vlans_list']:
                            mo_1 = VnicEtherIf(
                                parent_mo_or_dn=mo,
                                name=vlan['name'],
                                default_net=vlan['native'],
                            )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#17
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        descr=dict(type='str', default='', aliases=['descrption', 'description']),
        order=dict(type='str', default='default', choices=['default', 'sequential']),
        first_addr=dict(type='str'),
        last_addr=dict(type='str'),
        subnet_mask=dict(type='str', default='255.255.255.0'),
        default_gw=dict(type='str', default='0.0.0.0'),
        primary_dns=dict(type='str', default='0.0.0.0'),
        secondary_dns=dict(type='str', default='0.0.0.0'),
        ipv6_first_addr=dict(type='str'),
        ipv6_last_addr=dict(type='str'),
        ipv6_prefix=dict(type='str', default='64'),
        ipv6_default_gw=dict(type='str', default='::'),
        ipv6_primary_dns=dict(type='str', default='::'),
        ipv6_secondary_dns=dict(type='str', default='::'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    # UCSModule verifies ucsmsdk is present and exits on failure.  Imports are below ucs object creation.
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.ippool.IppoolPool import IppoolPool
    from ucsmsdk.mometa.ippool.IppoolBlock import IppoolBlock
    from ucsmsdk.mometa.ippool.IppoolIpV6Block import IppoolIpV6Block

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/ip-pool-<name>
        dn = module.params['org_dn'] + '/ip-pool-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(assignment_order=module.params['order'])
                kwargs['descr'] = module.params['descr']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    if module.params['last_addr'] and module.params['first_addr']:
                        # ipv4 block specified, check properties
                        block_dn = dn + '/block-' + module.params['first_addr'] + '-' + module.params['last_addr']
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            kwargs = dict(subnet=module.params['subnet_mask'])
                            kwargs['def_gw'] = module.params['default_gw']
                            kwargs['prim_dns'] = module.params['primary_dns']
                            kwargs['sec_dns'] = module.params['secondary_dns']
                            if (mo_1.check_prop_match(**kwargs)):
                                # ipv4 block exists and properties match
                                props_match = True
                    else:
                        # no ipv4 block specified, but top-level props matched
                        props_match = True

                    # only check ipv6 props if the top-level and ipv4 props matched
                    if props_match and module.params['ipv6_last_addr'] and module.params['ipv6_first_addr']:
                        # ipv6 block specified, check properties
                        block_dn = dn + '/v6block-' + module.params['ipv6_first_addr'].lower() + '-' + module.params['ipv6_last_addr'].lower()
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            kwargs = dict(prefix=module.params['ipv6_prefix'])
                            kwargs['def_gw'] = module.params['ipv6_default_gw']
                            kwargs['prim_dns'] = module.params['ipv6_primary_dns']
                            kwargs['sec_dns'] = module.params['ipv6_secondary_dns']
                            if (mo_1.check_prop_match(**kwargs)):
                                # ipv6 block exists and properties match
                                props_match = True
                        else:
                            # no ipv6 block specified, but previous checks matched
                            props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = IppoolPool(
                        parent_mo_or_dn=module.params['org_dn'],
                        name=module.params['name'],
                        descr=module.params['descr'],
                        assignment_order=module.params['order'],
                    )

                    if module.params['last_addr'] and module.params['first_addr']:
                        mo_1 = IppoolBlock(
                            parent_mo_or_dn=mo,
                            to=module.params['last_addr'],
                            r_from=module.params['first_addr'],
                            subnet=module.params['subnet_mask'],
                            def_gw=module.params['default_gw'],
                            prim_dns=module.params['primary_dns'],
                            sec_dns=module.params['secondary_dns'],
                        )

                    if module.params['ipv6_last_addr'] and module.params['ipv6_first_addr']:
                        mo_1 = IppoolIpV6Block(
                            parent_mo_or_dn=mo,
                            to=module.params['ipv6_last_addr'],
                            r_from=module.params['ipv6_first_addr'],
                            prefix=module.params['ipv6_prefix'],
                            def_gw=module.params['ipv6_default_gw'],
                            prim_dns=module.params['ipv6_primary_dns'],
                            sec_dns=module.params['ipv6_secondary_dns'],
                        )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()

                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(name=dict(type='str'),
                         num_drives=dict(type='str', default='1'),
                         num_ded_hot_spares=dict(type='str',
                                                 default='unspecified'),
                         num_glob_hot_spares=dict(type='str',
                                                  default='unspecified'),
                         drive_type=dict(type='str',
                                         default='unspecified',
                                         choice=['unspecified', 'HDD', 'SSD']),
                         use_jbod_disks=dict(type='str',
                                             default='no',
                                             choice=['yes', 'no']),
                         use_remaining_disks=dict(type='str',
                                                  default='no',
                                                  choice=['yes', 'no']),
                         min_drive_size=dict(type='str',
                                             default='unspecified'),
                         state=dict(type='str',
                                    default='present',
                                    choices=['present', 'absent']))

    module = AnsibleModule(argument_spec, supports_check_mode=True)
    ucs = UCSModule(module)

    from ucsmsdk.mometa.lstorage.LstorageDiskGroupQualifier import LstorageDiskGroupQualifier

    num_policies = 1
    name_start_index = 0

    name_list = module.params['name'].split(',')
    policy_name_prefix = name_list[0]
    policy_name = policy_name_prefix
    if len(name_list) == 3:
        name_start_index = int(name_list[1])
        num_policies = int(name_list[2])

    for num in range(name_start_index, name_start_index + num_policies):

        err = False
        changed = False
        mo_exists = False
        if num_policies > 1:
            policy_name = policy_name_prefix + str(num)

        try:
            dn_base = 'org-root'
            dn = dn_base + '/disk-group-config-' + policy_name

            existing_mo = ucs.login_handle.query_dn(dn + '/disk-group-qual')
            if existing_mo:
                # check top-level mo props
                kwargs = dict(num_drives=module.params['num_drives'])
                kwargs['drive_type'] = module.params['drive_type']
                kwargs['use_jbod_disks'] = module.params['use_jbod_disks']
                kwargs['use_remaining_disks'] = module.params[
                    'use_remaining_disks']
                kwargs['num_ded_hot_spares'] = module.params[
                    'num_ded_hot_spares']
                kwargs['num_glob_hot_spares'] = module.params[
                    'num_glob_hot_spares']
                kwargs['min_drive_size'] = module.params['min_drive_size']
                if existing_mo.check_prop_match(**kwargs):
                    mo_exists = True

            mo_dg_qual = LstorageDiskGroupQualifier(
                parent_mo_or_dn=dn,
                num_drives=module.params['num_drives'],
                drive_type=module.params['drive_type'],
                use_jbod_disks=module.params['use_jbod_disks'],
                use_remaining_disks=module.params['use_remaining_disks'],
                num_ded_hot_spares=module.params['num_ded_hot_spares'],
                num_glob_hot_spares=module.params['num_glob_hot_spares'],
                min_drive_size=module.params['min_drive_size'])

            if module.params['state'] == 'absent':
                if mo_exists:
                    if not module.check_mode:
                        # delete mo if dn already exist
                        ucs.login_handle.remove_mo(mo_dg_qual)
                        ucs.login_handle.commit()
                    changed = True
            else:
                if not mo_exists:
                    if not module.check_mode:
                        # create mo if dn does not already exist
                        ucs.login_handle.add_mo(mo_dg_qual, True)
                        ucs.login_handle.commit()
                    changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(name=dict(type='str'),
                         descr=dict(type='str', default=''),
                         raid_level=dict(type='str',
                                         default='stripe',
                                         choices=[
                                             'stripe', 'mirror',
                                             'mirror-stripe', 'stripe-parity',
                                             'stripe-dual parity',
                                             'stripe-parity-stripe',
                                             'stripe-dual-parity-stripe'
                                         ]),
                         state=dict(type='str',
                                    default='present',
                                    choices=['present', 'absent']))

    module = AnsibleModule(argument_spec, supports_check_mode=True)
    ucs = UCSModule(module)

    from ucsmsdk.mometa.lstorage.LstorageDiskGroupConfigPolicy import LstorageDiskGroupConfigPolicy

    num_policies = 1
    name_start_index = 0

    name_list = module.params['name'].split(',')
    policy_name_prefix = name_list[0]
    policy_name = policy_name_prefix
    if len(name_list) == 3:
        name_start_index = int(name_list[1])
        num_policies = int(name_list[2])

    for num in range(name_start_index, name_start_index + num_policies):

        err = False
        changed = False
        mo_exists = False
        if num_policies > 1:
            policy_name = policy_name_prefix + str(num)

        try:
            dn_base = 'org-root'
            mo = LstorageDiskGroupConfigPolicy(
                parent_mo_or_dn=dn_base,
                name=policy_name,
                descr=module.params['descr'],
                raid_level=module.params['raid_level'])

            dn = dn_base + '/disk-group-config-' + policy_name
            existing_mo = ucs.login_handle.query_dn(dn)
            if existing_mo:
                # check top-level mo props
                kwargs = dict(descr=module.params['descr'])
                kwargs['raid_level'] = module.params['raid_level']
                if existing_mo.check_prop_match(**kwargs):
                    mo_exists = True

            if module.params['state'] == 'absent':
                if mo_exists:
                    if not module.check_mode:
                        # delete mo if dn already exist
                        ucs.login_handle.remove_mo(mo)
                        ucs.login_handle.commit()
                    changed = True
            else:
                if not mo_exists:
                    if not module.check_mode:
                        # create mo if dn does not already exist
                        ucs.login_handle.add_mo(mo, True)
                        ucs.login_handle.commit()
                    changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(chassis_id=dict(type='str', required=True),
                         blade_id=dict(type='str', choices=['1', '2', '1, 2']),
                         enc_id=dict(type='str', choices=['3', '4', '3, 2']),
                         disk_id=dict(type='str', required=True),
                         blade_enc=dict(type='str',
                                        required=True,
                                        choices=['True', 'False']),
                         disk_state=dict(type='str',
                                         required=True,
                                         choices=['jbod',
                                                  'unconfigured-good']))

    module = AnsibleModule(argument_spec,
                           supports_check_mode=True,
                           required_if=[
                               ['blade_enc', 'True', ['blade_id']],
                               ['blade_enc', 'True', ['enc_id']],
                           ])

    ucs = UCSModule(module)

    from ucsmsdk.mometa.storage.StorageLocalDisk import StorageLocalDisk
    err = False
    changed = False

    try:
        dn_base = 'sys'

        num_chassis = 1
        chassis_list = module.params['chassis_id'].split(',')
        chassis_id_start = int(chassis_list[0])
        if (len(chassis_list) > 1):
            num_chassis = int(chassis_list[1])

        if (module.params['blade_enc'] == 'True'):
            num_blades = 1
            blade_list = module.params['blade_id'].split(',')
            blade_id_start = int(blade_list[0])
            if (len(blade_list) > 1):
                num_blades = int(blade_list[1])

            num_encs = 1
            enc_list = module.params['enc_id'].split(',')
            enc_id_start = int(enc_list[0])
            if (len(enc_list) > 1):
                num_encs = int(enc_list[1])

        num_disks = 1
        disk_list = module.params['disk_id'].split(',')
        disk_id_start = int(disk_list[0])
        if (len(disk_list) > 1):
            num_disks = int(disk_list[1])

        if (module.params['blade_enc'] == 'True'):
            for chassis_num in range(chassis_id_start,
                                     chassis_id_start + num_chassis):
                dn_chassis_base = dn_base + '/chassis-' + str(chassis_num)

                for blade_num in range(blade_id_start,
                                       blade_id_start + num_blades):
                    dn_blade_base = dn_chassis_base + '/blade-' + str(
                        blade_num)

                    for enc_num in range(enc_id_start,
                                         enc_id_start + num_encs):
                        dn_enc_base = dn_blade_base + '/enc-' + str(enc_num)

                        for disk_num in range(disk_id_start,
                                              disk_id_start + num_disks):
                            dn = dn_enc_base + '/disk-' + str(disk_num)

                            existing_mo = ucs.login_handle.query_dn(dn)
                            if existing_mo:
                                kwargs = dict(
                                    disk_state=module.params['disk_state'])
                                if not existing_mo.check_prop_match(**kwargs):
                                    if not module.check_mode:
                                        existing_mo.admin_action_trigger = "triggered"
                                        existing_mo.admin_action = module.params[
                                            'disk_state']
                                        ucs.login_handle.add_mo(
                                            existing_mo, True)
                                        ucs.login_handle.commit()
                                    changed = True
        else:
            for chassis_num in range(chassis_id_start,
                                     chassis_id_start + num_chassis):
                dn_chassis_base = dn_base + '/chassis-' + str(chassis_num)

                for disk_num in range(disk_id_start,
                                      disk_id_start + num_disks):
                    dn = dn_chassis_base + '/enc-1/disk-' + str(disk_num)

                    existing_mo = ucs.login_handle.query_dn(dn)
                    if existing_mo:
                        kwargs = dict(disk_state=module.params['disk_state'])
                        if not existing_mo.check_prop_match(**kwargs):
                            if not module.check_mode:
                                existing_mo.admin_action_trigger = "triggered"
                                existing_mo.admin_action = module.params[
                                    'disk_state']
                                ucs.login_handle.add_mo(existing_mo, True)
                                ucs.login_handle.commit()
                            changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        name=dict(type='str', required=True),
        description=dict(type='str', default=''),
        sioc_connectivity=dict(
            type='str',
            default='single-server-single-sioc',
            choices=['single-server-single-sioc', 'single-server-dual-sioc']),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )

    ucs = UCSModule(module)

    err = False

    # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below.
    from ucsmsdk.mometa.equipment.EquipmentComputeConnPolicy import EquipmentComputeConnPolicy

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is org-root//compute-conn-policy-<name> for chassis connection policy
        dn_base = 'org-root'
        dn = dn_base + '/compute-conn-policy-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(name=module.params['name'])
                kwargs['descr'] = module.params['description']
                kwargs['server_sioc_connectivity'] = module.params[
                    'sioc_connectivity']
                if (mo.check_prop_match(**kwargs)):
                    props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = EquipmentComputeConnPolicy(
                        parent_mo_or_dn=dn_base,
                        name=module.params['name'],
                        descr=module.params['description'],
                        server_sioc_connectivity=module.
                        params['sioc_connectivity'],
                    )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#22
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        name=dict(type='str', required=True),
        multicast_policy=dict(type='str', default=''),
        fabric=dict(type='str', default='common', choices=['common', 'A', 'B']),
        id=dict(type='str'),
        sharing=dict(type='str', default='none', choices=['none', 'primary', 'isolated', 'community']),
        native=dict(type='str', default='no', choices=['yes', 'no']),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'present', ['id']],
        ],
    )
    ucs = UCSModule(module)

    err = False

    # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below.
    from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is fabric/lan/net-<name> for common vlans or fabric/lan/[A or B]/net-<name> for A or B
        dn_base = 'fabric/lan'
        if module.params['fabric'] != 'common':
            dn_base += '/' + module.params['fabric']
        dn = dn_base + '/net-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(id=module.params['id'])
                kwargs['default_net'] = module.params['native']
                kwargs['sharing'] = module.params['sharing']
                kwargs['mcast_policy_name'] = module.params['multicast_policy']
                if (mo.check_prop_match(**kwargs)):
                    props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = FabricVlan(
                        parent_mo_or_dn=dn_base,
                        name=module.params['name'],
                        id=module.params['id'],
                        default_net=module.params['native'],
                        sharing=module.params['sharing'],
                        mcast_policy_name=module.params['multicast_policy'],
                    )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        description=dict(type='str', aliases=['descr'], default=''),
        order=dict(type='str',
                   default='default',
                   choices=['default', 'sequential']),
        prefix=dict(type='str', default=''),
        first_uuid=dict(type='str'),
        last_uuid=dict(type='str'),
        state=dict(default='present',
                   choices=['present', 'absent'],
                   type='str'),
    )
    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    # UCSModule verifies ucsmsdk is present and exits on failure.  Imports are below ucs object creation.
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.uuidpool.UuidpoolPool import UuidpoolPool
    from ucsmsdk.mometa.uuidpool.UuidpoolBlock import UuidpoolBlock

    ucs.result['changed'] = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/uuid-pool-<name>
        dn = module.params['org_dn'] + '/uuid-pool-' + module.params['name']
        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                ucs.result['changed'] = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(assignment_order=module.params['order'])
                kwargs['descr'] = module.params['description']
                if module.params['prefix']:
                    kwargs['prefix'] = module.params['prefix']
                if mo.check_prop_match(**kwargs):
                    # top-level props match, check next level mo/props
                    if module.params['last_uuid'] and module.params[
                            'first_uuid']:
                        # uuid address block specified, check properties
                        block_dn = dn + '/block-from-' + module.params[
                            'first_uuid'].upper(
                            ) + '-to-' + module.params['last_uuid'].upper()
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            props_match = True
                    else:
                        # no UUID address block specified, but top-level props matched
                        props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    if not module.params['prefix']:
                        module.params['prefix'] = 'derived'
                    mo = UuidpoolPool(parent_mo_or_dn=module.params['org_dn'],
                                      name=module.params['name'],
                                      descr=module.params['description'],
                                      assignment_order=module.params['order'],
                                      prefix=module.params['prefix'])

                    if module.params['last_uuid'] and module.params[
                            'first_uuid']:
                        mo_1 = UuidpoolBlock(
                            parent_mo_or_dn=mo,
                            to=module.params['last_uuid'],
                            r_from=module.params['first_uuid'],
                        )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                ucs.result['changed'] = True

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

    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#24
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(rack_id=dict(type='str'),
                         slot_id=dict(type='str'),
                         disk_id=dict(type='str'),
                         disk_state=dict(type='str',
                                         choices=['jbod',
                                                  'unconfigured-good']))

    module = AnsibleModule(argument_spec, supports_check_mode=True)
    ucs = UCSModule(module)

    from ucsmsdk.mometa.storage.StorageLocalDisk import StorageLocalDisk
    err = False
    changed = False

    try:
        dn_base = 'sys'

        num_racks = 1
        rack_list = module.params['rack_id'].split(',')
        rack_id_start = int(rack_list[0])
        if (len(rack_list) > 1):
            num_racks = int(rack_list[1])

        num_slots = 1
        slot_list = module.params['slot_id'].split(',')
        slot_id_start = int(slot_list[0])
        if (len(slot_list) > 1):
            num_slots = int(slot_list[1])

        num_disks = 1
        disk_list = module.params['disk_id'].split(',')
        disk_id_start = int(disk_list[0])
        if (len(disk_list) > 1):
            num_disks = int(disk_list[1])

        for rack_num in range(rack_id_start, rack_id_start + num_racks):
            dn_rack_base = dn_base + '/rack-unit-' + str(rack_num)

            for slot_num in range(slot_id_start, slot_id_start + num_slots):
                dn_slot_base = dn_rack_base + '/board/storage-SAS-' + str(
                    slot_num)

                for disk_num in range(disk_id_start,
                                      disk_id_start + num_disks):
                    dn = dn_slot_base + '/disk-' + str(disk_num)

                    existing_mo = ucs.login_handle.query_dn(dn)
                    if existing_mo:
                        kwargs = dict(disk_state=module.params['disk_state'])
                        if not existing_mo.check_prop_match(**kwargs):
                            if not module.check_mode:
                                existing_mo.admin_action_trigger = "triggered"
                                existing_mo.admin_action = module.params[
                                    'disk_state']
                                ucs.login_handle.add_mo(existing_mo, True)
                                ucs.login_handle.commit()
                            changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#25
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        vlangroup=dict(type='str', required=True),
        vlanname=dict(type='str', required=True),
        state=dict(default='present',
                   choices=['present', 'absent'],
                   type='str'),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'present', ['vlangroup', 'vlanname']],
        ],
    )
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.fabric.FabricNetGroup import FabricNetGroup
    from ucsmsdk.mometa.fabric.FabricPooledVlan import FabricPooledVlan

    changed = False
    try:
        dngroup1_exists = False
        dnvlan1_exists = False
        dnpooled1_exists = False

        #dn = fabric/lan/net-group-VLANGROUP
        #Check for VLAN Group
        dngroup = 'fabric/lan/net-group-' + module.params['vlangroup']
        dngroup1 = ucs.login_handle.query_dn(dngroup)

        #dn = fabric/lan/net-VLANNAME
        #Check for VLAN
        dnvlan = 'fabric/lan/net-' + module.params['vlanname']
        dnvlan1 = ucs.login_handle.query_dn(dnvlan)

        #dn = fabric/lan/net-group-VLANGROUP/net-VLANNAME
        #Check for VLAN within VLAN Group
        dnpooled = 'fabric/lan/net-group-' + module.params[
            'vlangroup'] + '/net-' + module.params['vlanname']
        dnpooled1 = ucs.login_handle.query_dn(dnpooled)

        #Configuration MOs. Couldn't really get this to work off the DNs, so I built additional objects.
        mo = FabricNetGroup(parent_mo_or_dn="fabric/lan",
                            name=module.params['vlangroup'])
        mo_1 = FabricPooledVlan(parent_mo_or_dn=mo,
                                name=module.params['vlanname'])

        if not dngroup1:
            #Error out if the VLAN Group is missing
            err = True
            ucs.result['msg'] = module.params[
                'vlangroup'] + " VLAN Group not configured in UCS"

        if not dnvlan1:
            #Error out if VLAN is missing
            err = True
            ucs.result['msg'] = module.params[
                'vlanname'] + " VLAN not configured in UCS. Use ucs_vlans module to create the VLAN first"

        if dnpooled1:
            dnpooled1_exists = True

        if module.params['state'] == 'absent':
            if dnpooled1_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo_1)
                    ucs.login_handle.commit()
                changed = True

        if module.params['state'] == 'present':
            if not dnpooled1_exists:
                if not module.check_mode:
                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#26
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        description=dict(type='str', default=''),
        source_template=dict(type='str', required=True),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    ucs = UCSModule(module)

    err = False

    # UCSModule creation above verifies ucsmsdk is present and exits on failure.  Additional imports are done below.
    from ucsmsdk.mometa.equipment.EquipmentChassisProfile import EquipmentChassisProfile

    changed = False
    try:
        mo_exists = False
        props_match = False
        dn_base = 'org-root'
        dn = dn_base + '/cp-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(src_templ_name=module.params['source_template'])
                kwargs['descr'] = module.params['description']
                # chassis profiles are of type 'instance'
                kwargs['type'] = 'instance'

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = EquipmentChassisProfile(
                        parent_mo_or_dn=dn_base,
                        name=module.params['name'],
                        descr=module.params['description'],
                        src_templ_name=module.params['source_template'],
                        type='instance',
                    )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#27
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(service_profile_name=dict(type='str', required=True),
                         delegate_to=dict(type='str', default='localhost'))

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=False,
        mutually_exclusive=[],
    )

    # UCSModule verifies ucsmsdk is present and exits on failure.
    # Imports are below for UCS object creation.
    from ucsmsdk.mometa.ls.LsServer import LsServer

    ucs = UCSModule(module)
    err = False
    query_result = []

    try:
        all_service_profiles = ucs.login_handle.query_classid(
            class_id="lsServer")

        for profile in all_service_profiles:
            if module.params['service_profile_name']:
                if profile.name != module.params['service_profile_name']:
                    continue

            if profile.type != 'instance':
                continue

            if profile.assoc_state != 'associated':
                continue

            vnics = ucs.login_handle.query_children(in_mo=profile,
                                                    class_id='vnicFc')

            # Handy sorting function
            def sort_func(x):
                return (x.name)

            # Sort vHBA list by name
            vnics.sort(key=sort_func)

            for vnic in vnics:
                interfaces = ucs.login_handle.query_children(in_dn=vnic.oper_nw_templ_name, \
                    class_id='vnicFcIf')

                for interface in interfaces:
                    filter_string = '(name, "' + interface.name + '", type="eq")'

                    vsans = ucs.login_handle.query_classid(
                        class_id='fabricVsan', filter_str=filter_string)

                    for vsan in vsans:
                        vhba_obj = {
                            "profile": profile.name,
                            "name": vnic.name,
                            "wwn": vnic.addr,
                            "fabric_id": vnic.switch_id,
                            "vsan": vsan.id
                        }
                        query_result.append(vhba_obj)

        ucs.result['vhba_facts'] = query_result

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

    if err:
        module.fail_json(**ucs.result)

    ucs.result['changed'] = False
    module.exit_json(**ucs.result)
示例#28
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        source_template=dict(type='str', required=True),
        user_label=dict(type='str', default=''),
        power_state=dict(type='str', choices=['up', 'down']),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    ucs = UCSModule(module)

    err = False

    # UCSModule creation above verifies ucsmsdk is present and exits on failure.  Additional imports are done below.
    from ucsmsdk.mometa.ls.LsServer import LsServer
    from ucsmsdk.mometa.ls.LsPower import LsPower

    changed = False
    try:
        mo_exists = False
        props_match = False
        dn = module.params['org_dn'] + '/ls-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(src_templ_name=module.params['source_template'])
                kwargs['usr_lbl'] = module.params['user_label']
                # service profiles are of type 'instance'
                kwargs['type'] = 'instance'

                if mo.check_prop_match(**kwargs):
                    # top-level props match
                    if module.params.get('power_state'):
                        child_dn = dn + '/power'
                        mo_1 = ucs.login_handle.query_dn(child_dn)
                        if mo_1:
                            kwargs = dict(state=module.params['power_state'])
                            if mo_1.check_prop_match(**kwargs):
                                props_match = True
                    else:
                        # no power state provided, use existing state as match
                        props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = LsServer(
                        parent_mo_or_dn=module.params['org_dn'],
                        name=module.params['name'],
                        src_templ_name=module.params['source_template'],
                        type='instance',
                        usr_lbl=module.params['user_label'],
                    )
                    if module.params.get('power_state'):
                        admin_state = 'admin-' + module.params['power_state']
                        mo_1 = LsPower(
                            parent_mo_or_dn=mo,
                            state=admin_state,
                        )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#29
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str'),
        descr=dict(type='str'),
        fabric=dict(type='str', default='A', choices=['A', 'B']),
        redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']),
        vsan=dict(type='str', default='default'),
        template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']),
        max_data=dict(type='str', default='2048'),
        wwpn_pool=dict(type='str', default='default'),
        qos_policy=dict(type='str'),
        pin_group=dict(type='str'),
        stats_policy=dict(type='str', default='default'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
        vhba_template_list=dict(type='list'),
    )

    # Note that use of vhba_template_list is an experimental feature which allows multiple resource updates with a single UCSM connection.
    # Support for vhba_template_list may change or be removed once persistent UCS connections are supported.
    # Either vhba_template_list or name is required (user can specify either a list of single resource).

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_one_of=[
            ['vhba_template_list', 'name']
        ],
        mutually_exclusive=[
            ['vhba_template_list', 'name']
        ],
    )
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.vnic.VnicSanConnTempl import VnicSanConnTempl
    from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf

    changed = False
    try:
        # Only documented use is a single resource, but to also support experimental
        # feature allowing multiple updates all params are converted to a vhba_template_list below.

        if module.params['vhba_template_list']:
            # directly use the list (single resource and list are mutually exclusive
            vhba_template_list = module.params['vhba_template_list']
        else:
            # single resource specified, create list from the current params
            vhba_template_list = [module.params]
        for vhba_template in vhba_template_list:
            mo_exists = False
            props_match = False
            # set default params.  Done here to set values for lists which can't be done in the argument_spec
            if not vhba_template.get('descr'):
                vhba_template['descr'] = ''
            if not vhba_template.get('fabric'):
                vhba_template['fabric'] = 'A'
            if not vhba_template.get('redundancy_type'):
                vhba_template['redundancy_type'] = 'none'
            if not vhba_template.get('vsan'):
                vhba_template['vsan'] = 'default'
            if not vhba_template.get('template_type'):
                vhba_template['template_type'] = 'initial-template'
            if not vhba_template.get('max_data'):
                vhba_template['max_data'] = '2048'
            if not vhba_template.get('wwpn_pool'):
                vhba_template['wwpn_pool'] = 'default'
            if not vhba_template.get('qos_policy'):
                vhba_template['qos_policy'] = ''
            if not vhba_template.get('pin_group'):
                vhba_template['pin_group'] = ''
            if not vhba_template.get('stats_policy'):
                vhba_template['stats_policy'] = 'default'
            # dn is <org_dn>/san-conn-templ-<name>
            dn = module.params['org_dn'] + '/san-conn-templ-' + vhba_template['name']

            mo = ucs.login_handle.query_dn(dn)
            if mo:
                mo_exists = True
                # check top-level mo props
                kwargs = dict(descr=vhba_template['descr'])
                kwargs['switch_id'] = vhba_template['fabric']
                kwargs['redundancy_pair_type'] = vhba_template['redundancy_type']
                kwargs['templ_type'] = vhba_template['template_type']
                kwargs['max_data_field_size'] = vhba_template['max_data']
                kwargs['ident_pool_name'] = vhba_template['wwpn_pool']
                kwargs['qos_policy_name'] = vhba_template['qos_policy']
                kwargs['pin_to_group_name'] = vhba_template['pin_group']
                kwargs['stats_policy_name'] = vhba_template['stats_policy']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    child_dn = dn + '/if-default'
                    mo_1 = ucs.login_handle.query_dn(child_dn)
                    if mo_1:
                        kwargs = dict(name=vhba_template['vsan'])
                        if (mo_1.check_prop_match(**kwargs)):
                            props_match = True

            if module.params['state'] == 'absent':
                # mo must exist but all properties do not have to match
                if mo_exists:
                    if not module.check_mode:
                        ucs.login_handle.remove_mo(mo)
                        ucs.login_handle.commit()
                    changed = True
            else:
                if not props_match:
                    if not module.check_mode:
                        # create if mo does not already exist
                        mo = VnicSanConnTempl(
                            parent_mo_or_dn=module.params['org_dn'],
                            name=vhba_template['name'],
                            descr=vhba_template['descr'],
                            switch_id=vhba_template['fabric'],
                            redundancy_pair_type=vhba_template['redundancy_type'],
                            templ_type=vhba_template['template_type'],
                            max_data_field_size=vhba_template['max_data'],
                            ident_pool_name=vhba_template['wwpn_pool'],
                            qos_policy_name=vhba_template['qos_policy'],
                            pin_to_group_name=vhba_template['pin_group'],
                            stats_policy_name=vhba_template['stats_policy'],
                        )

                        mo_1 = VnicFcIf(
                            parent_mo_or_dn=mo,
                            name=vhba_template['vsan'],
                        )

                        ucs.login_handle.add_mo(mo, True)
                        ucs.login_handle.commit()
                    changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#30
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str'),
        purpose=dict(type='str', choices=['node', 'port']),
        descr=dict(type='str'),
        order=dict(type='str', default='default', choices=['default', 'sequential']),
        first_addr=dict(type='str'),
        last_addr=dict(type='str'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
        wwn_list=dict(type='list'),
    )

    # Note that use of wwn_list is an experimental feature which allows multiple resource updates with a single UCSM connection.
    # Support for wwn_list may change or be removed once persistent UCS connections are supported.
    # Either wwn_list or name is required (user can specify either a list or single resource).

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_one_of=[
            ['wwn_list', 'name']
        ],
        mutually_exclusive=[
            ['wwn_list', 'name']
        ],
    )
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.fcpool.FcpoolInitiators import FcpoolInitiators
    from ucsmsdk.mometa.fcpool.FcpoolBlock import FcpoolBlock

    changed = False
    try:
        # Only documented use is a single resource, but to also support experimental
        # feature allowing multiple updates all params are converted to a wwn_list below.

        if module.params['wwn_list']:
            # directly use the list (single resource and list are mutually exclusive
            wwn_list = module.params['wwn_list']
        else:
            # single resource specified, create list from the current params
            wwn_list = [module.params]
        for wwn in wwn_list:
            mo_exists = False
            props_match = False
            # set default params.  Done here to set values for lists which can't be done in the argument_spec
            if not wwn.get('descr'):
                wwn['descr'] = ''
            if not wwn.get('order'):
                wwn['order'] = 'default'
            # dn is <org_dn>/wwn-pool-<name> for WWNN or WWPN
            dn = module.params['org_dn'] + '/wwn-pool-' + wwn['name']

            mo = ucs.login_handle.query_dn(dn)
            if mo:
                mo_exists = True

            if module.params['state'] == 'absent':
                if mo_exists:
                    if not module.check_mode:
                        ucs.login_handle.remove_mo(mo)
                        ucs.login_handle.commit()
                    changed = True
            else:
                # append purpose param with suffix used by UCSM
                purpose_param = wwn['purpose'] + '-wwn-assignment'
                if mo_exists:
                    # check top-level mo props
                    kwargs = dict(assignment_order=wwn['order'])
                    kwargs['descr'] = wwn['descr']
                    kwargs['purpose'] = purpose_param
                    if (mo.check_prop_match(**kwargs)):
                        # top-level props match, check next level mo/props
                        if 'last_addr' in wwn and 'first_addr' in wwn:
                            block_dn = dn + '/block-' + wwn['first_addr'].upper() + '-' + wwn['last_addr'].upper()
                            mo_1 = ucs.login_handle.query_dn(block_dn)
                            if mo_1:
                                props_match = True
                        else:
                            props_match = True

                if not props_match:
                    if not module.check_mode:
                        # create if mo does not already exist
                        mo = FcpoolInitiators(
                            parent_mo_or_dn=module.params['org_dn'],
                            name=wwn['name'],
                            descr=wwn['descr'],
                            assignment_order=wwn['order'],
                            purpose=purpose_param,
                        )
                        if 'last_addr' in wwn and 'first_addr' in wwn:
                            mo_1 = FcpoolBlock(
                                parent_mo_or_dn=mo,
                                to=wwn['last_addr'],
                                r_from=wwn['first_addr'],
                            )

                        ucs.login_handle.add_mo(mo, True)
                        ucs.login_handle.commit()
                    changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#31
0
def main():
    manual_disk = dict(
        slot_num=dict(type='str', required=True),
        role=dict(type='str',
                  default='normal',
                  choices=['normal', 'ded-hot-spare', 'glob-hot-spare']),
        span_id=dict(type='str', default='unspecified'),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        description=dict(type='str', aliases=['descr'], default=''),
        raid_level=dict(
            type='str',
            default='stripe',
            choices=[
                'stripe',
                'mirror',
                'mirror-stripe',
                'stripe-parity',
                'stripe-dual-parity',
                'stripe-parity-stripe',
                'stripe-dual-parity-stripe',
            ],
        ),
        num_drives=dict(type='str', default='1'),
        configuration_mode=dict(type='str',
                                default='automatic',
                                choices=['automatic', 'manual']),
        num_ded_hot_spares=dict(type='str', default='unspecified'),
        num_glob_hot_spares=dict(type='str', default='unspecified'),
        drive_type=dict(type='str',
                        default='unspecified',
                        choices=['unspecified', 'HDD', 'SSD']),
        use_remaining_disks=dict(type='str',
                                 default='no',
                                 choices=['yes', 'no']),
        min_drive_size=dict(type='str', default='unspecified'),
        manual_disks=dict(type='list', elements='dict', options=manual_disk),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
        virtual_drive=dict(type='dict',
                           options=_virtual_drive_argument_spec()),
    )
    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    ucs = UCSModule(module)
    # UCSModule creation above verifies ucsmsdk is present and exits on failure.
    # Additional imports are done below or in called functions.

    ucs.result['changed'] = False
    props_match = False
    # dn is <org_dn>/disk-group-config-<name>
    dn = module.params['org_dn'] + '/disk-group-config-' + module.params['name']

    mo = ucs.login_handle.query_dn(dn)
    if mo:
        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if not module.check_mode:
                ucs.login_handle.remove_mo(mo)
                ucs.login_handle.commit()
            ucs.result['changed'] = True
        else:  # state == 'present'
            props_match = check_disk_policy_props(ucs, module, mo, dn)

    if module.params['state'] == 'present' and not props_match:
        configure_disk_policy(ucs, module, dn)

    module.exit_json(**ucs.result)
示例#32
0
def main():
    from ansible.module_utils.basic import AnsibleModule
    from ansible.module_utils.remote_management.ucs import UCSModule, ucs_argument_spec

    argument_spec = ucs_argument_spec
    argument_spec.update(
        priority=dict(required=True,
                      type='str',
                      choices=[
                          "best-effort", "bronze", "fc", "gold", "platinum",
                          "silver"
                      ]),
        cos=dict(required=False, type='str'),
        weight=dict(required=False, type='str', default=''),
        admin_state=dict(required=False,
                         type='str',
                         default='',
                         choices=['disabled', 'enabled']),
        drop=dict(required=False,
                  type='str',
                  default='no-drop',
                  choices=['drop', 'no-drop']),
        mtu=dict(required=False, type='str', default=''),
        multicast_optimize=dict(required=False,
                                type='str',
                                default='no',
                                choices=['false', 'no', 'true', 'yes']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    ucs = UCSModule(module)

    err = False

    changed = False
    try:
        props_match = False
        dn = "fabric/lan/classes/class-" + module.params['priority']
        mo = ucs.login_handle.query_dn(dn)
        # check top-level mo props
        if module.params['priority'] == 'best-effort':
            kwargs = dict(weight=module.params['weight'])
            kwargs['mtu'] = module.params['mtu']
            kwargs['multicast_optimize'] = module.params['multicast_optimize']
            if mo.check_prop_match(**kwargs):
                props_match = True
            else:
                if not module.check_mode:
                    mo.weight = module.params['weight']
                    mo.mtu = module.params['mtu']
                    mo.multicast_optimize = module.params['multicast_optimize']
                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True
        elif module.params['priority'] == 'fc':
            kwargs = dict(weight=module.params['weight'])
            kwargs['cos'] = module.params['cos']
            if mo.check_prop_match(**kwargs):
                props_match = True
            else:
                if not module.check_mode:
                    mo.weight = module.params['weight']
                    mo.cos = module.params['cos']
                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

        else:
            kwargs = dict(weight=module.params['weight'])
            kwargs['priority'] = module.params['priority']
            kwargs['mtu'] = module.params['mtu']
            kwargs['cos'] = module.params['cos']
            kwargs['drop'] = module.params['drop']
            kwargs['admin_state'] = module.params['admin_state']
            kwargs['multicast_optimize'] = module.params['multicast_optimize']
        if mo.check_prop_match(**kwargs):
            props_match = True
        else:
            if not module.check_mode:
                mo.weight = module.params['weight']
                mo.mtu = module.params['mtu']
                mo.cos = module.params['cos']
                mo.drop = module.params['drop']
                mo.admin_state = module.params['admin_state']
                mo.multicast_optimize = module.params['multicast_optimize']

                ucs.login_handle.add_mo(mo, True)
                ucs.login_handle.commit()
            changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#33
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        descr=dict(type='str', default=''),
        order=dict(type='str', default='default', choices=['default', 'sequential']),
        first_addr=dict(type='str'),
        last_addr=dict(type='str'),
        subnet_mask=dict(type='str', default='255.255.255.0'),
        default_gw=dict(type='str', default='0.0.0.0'),
        primary_dns=dict(type='str', default='0.0.0.0'),
        secondary_dns=dict(type='str', default='0.0.0.0'),
        ipv6_first_addr=dict(type='str'),
        ipv6_last_addr=dict(type='str'),
        ipv6_prefix=dict(type='str', default='64'),
        ipv6_default_gw=dict(type='str', default='::'),
        ipv6_primary_dns=dict(type='str', default='::'),
        ipv6_secondary_dns=dict(type='str', default='::'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    # UCSModule verifies ucsmsdk is present and exits on failure.  Imports are below ucs object creation.
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.ippool.IppoolPool import IppoolPool
    from ucsmsdk.mometa.ippool.IppoolBlock import IppoolBlock
    from ucsmsdk.mometa.ippool.IppoolIpV6Block import IppoolIpV6Block

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/ip-pool-<name>
        dn = module.params['org_dn'] + '/ip-pool-' + module.params['name']

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(assignment_order=module.params['order'])
                kwargs['descr'] = module.params['descr']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    if module.params['last_addr'] and module.params['first_addr']:
                        # ipv4 block specified, check properties
                        block_dn = dn + '/block-' + module.params['first_addr'] + '-' + module.params['last_addr']
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            kwargs = dict(subnet=module.params['subnet_mask'])
                            kwargs['def_gw'] = module.params['default_gw']
                            kwargs['prim_dns'] = module.params['primary_dns']
                            kwargs['sec_dns'] = module.params['secondary_dns']
                            if (mo_1.check_prop_match(**kwargs)):
                                # ipv4 block exists and properties match
                                props_match = True
                    else:
                        # no ipv4 block specified, but top-level props matched
                        props_match = True

                    # only check ipv6 props if the top-level and ipv4 props matched
                    if props_match and module.params['ipv6_last_addr'] and module.params['ipv6_first_addr']:
                        # ipv6 block specified, check properties
                        block_dn = dn + '/v6block-' + module.params['ipv6_first_addr'].lower() + '-' + module.params['ipv6_last_addr'].lower()
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            kwargs = dict(prefix=module.params['ipv6_prefix'])
                            kwargs['def_gw'] = module.params['ipv6_default_gw']
                            kwargs['prim_dns'] = module.params['ipv6_primary_dns']
                            kwargs['sec_dns'] = module.params['ipv6_secondary_dns']
                            if (mo_1.check_prop_match(**kwargs)):
                                # ipv6 block exists and properties match
                                props_match = True
                        else:
                            # no ipv6 block specified, but previous checks matched
                            props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = IppoolPool(
                        parent_mo_or_dn=module.params['org_dn'],
                        name=module.params['name'],
                        descr=module.params['descr'],
                        assignment_order=module.params['order'],
                    )

                    if module.params['last_addr'] and module.params['first_addr']:
                        mo_1 = IppoolBlock(
                            parent_mo_or_dn=mo,
                            to=module.params['last_addr'],
                            r_from=module.params['first_addr'],
                            subnet=module.params['subnet_mask'],
                            def_gw=module.params['default_gw'],
                            prim_dns=module.params['primary_dns'],
                            sec_dns=module.params['secondary_dns'],
                        )

                    if module.params['ipv6_last_addr'] and module.params['ipv6_first_addr']:
                        mo_1 = IppoolIpV6Block(
                            parent_mo_or_dn=mo,
                            to=module.params['ipv6_last_addr'],
                            r_from=module.params['ipv6_first_addr'],
                            prefix=module.params['ipv6_prefix'],
                            def_gw=module.params['ipv6_default_gw'],
                            prim_dns=module.params['ipv6_primary_dns'],
                            sec_dns=module.params['ipv6_secondary_dns'],
                        )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()

                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#34
0
def run_module():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        timezone=dict(type='str'),
        description=dict(type='str', aliases=['descr'], default=''),
        admin_state=dict(type='str',
                         default='enabled',
                         choices=['disabled', 'enabled']),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'present', ['timezone']],
        ],
    )
    ucs = UCSModule(module)

    err = False

    changed = False
    try:
        mo_exists = False
        props_match = False

        dn = 'sys/svc-ext/datetime-svc'

        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    mo.timezone = ""
                    mo.descr = ""
                    ucs.login_handle.add_mo(mo, modify_present=True)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(descr=module.params['description'])
                kwargs['timezone'] = module.params['timezone']
                kwargs['admin_state'] = module.params['admin_state']
                if mo.check_prop_match(**kwargs):
                    props_match = True

            if not props_match:
                if not module.check_mode:
                    # update mo, timezone mo always exists
                    mo.timezone = module.params['timezone']
                    mo.descr = module.params['description']
                    mo.admin_state = module.params['admin_state']
                    ucs.login_handle.add_mo(mo, modify_present=True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#35
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        name=dict(type='str', required=True),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
        switch_id=dict(type='str', required=True),
        port_id=dict(type='str', required=True),
        descr=dict(type='str', required=False),
        flow_ctrl_policy=dict(type='str', default='default'),
        lacp_policy_name=dict(type='str', default='default'),
        admin_speed=dict(type='str',
                         default='10gbps',
                         choices=['1gbps', '10gbps', '40gbps']),
        ports=dict(type='list', default=[]),
    )

    module = AnsibleModule(argument_spec, )
    ucs = UCSModule(module)
    err = False
    ''' 
    print("********Inputs start********")
    print("name=",module.params['name'])
    print("state=",module.params['state'])
    print("switch_id=",module.params['switch_id'])
    print("port_id=",module.params['port_id'])
    print("descr=",module.params['descr'])
    print("flow_ctrl_policy=",module.params['flow_ctrl_policy'])
    print("lacp_policy_name=",module.params['lacp_policy_name'])
    print("admin_speed=",module.params['admin_speed'])
    print("ports=",module.params['ports'])
    print("********Inputs end********") '''

    # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below.
    from ucsmsdk.mometa.fabric.FabricEthLanPc import FabricEthLanPc

    changed = False

    try:
        mo_exists = False
        props_match = False
        #fabric/lan/A/pc-2
        dn_base = 'fabric/lan' + '/' + module.params['switch_id']
        dn = dn_base + '/pc-' + module.params['port_id']
        mo = ucs.login_handle.query_dn(dn)
        #print(mo)

        if mo:
            mo_exists = True
        if module.params['state'] == 'absent':
            # mo must exist but all properties do not have to match
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                #print("mo exists")
                # check top-level mo props
                kwargs = dict(name=module.params['name'])
                kwargs = dict(descr=module.params['descr'])
                kwargs = dict(switch_id=module.params['switch_id'])
                kwargs = dict(port_id=module.params['port_id'])
                kwargs = dict(
                    flow_ctrl_policy=module.params['flow_ctrl_policy'])
                kwargs = dict(
                    lacp_policy_name=module.params['lacp_policy_name'])
                kwargs = dict(admin_speed=module.params['admin_speed'])
                if (mo.check_prop_match(**kwargs)):
                    props_match = True
                if ((mo.name != module.params['name'])
                        or (mo.descr != module.params['descr'])):
                    props_match = False
                #print('props_match=',props_match)
            if not props_match:
                #print("mo do not exists or props do not match")

                mo = FabricEthLanPc(
                    parent_mo_or_dn=dn_base,
                    name=module.params['name'],
                    #switch_id=module.params['switch_id'], not a read write field
                    descr=module.params['descr'],
                    port_id=module.params['port_id'],
                    flow_ctrl_policy=module.params['flow_ctrl_policy'],
                    lacp_policy_name=module.params['lacp_policy_name'],
                    admin_speed=module.params['admin_speed'],
                )

                ucs.login_handle.add_mo(mo, True)
                ucs.login_handle.commit()
                changed = True

            mo_list = ucs.login_handle.query_children(
                in_mo=mo, class_id="FabricEthLanPcEp")
            for port in module.params['ports']:
                if not portExists(port, mo_list):
                    createFabricEthLanPcEp(mo.dn, port["slot_id"],
                                           port["port_id"], ucs)
                    changed = True

            for port in mo_list:
                if deletePort(port, module.params['ports']):
                    ucs.login_handle.remove_mo(port)
                    ucs.login_handle.commit()
                    changed = True

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

    ucs.result['changed'] = changed

    if err:
        module.fail_json(**ucs.result)

    module.exit_json(**ucs.result)
示例#36
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str'),
        descr=dict(type='str'),
        fabric=dict(type='str', default='A', choices=['A', 'B']),
        redundancy_type=dict(type='str', default='none', choices=['none', 'primary', 'secondary']),
        vsan=dict(type='str', default='default'),
        template_type=dict(type='str', default='initial-template', choices=['initial-template', 'updating-template']),
        max_data=dict(type='str', default='2048'),
        wwpn_pool=dict(type='str', default='default'),
        qos_policy=dict(type='str'),
        pin_group=dict(type='str'),
        stats_policy=dict(type='str', default='default'),
        state=dict(type='str', default='present', choices=['present', 'absent']),
        vhba_template_list=dict(type='list'),
    )

    # Note that use of vhba_template_list is an experimental feature which allows multiple resource updates with a single UCSM connection.
    # Support for vhba_template_list may change or be removed once persistent UCS connections are supported.
    # Either vhba_template_list or name is required (user can specify either a list of single resource).

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_one_of=[
            ['vhba_template_list', 'name']
        ],
        mutually_exclusive=[
            ['vhba_template_list', 'name']
        ],
    )
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.vnic.VnicSanConnTempl import VnicSanConnTempl
    from ucsmsdk.mometa.vnic.VnicFcIf import VnicFcIf

    changed = False
    try:
        # Only documented use is a single resource, but to also support experimental
        # feature allowing multiple updates all params are converted to a vhba_template_list below.

        if module.params['vhba_template_list']:
            # directly use the list (single resource and list are mutually exclusive
            vhba_template_list = module.params['vhba_template_list']
        else:
            # single resource specified, create list from the current params
            vhba_template_list = [module.params]
        for vhba_template in vhba_template_list:
            mo_exists = False
            props_match = False
            # set default params.  Done here to set values for lists which can't be done in the argument_spec
            if not vhba_template.get('descr'):
                vhba_template['descr'] = ''
            if not vhba_template.get('fabric'):
                vhba_template['fabric'] = 'A'
            if not vhba_template.get('redundancy_type'):
                vhba_template['redundancy_type'] = 'none'
            if not vhba_template.get('vsan'):
                vhba_template['vsan'] = 'default'
            if not vhba_template.get('template_type'):
                vhba_template['template_type'] = 'initial-template'
            if not vhba_template.get('max_data'):
                vhba_template['max_data'] = '2048'
            if not vhba_template.get('wwpn_pool'):
                vhba_template['wwpn_pool'] = 'default'
            if not vhba_template.get('qos_policy'):
                vhba_template['qos_policy'] = ''
            if not vhba_template.get('pin_group'):
                vhba_template['pin_group'] = ''
            if not vhba_template.get('stats_policy'):
                vhba_template['stats_policy'] = 'default'
            # dn is <org_dn>/san-conn-templ-<name>
            dn = module.params['org_dn'] + '/san-conn-templ-' + vhba_template['name']

            mo = ucs.login_handle.query_dn(dn)
            if mo:
                mo_exists = True
                # check top-level mo props
                kwargs = dict(descr=vhba_template['descr'])
                kwargs['switch_id'] = vhba_template['fabric']
                kwargs['redundancy_pair_type'] = vhba_template['redundancy_type']
                kwargs['templ_type'] = vhba_template['template_type']
                kwargs['max_data_field_size'] = vhba_template['max_data']
                kwargs['ident_pool_name'] = vhba_template['wwpn_pool']
                kwargs['qos_policy_name'] = vhba_template['qos_policy']
                kwargs['pin_to_group_name'] = vhba_template['pin_group']
                kwargs['stats_policy_name'] = vhba_template['stats_policy']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    child_dn = dn + '/if-default'
                    mo_1 = ucs.login_handle.query_dn(child_dn)
                    if mo_1:
                        kwargs = dict(name=vhba_template['vsan'])
                        if (mo_1.check_prop_match(**kwargs)):
                            props_match = True

            if module.params['state'] == 'absent':
                # mo must exist but all properties do not have to match
                if mo_exists:
                    if not module.check_mode:
                        ucs.login_handle.remove_mo(mo)
                        ucs.login_handle.commit()
                    changed = True
            else:
                if not props_match:
                    if not module.check_mode:
                        # create if mo does not already exist
                        mo = VnicSanConnTempl(
                            parent_mo_or_dn=module.params['org_dn'],
                            name=vhba_template['name'],
                            descr=vhba_template['descr'],
                            switch_id=vhba_template['fabric'],
                            redundancy_pair_type=vhba_template['redundancy_type'],
                            templ_type=vhba_template['template_type'],
                            max_data_field_size=vhba_template['max_data'],
                            ident_pool_name=vhba_template['wwpn_pool'],
                            qos_policy_name=vhba_template['qos_policy'],
                            pin_to_group_name=vhba_template['pin_group'],
                            stats_policy_name=vhba_template['stats_policy'],
                        )

                        mo_1 = VnicFcIf(
                            parent_mo_or_dn=mo,
                            name=vhba_template['vsan'],
                        )

                        ucs.login_handle.add_mo(mo, True)
                        ucs.login_handle.commit()
                    changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)
示例#37
0
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_dn=dict(type='str', default='org-root'),
        name=dict(type='str', required=True),
        descr=dict(type='str', default=''),
        order=dict(type='str', default='default', choices=['default', 'sequential']),
        first_addr=dict(type='str'),
        last_addr=dict(type='str'),
        state=dict(default='present', choices=['present', 'absent'], type='str'),
    )
    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
    )
    # UCSModule verifies ucsmsdk is present and exits on failure.  Imports are below ucs object creation.
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool
    from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock

    changed = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/mac-pool-<name>
        dn = module.params['org_dn'] + '/mac-pool-' + module.params['name']
        mo = ucs.login_handle.query_dn(dn)
        if mo:
            mo_exists = True

        if module.params['state'] == 'absent':
            if mo_exists:
                if not module.check_mode:
                    ucs.login_handle.remove_mo(mo)
                    ucs.login_handle.commit()
                changed = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(assignment_order=module.params['order'])
                kwargs['descr'] = module.params['descr']
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    if module.params['last_addr'] and module.params['first_addr']:
                        # mac address block specified, check properties
                        block_dn = dn + '/block-' + module.params['first_addr'].upper() + '-' + module.params['last_addr'].upper()
                        mo_1 = ucs.login_handle.query_dn(block_dn)
                        if mo_1:
                            props_match = True
                    else:
                        # no MAC address block specified, but top-level props matched
                        props_match = True

            if not props_match:
                if not module.check_mode:
                    # create if mo does not already exist
                    mo = MacpoolPool(
                        parent_mo_or_dn=module.params['org_dn'],
                        name=module.params['name'],
                        descr=module.params['descr'],
                        assignment_order=module.params['order'],
                    )

                    if module.params['last_addr'] and module.params['first_addr']:
                        mo_1 = MacpoolBlock(
                            parent_mo_or_dn=mo,
                            to=module.params['last_addr'],
                            r_from=module.params['first_addr'],
                        )

                    ucs.login_handle.add_mo(mo, True)
                    ucs.login_handle.commit()
                changed = True

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

    ucs.result['changed'] = changed
    if err:
        module.fail_json(**ucs.result)
    module.exit_json(**ucs.result)