예제 #1
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)
예제 #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)

    ucs.result['changed'] = False
    module.exit_json(**ucs.result)
예제 #3
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=missing_required_lib('importlib'),
                         exception=IMPORT_IMP_ERR)
    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)
예제 #4
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=['description', 'descrption']),
        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)
예제 #5
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)
예제 #6
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)
예제 #7
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)
예제 #8
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'
                    if not vlan.get('state'):
                        vlan['state'] = 'present'
            # 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-' + str(vlan['name'])
                            mo_1 = ucs.login_handle.query_dn(child_dn)
                            if vlan['state'] == 'absent':
                                if mo_1:
                                    props_match = False
                                    break
                            else:
                                if mo_1:
                                    kwargs = dict(default_net=vlan['native'])
                                    if mo_1.check_prop_match(**kwargs):
                                        props_match = True
                                else:
                                    props_match = False
                                    break

            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_template'],
                            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']:
                            if vlan['state'] == 'absent':
                                child_dn = dn + '/if-' + str(vlan['name'])
                                mo_1 = ucs.login_handle.query_dn(child_dn)
                                ucs.login_handle.remove_mo(mo_1)
                            else:
                                mo_1 = VnicEtherIf(
                                    parent_mo_or_dn=mo,
                                    name=str(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)
예제 #9
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)
예제 #10
0
파일: ucs_org.py 프로젝트: nasirhm/general
def main():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        org_name=dict(type='str', aliases=['name']),
        parent_org_path=dict(type='str', default='root'),
        description=dict(type='str', aliases=['descr']),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
        delegate_to=dict(type='str', default='localhost'),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'present', ['org_name']],
        ],
    )

    # UCSModule verifies ucsmsdk is present and exits on failure.
    # Imports are below for UCS object creation.
    ucs = UCSModule(module)
    from ucsmsdk.mometa.org.OrgOrg import OrgOrg

    err = False
    changed = False
    requested_state = module.params['state']

    kwargs = dict()

    if module.params['description'] is not None:
        kwargs['descr'] = module.params['description']

    try:
        parent_org_dn = 'org-' + module.params['parent_org_path'].replace(
            '/', '/org-')
        dn = parent_org_dn + '/org-' + module.params['org_name']

        mo = ucs.login_handle.query_dn(dn)

        # Determine state change
        if mo:
            # Object exists, if it should exist has anything changed?
            if requested_state == 'present':
                # Do some or all Object properties not match, that is a change
                if not mo.check_prop_match(**kwargs):
                    changed = True

        # Object does not exist but should, that is a change
        else:
            if requested_state == 'present':
                changed = True

        # Object exists but should not, that is a change
        if mo and requested_state == 'absent':
            changed = True

        # Apply state if not check_mode
        if changed and not module.check_mode:
            if requested_state == 'absent':
                ucs.login_handle.remove_mo(mo)
            else:
                kwargs['parent_mo_or_dn'] = parent_org_dn
                kwargs['name'] = module.params['org_name']
                if module.params['description'] is not None:
                    kwargs['descr'] = module.params['description']

                mo = OrgOrg(**kwargs)
                ucs.login_handle.add_mo(mo, modify_present=True)
            ucs.login_handle.commit()

    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)
예제 #11
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'),
        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)
