Example #1
0
def set_inband_mgmt(ucs_handle, sp_dn, vlan_name):
    """
    Set mgmt interface mode to "in-band"

    Args:
        ucs_handle (ucs_handle)
        sp_dn (string): dn of service profile
        vlan_name (string): name of vlan

    Returns:
        MgmtInterface: Managed Object

    Raises:
        ValueError: If LsServer is not present

    Example:
        set_inband_mgmt(ucs_handle, "org-root/ls-testsp", "test_vlan")
    """

    from ucsmsdk.mometa.mgmt.MgmtInterface import MgmtInterface
    from ucsmsdk.mometa.mgmt.MgmtVnet import MgmtVnet
    from ucsmsdk.mometa.vnic.VnicIpV4MgmtPooledAddr import 	VnicIpV4MgmtPooledAddr

    obj = ucs_handle.query_dn(sp_dn)
    if not obj:
        raise ValueError("SP '%s' does not exist" % sp_dn)

    mo = MgmtInterface(parent_mo_or_dn=obj, mode="in-band")
    MgmtVnet(parent_mo_or_dn=mo, name=vlan_name)
    VnicIpV4MgmtPooledAddr(parent_mo_or_dn=mo, name="hyperflex")

    ucs_handle.add_mo(mo, True)
    ucs_handle.commit()
    return mo
