예제 #1
0
fault_server = []

while reboot_count < num_of_reboot:
    handle.logout()
    handle.login()
    print("=======================================")
    print("PERFORMING REBOOT %s" % (reboot_count+1))
    print("=======================================\n")
    print(time.ctime())
    if not servers_to_reboot:
        print("There are no servers to reboot, re-run the test with servers to reboot")
        handle.logout()
        exit()
    for server in servers_to_reboot:
        print("REBOOTING SERVER %s WITH SP %s" % (server.dn, server.assigned_to_dn))
        mo = LsPower(parent_mo_or_dn=server.assigned_to_dn, state="hard-reset-immediate")
        handle.set_mo(mo)
        handle.commit()

    time.sleep(240)
    print("\nCHECKING FAULTS AFTER REBOOT...")
    faults = check_fault(handle, servers_to_reboot)
    if faults:
        print("Faults observers, waiting to see if UCSM clears...")
        time.sleep(90)
        faults = check_fault(handle, servers_to_reboot)
        if faults:
            print("THE FOLLOWING FAULTS ARE OBSERVED")
            for fault in faults:
                print(fault.descr)
                print("FAILURE REPORTED AFTER %s REBOOTS, CHECK SYSTEM FOR FAULTS", (reboot_count+1))
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(service_profile_name=dict(type='str', required=True),
                         power_state=dict(type='str',
                                          required=True,
                                          choices=['up', 'down']),
                         force=dict(type='bool'),
                         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
    from ucsmsdk.mometa.ls.LsPower import LsPower

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

    ucs.result['changed'] = False

    try:
        filter_string = '(name, "{:s}", type="eq")'.format(
            module.params['service_profile_name'])

        service_profiles = ucs.login_handle.query_classid(
            class_id="lsServer", filter_str=filter_string)

        if len(service_profiles) == 0:
            raise Exception('Service profile {:s} does not exist'.format(
                module.params['service_profile_name']))
        elif len(service_profiles) > 1:
            raise Exception('Service profile name {:s} is not unique'.format(
                module.params['service_profile_name']))

        service_profile = service_profiles[0]

        children = ucs.login_handle.query_children(in_mo=service_profile,
                                                   class_id="lsPower")

        power = children[0]

        if power.state == module.params[
                'power_state'] and not module.params['force']:
            ucs.result['changed'] = False
            module.exit_json(**ucs.result)

        if module.params['power_state'] == 'up':
            new_power = LsPower(parent_mo_or_dn=service_profile,
                                state="admin-up")
        else:
            new_power = LsPower(parent_mo_or_dn=service_profile,
                                state="soft-shut-down")

        ucs.login_handle.add_mo(new_power, 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)
예제 #4
0
#Create Service Profile Template ESX
mo = LsServer(parent_mo_or_dn="org-root", vmedia_policy_name="", ext_ip_state="none", bios_profile_name="", mgmt_fw_policy_name="", agent_policy_name="", mgmt_access_policy_name="", dynamic_con_policy_name="", kvm_mgmt_policy_name="", sol_policy_name="", uuid="0", descr="", stats_policy_name="default", policy_owner="local", ext_ip_pool_name="ext-mgmt", boot_policy_name="default", usr_lbl="", host_fw_policy_name="", vcon_profile_name="", ident_pool_name="default", src_templ_name="", type="updating-template", local_disk_policy_name="default", scrub_policy_name="", power_policy_name="default", maint_policy_name="User-Ack", name="ESX", resolve_remote="yes")
mo_1 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="1", transport="ethernet", vnic_name="VMDATA_A")
mo_2 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="2", transport="ethernet", vnic_name="VMDATA_B")
mo_3 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="3", transport="ethernet", vnic_name="vMotion_A")
mo_4 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="4", transport="ethernet", vnic_name="vMotion_B")
mo_5 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="5", transport="ethernet", vnic_name="mgmt_A")
mo_6 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="6", transport="ethernet", vnic_name="mgmt_B")
mo_7 = VnicDefBeh(parent_mo_or_dn=mo, name="", descr="", policy_owner="local", action="none", type="vhba", nw_templ_name="")
mo_8 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="VMDATA_A", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="1", nw_templ_name="VMDATA_A", addr="derived")
mo_9 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="VMDATA_B", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", switch_id="B", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="2", nw_templ_name="VMDATA_B", addr="derived")
mo_11 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="vMotion_A", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="3", nw_templ_name="vMotion_A", addr="derived")
mo_12 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="vMotion_B", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", switch_id="B", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="4", nw_templ_name="vMotion_B", addr="derived")
mo_13 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="mgmt_A", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="5", nw_templ_name="mgmt_A", addr="derived")
mo_14 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="mgmt_B", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", switch_id="B", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="6", nw_templ_name="mgmt_B", addr="derived")
mo_15 = LsPower(parent_mo_or_dn=mo, state="admin-up")
mo_16 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="1", inst_type="auto")
mo_17 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="2", inst_type="auto")
mo_18 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="3", inst_type="auto")
mo_19 = FabricVCon(parent_mo_or_dn=mo, placement="physical", fabric="NONE", share="shared", select="all", transport="ethernet,fc", id="4", inst_type="auto")
if vnamenfs != "no":
    mo_20 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="7", transport="ethernet", vnic_name="NFS_A")
    mo_21 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="8", transport="ethernet", vnic_name="NFS_B")
    mo_22 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="NFS_A", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", switch_id="A", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="7", nw_templ_name="NFS_A", addr="derived")
    mo_23 = VnicEther(parent_mo_or_dn=mo, nw_ctrl_policy_name="", name="NFS_B", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", switch_id="B", pin_to_group_name="", mtu="1500", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="8", nw_templ_name="NFS_B", addr="derived")
    if needfc == "yes":
        mo_24 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="1", transport="fc", vnic_name="FC_A")
        mo_25 = LsVConAssign(parent_mo_or_dn=mo, admin_vcon="any", order="2", transport="fc", vnic_name="FC_B")
        mo_26 = VnicFc(parent_mo_or_dn=mo, addr="derived", name="FC_A", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", pers_bind_clear="no", switch_id="A", pin_to_group_name="", pers_bind="disabled", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="1", nw_templ_name="FC_A", max_data_field_size="2048")
        mo_26_1 = VnicFcIf(parent_mo_or_dn=mo_26, name="")
        mo_27 = VnicFc(parent_mo_or_dn=mo, addr="derived", name="FC_B", admin_host_port="ANY", admin_vcon="any", stats_policy_name="default", pers_bind_clear="no", switch_id="A", pin_to_group_name="", pers_bind="disabled", qos_policy_name="", adaptor_profile_name="VMWare", ident_pool_name="", order="2", nw_templ_name="FC_B", max_data_field_size="2048")
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=''),
        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=''),
        storage_profile=dict(type='str', default=''),
        power_state=dict(type='str', default='up', 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.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

    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
                    # 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):
                            # 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):
                                    # 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'],
                    )

                    # Storage profile
                    mo_1 = LstorageProfileBinding(
                        parent_mo_or_dn=mo,
                        storage_profile_name=module.params['storage_profile'],
                    )
                    # 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,
                    )
                    # 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)