예제 #13
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)
예제 #14
0
def run_module():
    argument_spec = ucs_argument_spec
    argument_spec.update(
        dns_server=dict(type='str', aliases=['name']),
        description=dict(type='str', aliases=['descr'], default=''),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
        delegate_to=dict(type='str', default='localhost'),
    )

    module = AnsibleModule(
        argument_spec,
        supports_check_mode=True,
        required_if=[
            ['state', 'present', ['dns_server']],
        ],
    )
    # UCSModule verifies ucsmsdk is present and exits on failure.
    # Imports are below for UCS object creation.
    ucs = UCSModule(module)
    from ucsmsdk.mometa.comm.CommDnsProvider import CommDnsProvider

    err = False
    changed = False

    try:
        mo_exists = False
        props_match = False

        dn = 'sys/svc-ext/dns-svc/dns-' + module.params['dns_server']

        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(descr=module.params['description'])
                if mo.check_prop_match(**kwargs):
                    props_match = True

            if not props_match:
                if not module.check_mode:
                    # update/add mo
                    mo = CommDnsProvider(parent_mo_or_dn='sys/svc-ext/dns-svc',
                                         name=module.params['dns_server'],
                                         descr=module.params['description'])
                    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)
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)
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'),
        wwnn_pool=dict(type='str', default='default'),
        vhba_list=dict(type='list'),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
        san_connectivity_list=dict(type='list'),
    )

    # Note that use of san_connectivity_list is an experimental feature which allows multiple resource updates with a single UCSM connection.
    # Support for san_connectivity_list may change or be removed once persistent UCS connections are supported.
    # Either san_connectivity_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=[
            ['san_connectivity_list', 'name'],
        ],
        mutually_exclusive=[
            ['san_connectivity_list', 'name'],
        ],
    )
    ucs = UCSModule(module)

    err = False

    from ucsmsdk.mometa.vnic.VnicSanConnPolicy import VnicSanConnPolicy
    from ucsmsdk.mometa.vnic.VnicFcNode import VnicFcNode
    from ucsmsdk.mometa.vnic.VnicFc import VnicFc
    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 san_connectivity_list below.

        if module.params['san_connectivity_list']:
            # directly use the list (single resource and list are mutually exclusive
            san_connectivity_list = module.params['san_connectivity_list']
        else:
            # single resource specified, create list from the current params
            san_connectivity_list = [module.params]
        for san_connectivity in san_connectivity_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 san_connectivity.get('descr'):
                san_connectivity['descr'] = ''
            if not san_connectivity.get('wwnn_pool'):
                san_connectivity['wwnn_pool'] = 'default'
            if san_connectivity.get('vhba_list'):
                for vhba in san_connectivity['vhba_list']:
                    if not vhba.get('adapter_policy'):
                        vhba['adapter_policy'] = ''
                    if not vhba.get('order'):
                        vhba['order'] = 'unspecified'
            # dn is <org_dn>/san-conn-pol-<name>
            dn = module.params['org_dn'] + '/san-conn-pol-' + san_connectivity[
                'name']

            mo = ucs.login_handle.query_dn(dn)
            if mo:
                mo_exists = True
                # check top-level mo props
                kwargs = dict(descr=san_connectivity['descr'])
                if (mo.check_prop_match(**kwargs)):
                    # top-level props match, check next level mo/props
                    # vnicFcNode object
                    child_dn = dn + '/fc-node'
                    mo_1 = ucs.login_handle.query_dn(child_dn)
                    if mo_1:
                        kwargs = dict(
                            ident_pool_name=san_connectivity['wwnn_pool'])
                        if (mo_1.check_prop_match(**kwargs)):
                            if not san_connectivity.get('vhba_list'):
                                props_match = True
                            else:
                                # check vnicFc props
                                for vhba in san_connectivity['vhba_list']:
                                    child_dn = dn + '/fc-' + vhba['name']
                                    mo_2 = ucs.login_handle.query_dn(child_dn)
                                    kwargs = {}
                                    kwargs['adaptor_profile_name'] = vhba[
                                        'adapter_policy']
                                    kwargs['order'] = vhba['order']
                                    kwargs['nw_templ_name'] = vhba[
                                        'vhba_template']
                                    if (mo_2.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 = VnicSanConnPolicy(
                            parent_mo_or_dn=module.params['org_dn'],
                            name=san_connectivity['name'],
                            descr=san_connectivity['descr'],
                        )
                        mo_1 = VnicFcNode(
                            parent_mo_or_dn=mo,
                            ident_pool_name=san_connectivity['wwnn_pool'],
                            addr='pool-derived',
                        )
                        if san_connectivity.get('vhba_list'):
                            for vhba in san_connectivity['vhba_list']:
                                mo_2 = VnicFc(
                                    parent_mo_or_dn=mo,
                                    name=vhba['name'],
                                    adaptor_profile_name=vhba[
                                        'adapter_policy'],
                                    nw_templ_name=vhba['vhba_template'],
                                    order=vhba['order'],
                                )
                                mo_2_1 = VnicFcIf(
                                    parent_mo_or_dn=mo_2,
                                    name='default',
                                )

                        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():
    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():
    local_lun = dict(
        name=dict(type='str', required=True),
        state=dict(type='str',
                   default='present',
                   choices=['present', 'absent']),
        size=dict(type='str', default='1'),
        auto_deploy=dict(type='str',
                         default='auto-deploy',
                         choices=['auto-deploy', 'no-auto-deploy']),
        expand_to_avail=dict(type='str', default='no', choices=['no', 'yes']),
        fractional_size=dict(type='str', default='0'),
        disk_policy_name=dict(type='str', default=''),
    )
    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=''),
        local_luns=dict(type='list', elements='dict', options=local_lun),
        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.lstorage.LstorageProfile import LstorageProfile
    from ucsmsdk.mometa.lstorage.LstorageDasScsiLun import LstorageDasScsiLun

    ucs.result['changed'] = False
    try:
        mo_exists = False
        props_match = False
        # dn is <org_dn>/profile-<name>
        dn = module.params['org_dn'] + '/profile-' + 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()
                ucs.result['changed'] = True
        else:
            if mo_exists:
                # check top-level mo props
                kwargs = dict(descr=module.params['description'])
                if mo.check_prop_match(**kwargs):
                    # top-level props match, check next level mo/props
                    if not module.params.get('local_luns'):
                        props_match = True
                    else:
                        # check local lun props
                        for lun in module.params['local_luns']:
                            child_dn = dn + '/das-scsi-lun-' + lun['name']
                            mo_1 = ucs.login_handle.query_dn(child_dn)
                            if lun['state'] == 'absent':
                                if mo_1:
                                    props_match = False
                                    break
                            else:
                                if mo_1:
                                    kwargs = dict(size=str(lun['size']))
                                    kwargs['auto_deploy'] = lun['auto_deploy']
                                    kwargs['expand_to_avail'] = lun[
                                        'expand_to_avail']
                                    kwargs['fractional_size'] = str(
                                        lun['fractional_size'])
                                    kwargs['local_disk_policy_name'] = lun[
                                        'disk_policy_name']
                                    if mo_1.check_prop_match(**kwargs):
                                        props_match = True
                                else:
                                    props_match = False
                                    break

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

                    if module.params.get('local_luns'):
                        for lun in module.params['local_luns']:
                            if lun['state'] == 'absent':
                                child_dn = dn + '/das-scsi-lun-' + lun['name']
                                mo_1 = ucs.login_handle.query_dn(child_dn)
                                ucs.login_handle.remove_mo(mo_1)
                            else:
                                mo_1 = LstorageDasScsiLun(
                                    parent_mo_or_dn=mo,
                                    name=lun['name'],
                                    size=str(lun['size']),
                                    auto_deploy=lun['auto_deploy'],
                                    expand_to_avail=lun['expand_to_avail'],
                                    fractional_size=str(
                                        lun['fractional_size']),
                                    local_disk_policy_name=lun[
                                        'disk_policy_name'],
                                )

                    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)