def configure_service_profile_template(ucs, module):
    from ucsmsdk.mometa.ls.LsServer import LsServer
    from ucsmsdk.mometa.vnic.VnicConnDef import VnicConnDef
    from ucsmsdk.mometa.vnic.VnicIScsiNode import VnicIScsiNode
    from ucsmsdk.mometa.ls.LsRequirement import LsRequirement
    from ucsmsdk.mometa.ls.LsPower import LsPower
    from ucsmsdk.mometa.lstorage.LstorageProfileBinding import LstorageProfileBinding
    from ucsmsdk.mometa.mgmt.MgmtInterface import MgmtInterface
    from ucsmsdk.mometa.mgmt.MgmtVnet import MgmtVnet
    from ucsmsdk.mometa.vnic.VnicIpV4MgmtPooledAddr import VnicIpV4MgmtPooledAddr

    if not module.check_mode:
        try:
            # create if mo does not already exist
            mo = LsServer(
                parent_mo_or_dn=module.params['org_dn'],
                bios_profile_name=module.params['bios_policy'],
                boot_policy_name=module.params['boot_policy'],
                descr=module.params['description'],
                ext_ip_state='pooled',
                ext_ip_pool_name=module.params['mgmt_ip_pool'],
                # graphics_card_policy_name=module.params['graphics_card_policy'],
                host_fw_policy_name=module.params['host_firmware_package'],
                ident_pool_name=module.params['uuid_pool'],
                kvm_mgmt_policy_name=module.params['kvm_mgmt_policy'],
                local_disk_policy_name=module.params['local_disk_policy'],
                maint_policy_name=module.params['maintenance_policy'],
                mgmt_access_policy_name=module.params['ipmi_access_profile'],
                name=module.params['name'],
                power_policy_name=module.params['power_control_policy'],
                power_sync_policy_name=module.params['power_sync_policy'],
                scrub_policy_name=module.params['scrub_policy'],
                sol_policy_name=module.params['sol_policy'],
                stats_policy_name=module.params['threshold_policy'],
                type=module.params['template_type'],
                usr_lbl=module.params['user_label'],
                vmedia_policy_name=module.params['vmedia_policy'],
            )

            if module.params['storage_profile']:
                # Storage profile
                mo_1 = LstorageProfileBinding(
                    parent_mo_or_dn=mo,
                    storage_profile_name=module.params['storage_profile'],
                )

            if module.params['mgmt_interface_mode']:
                # Management Interface
                mo_1 = MgmtInterface(
                    parent_mo_or_dn=mo,
                    mode=module.params['mgmt_interface_mode'],
                    ip_v4_state='pooled',
                )
                mo_2 = MgmtVnet(
                    parent_mo_or_dn=mo_1,
                    id='1',
                    name=module.params['mgmt_vnet_name'],
                )
                VnicIpV4MgmtPooledAddr(
                    parent_mo_or_dn=mo_2,
                    name=module.params['mgmt_inband_pool_name'],
                )

            # LAN/SAN connectivity policy
            mo_1 = VnicConnDef(
                parent_mo_or_dn=mo,
                lan_conn_policy_name=module.params['lan_connectivity_policy'],
                san_conn_policy_name=module.params['san_connectivity_policy'],
            )

            if module.params['iqn_pool']:
                # IQN pool
                mo_1 = VnicIScsiNode(
                    parent_mo_or_dn=mo,
                    iqn_ident_pool_name=module.params['iqn_pool']
                )

            # power state
            admin_state = 'admin-' + module.params['power_state']
            mo_1 = LsPower(
                parent_mo_or_dn=mo,
                state=admin_state,
            )

            if module.params['server_pool']:
                # server pool
                mo_1 = LsRequirement(
                    parent_mo_or_dn=mo,
                    name=module.params['server_pool'],
                    qualifier=module.params['server_pool_qualification'],
                )

            ucs.login_handle.add_mo(mo, True)
            ucs.login_handle.commit()
        except Exception as e:  # generic Exception handling because SDK can throw a variety of exceptions
            ucs.result['msg'] = "setup error: %s " % str(e)
            module.fail_json(**ucs.result)

    ucs.result['changed'] = True
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=''),
        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)

    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.vnic.VnicConnDef import VnicConnDef
    from ucsmsdk.mometa.ls.LsRequirement import LsRequirement
    from ucsmsdk.mometa.ls.LsPower import LsPower
    from ucsmsdk.mometa.lstorage.LstorageProfileBinding import LstorageProfileBinding
    from ucsmsdk.mometa.mgmt.MgmtInterface import MgmtInterface
    from ucsmsdk.mometa.mgmt.MgmtVnet import MgmtVnet
    from ucsmsdk.mometa.vnic.VnicIpV4MgmtPooledAddr import VnicIpV4MgmtPooledAddr

    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(bios_profile_name=module.params['bios_policy'])
                kwargs['boot_policy_name'] = module.params['boot_policy']
                kwargs['descr'] = module.params['description']
                kwargs['ext_ip_pool_name'] = module.params['mgmt_ip_pool']
                # kwargs['graphics_card_policy_name'] = module.params['graphics_card_policy']
                kwargs['host_fw_policy_name'] = module.params[
                    'host_firmware_package']
                kwargs['ident_pool_name'] = module.params['uuid_pool']
                kwargs['kvm_mgmt_policy_name'] = module.params[
                    'kvm_mgmt_policy']
                kwargs['local_disk_policy_name'] = module.params[
                    'local_disk_policy']
                kwargs['maint_policy_name'] = module.params[
                    'maintenance_policy']
                kwargs['mgmt_access_policy_name'] = module.params[
                    'ipmi_access_profile']
                kwargs['power_policy_name'] = module.params[
                    'power_control_policy']
                kwargs['power_sync_policy_name'] = module.params[
                    'power_sync_policy']
                kwargs['scrub_policy_name'] = module.params['scrub_policy']
                kwargs['sol_policy_name'] = module.params['sol_policy']
                kwargs['stats_policy_name'] = module.params['threshold_policy']
                kwargs['type'] = module.params['template_type']
                kwargs['usr_lbl'] = module.params['user_label']
                kwargs['vmedia_policy_name'] = module.params['vmedia_policy']

                if mo.check_prop_match(**kwargs):
                    # top-level props match, check next level mo/props
                    # code below should discontinue checks once any mismatch is found

                    # check storage profile 1st
                    child_dn = dn + '/profile-binding'
                    mo_1 = ucs.login_handle.query_dn(child_dn)
                    if mo_1:
                        kwargs = dict(storage_profile_name=module.
                                      params['storage_profile'])
                        if mo_1.check_prop_match(**kwargs):
                            props_match = True
                    elif not module.params['storage_profile']:
                        # no stroage profile mo or desired state
                        props_match = True

                    if props_match:
                        props_match = False
                        # LAN/SAN connectivity policies
                        child_dn = dn + '/conn-def'
                        mo_1 = ucs.login_handle.query_dn(child_dn)
                        if mo_1:
                            kwargs = dict(lan_conn_policy_name=module.
                                          params['lan_connectivity_policy'])
                            kwargs['san_conn_policy_name'] = module.params[
                                'san_connectivity_policy']
                            if mo_1.check_prop_match(**kwargs):
                                props_match = True
                        elif not module.params[
                                'lan_connectivity_policy'] and not module.params[
                                    'san_connectivity_policy']:
                            # no mo and no desired state
                            props_match = True

                    if props_match:
                        props_match = False
                        # inband management policies
                        child_dn = dn + '/iface-in-band'
                        mo_1 = ucs.login_handle.query_dn(child_dn)
                        if mo_1:
                            kwargs = dict(
                                mode=module.params['mgmt_interface_mode'])
                            if mo_1.check_prop_match(**kwargs):
                                child_dn = child_dn + '/network'
                                mo_2 = ucs.login_handle.query_dn(child_dn)
                                if mo_2:
                                    kwargs = dict(
                                        name=module.params['mgmt_vnet_name'])
                                    if mo_2.check_prop_match(**kwargs):
                                        child_dn = child_dn + '/ipv4-pooled-addr'
                                        mo_3 = ucs.login_handle.query_dn(
                                            child_dn)
                                        if mo_3:
                                            kwargs = dict(
                                                name=module.
                                                params['mgmt_inband_pool_name']
                                            )
                                            if mo_3.check_prop_match(**kwargs):
                                                props_match = True
                        elif not module.params['mgmt_interface_mode']:
                            # no mo and no desired state
                            props_match = True

                    if props_match:
                        props_match = False
                        # 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
                        elif not module.params['power_state']:
                            # no mo and no desired state
                            props_match = True

                    if props_match:
                        props_match = False
                        # server pool
                        child_dn = dn + '/pn-req'
                        mo_1 = ucs.login_handle.query_dn(child_dn)
                        if mo_1:
                            kwargs = dict(name=module.params['server_pool'])
                            kwargs['qualifier'] = module.params[
                                'server_pool_qualification']
                            if mo_1.check_prop_match(**kwargs):
                                props_match = True
                        elif not module.params['server_pool']:
                            # no pn-req object and no server pool name provided
                            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'],
                        bios_profile_name=module.params['bios_policy'],
                        boot_policy_name=module.params['boot_policy'],
                        descr=module.params['description'],
                        ext_ip_state='pooled',
                        ext_ip_pool_name=module.params['mgmt_ip_pool'],
                        # graphics_card_policy_name=module.params['graphics_card_policy'],
                        host_fw_policy_name=module.
                        params['host_firmware_package'],
                        ident_pool_name=module.params['uuid_pool'],
                        kvm_mgmt_policy_name=module.params['kvm_mgmt_policy'],
                        local_disk_policy_name=module.
                        params['local_disk_policy'],
                        maint_policy_name=module.params['maintenance_policy'],
                        mgmt_access_policy_name=module.
                        params['ipmi_access_profile'],
                        name=module.params['name'],
                        power_policy_name=module.
                        params['power_control_policy'],
                        power_sync_policy_name=module.
                        params['power_sync_policy'],
                        scrub_policy_name=module.params['scrub_policy'],
                        sol_policy_name=module.params['sol_policy'],
                        stats_policy_name=module.params['threshold_policy'],
                        type=module.params['template_type'],
                        usr_lbl=module.params['user_label'],
                        vmedia_policy_name=module.params['vmedia_policy'],
                    )

                    if module.params['storage_profile']:
                        # Storage profile
                        mo_1 = LstorageProfileBinding(
                            parent_mo_or_dn=mo,
                            storage_profile_name=module.
                            params['storage_profile'],
                        )

                    if module.params['mgmt_interface_mode']:
                        # Management Interface
                        mo_1 = MgmtInterface(
                            parent_mo_or_dn=mo,
                            mode=module.params['mgmt_interface_mode'],
                            ip_v4_state='pooled',
                        )
                        mo_2 = MgmtVnet(
                            parent_mo_or_dn=mo_1,
                            id='1',
                            name=module.params['mgmt_vnet_name'],
                        )
                        mo_3 = VnicIpV4MgmtPooledAddr(
                            parent_mo_or_dn=mo_2,
                            name=module.params['mgmt_inband_pool_name'],
                        )

                    # LAN/SAN connectivity policy
                    mo_1 = VnicConnDef(
                        parent_mo_or_dn=mo,
                        lan_conn_policy_name=module.
                        params['lan_connectivity_policy'],
                        san_conn_policy_name=module.
                        params['san_connectivity_policy'],
                    )

                    # power state
                    admin_state = 'admin-' + module.params['power_state']
                    mo_1 = LsPower(
                        parent_mo_or_dn=mo,
                        state=admin_state,
                    )

                    if module.params['server_pool']:
                        # server pool
                        mo_1 = LsRequirement(
                            parent_mo_or_dn=mo,
                            name=module.params['server_pool'],
                            qualifier=module.
                            params['server_pool_qualification'],
                        )

                    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)