def main(): handle = UcsHandle("192.168.254.200","ucspe","ucspe", secure=False) handle.login() # Query Blades that are unassociated print("\n\n=== Query Based on Class Name with Filter equal to") filter = "(oper_state,'unassociated',type='eq')".format(BLADE_MODEL) blades = handle.query_classid("computeBlade",filter_str=filter) print("***Found {} Blades".format(len(blades))) # Error Check for available blades if len(blades) < NUM_SP: error = "There are only {} blades left to associate, and you asked for {} Servers to be generated".format(len(blades),NUM_SP) raise NameError(error) # Setup Variable for SP Templates to be deployed dn_set = DnSet() for i in range(1, NUM_SP+1): dn = Dn() sp_name = "SP{}".format(str(i)) dn.attr_set("value", sp_name) dn_set.child_add(dn) # Build XML Object to submit to the API templates = ls_instantiate_n_named_template( cookie=handle.cookie, dn="org-root/ls-globotemplate", in_target_org="org-root", in_error_on_existing="false", in_name_set=dn_set ) # Send XML Object to xml process handler sp_list = handle.process_xml_elem(templates) # Loop through each created sp, and associate them to blades i = 0 while i < len(sp_list): sp = sp_list[i] blade = blades[i] # Print SP and Blade Combination print(sp.dn,blade.dn) # Get Binding Object mo = LsBinding( parent_mo_or_dn=sp.dn, pn_dn=blade.dn, restrict_migration="no" ) # Add MO Binding Object to handler handle.add_mo(mo,modify_present=True) i=i+1 # Bundle the SP Associates handle.commit()
def sp_assoc(): # ########################################### # associate the SP to a blade # ########################################### log.debug("sp_assoc") from ucsmsdk.mometa.ls.LsBinding import LsBinding mo = LsBinding(parent_mo_or_dn="org-root/ls-test_sp", pn_dn=blade_dn, restrict_migration="no") handle.add_mo(mo) handle.commit() # ########################################### # add a handler to watch for sp assoc # ########################################### mo = handle.query_dn("org-root/ls-test_sp") sp_assoc_wait(mo)
def associate_server(handle, org, h): """ handle: connection to ucsc org: org-root or something else h: this is the hash of the host. - 'server' is the server to be bound to. - the service profile is the name of the host with the org: - eg: <org>/ls-<h[name]> => org-root/ls-server - the blade will be something like: - 1006/1/6 or 1 """ # translate physical server name: server = h['server'] try: chassis, slot = server.split("/") except Exception as e: return 1, "server value should be <chassis ID>/<serverID>. Not {0}".format(server) #dn = "compute/sys-1009/chassis-{0}/blade-{1}" dn = "sys/chassis-{0}/blade-{1}".format(chassis, slot) sp = "{0}/ls-{1}".format(org, h['name']) #TODO more error checking. from ucsmsdk.mometa.ls.LsBinding import LsBinding mo = LsBinding(parent_mo_or_dn=sp, #pn_dn="sys/chassis-1/blade-6", pn_dn=dn, restrict_migration="no") handle.add_mo(mo, True) try: handle.commit() except AttributeError: print "\talready associated" except UcsException as err: return 1, sp_name + ": " + err.error_descr return 0, None
def sp_associate(handle, sp_dn, server_dn, wait_assoc_completion=True): from ucsmsdk.mometa.ls.LsBinding import LsBinding # check if sp exists sp = handle.query_dn(sp_dn) if sp is None: raise ValueError("Service profile '%s' does not exist." % sp_dn) # check if dn exists blade = handle.query_dn(server_dn) if blade is None: raise ValueError("Server '%s' does not exist." % server_dn) # check if sp is already associated with blade if sp.assoc_state == LsServerConsts.ASSOC_STATE_ASSOCIATED \ and sp.pn_dn == server_dn: raise ValueError("Service Profile is already associated with Server %s" % (server_dn)) # check if sp already has lsBinding with blade binding = handle.query_dn(sp_dn + "/pn") if binding is not None and binding.pn_dn == server_dn: raise ValueError("Service Profile is already administratively associated with Server %s" % (server_dn)) mo = LsBinding(parent_mo_or_dn=sp_dn, pn_dn=server_dn, restrict_migration="no") handle.add_mo(mo, modify_present=True) handle.commit() if wait_assoc_completion: # add a watch on sp event_handle = UcsEventHandle(handle) _sp_associate_monitor(event_handle=event_handle, mo=sp) while not end_script: time.sleep(1)
Cisco Systems, Inc. """ from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.ls.LsBinding import LsBinding from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.org.OrgOrg import OrgOrg HANDLE = UcsHandle("sandbox-ucsm1.cisco.com", "admin", "password") HANDLE.login() ORG_ORG = OrgOrg( parent_mo_or_dn='org-root', name="devnet", ) HANDLE.add_mo(ORG_ORG, modify_present=True) HANDLE.commit() SP_FROM_TEMPLATE = LsServer(parent_mo_or_dn='org-root/org-devnet', name="devcore-server-01", src_templ_name="devcore_template", type="instance") LsBinding(parent_mo_or_dn=SP_FROM_TEMPLATE, pn_dn="sys/chassis-7/blade-3") HANDLE.add_mo(SP_FROM_TEMPLATE, modify_present=True) HANDLE.commit() HANDLE.logout()
def main(): argument_spec = ucs_argument_spec argument_spec.update( org_dn=dict(type='str', default='org-root'), service_profile_name=dict(type='str', required=True), server_assignment=dict(type='str', choices=['server', 'pool']), server_dn=dict(type='str'), server_pool_name=dict(type='str'), restrict_migration=dict(type='str', default='no', choices=['yes', 'no']), state=dict(default='present', choices=['present', 'absent'], type='str'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, required_if=[ ['state', 'present', ['server_assignment']], ['server_assignment', 'server', ['server_dn']], ['server_assignment', 'pool', ['server_pool_name']], ], mutually_exclusive=[ ['server_dn', 'server_pool_name'], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation. ucs = UCSModule(module) err = False from ucsmsdk.mometa.ls.LsRequirement import LsRequirement from ucsmsdk.mometa.ls.LsBinding import LsBinding from ucsmsdk.mometa.ls.LsServer import LsServer changed = False ucs.result['assign_state'] = 'unassigned' ucs.result['assoc_state'] = 'unassociated' try: ls_mo_exists = False pn_mo_exists = False pn_req_mo_exists = False props_match = False # logical server distinguished name is <org>/ls-<name> and physical node dn appends 'pn' or 'pn-req' ls_dn = module.params['org_dn'] + '/ls-' + module.params['service_profile_name'] ls_mo = ucs.login_handle.query_dn(ls_dn) if ls_mo: ls_mo_exists = True pn_dn = ls_dn + '/pn' pn_mo = ucs.login_handle.query_dn(pn_dn) if pn_mo: pn_mo_exists = True pn_req_dn = ls_dn + '/pn-req' pn_req_mo = ucs.login_handle.query_dn(pn_req_dn) if pn_req_mo: pn_req_mo_exists = True if module.params['state'] == 'absent': if ls_mo_exists and ls_mo.assign_state != 'unassigned': if pn_mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(pn_mo) ucs.login_handle.commit() changed = True elif pn_req_mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(pn_req_mo) ucs.login_handle.commit() changed = True elif ls_mo_exists: # check if logical server is assigned and associated ucs.result['assign_state'] = ls_mo.assign_state ucs.result['assoc_state'] = ls_mo.assoc_state if module.params['server_assignment'] == 'pool' and pn_req_mo_exists: # check the current pool kwargs = dict(name=module.params['server_pool_name']) kwargs['restrict_migration'] = module.params['restrict_migration'] if pn_req_mo.check_prop_match(**kwargs): props_match = True elif pn_mo_exists: kwargs = dict(pn_dn=module.params['server_dn']) kwargs['restrict_migration'] = module.params['restrict_migration'] if pn_mo.check_prop_match(**kwargs): props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist in desired state mo = LsServer( parent_mo_or_dn=module.params['org_dn'], name=module.params['service_profile_name'], ) if module.params['server_assignment'] == 'pool': if pn_mo_exists: ucs.login_handle.remove_mo(pn_mo) mo_1 = LsRequirement( parent_mo_or_dn=mo, name=module.params['server_pool_name'], restrict_migration=module.params['restrict_migration'], ) else: mo_1 = LsBinding( parent_mo_or_dn=mo, pn_dn=module.params['server_dn'], restrict_migration=module.params['restrict_migration'], ) ucs.login_handle.add_mo(mo_1, True) ucs.login_handle.commit() pn_req_mo = ucs.login_handle.query_dn(pn_req_dn) if pn_req_mo: # profiles from templates will add a server pool, so remove and add the server again ucs.login_handle.remove_mo(pn_req_mo) ucs.login_handle.add_mo(mo_1, True) ucs.login_handle.commit() ls_mo = ucs.login_handle.query_dn(ls_dn) if ls_mo: ucs.result['assign_state'] = ls_mo.assign_state ucs.result['assoc_state'] = ls_mo.assoc_state 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'), service_profile_name=dict(type='str', required=True), association_rn=dict(type='str', default='pn', choices=['pn', 'pn-req']), server_dn=dict(type='str'), server_pool_name=dict(type='str'), restrict_migration=dict(type='str', default='no', choices=['yes', 'no']), state=dict(default='present', choices=['present', 'absent'], type='str'), ) module = AnsibleModule( argument_spec, supports_check_mode=True, mutually_exclusive=[ ['server_dn', 'server_pool_name'], ], ) # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation. ucs = UCSModule(module) err = False from ucsmsdk.mometa.ls.LsRequirement import LsRequirement from ucsmsdk.mometa.ls.LsBinding import LsBinding changed = False ucs.result['assign_state'] = 'unassigned' ucs.result['assoc_state'] = 'unassociated' try: ls_mo_exists = False pn_mo_exists = False props_match = False # logical server distinguished name is <org>/ls-<name> and physical node dn appends 'pn' or 'pn-req' ls_dn = module.params['org_dn'] + '/ls-' + module.params['service_profile_name'] ls_mo = ucs.login_handle.query_dn(ls_dn) if ls_mo: ls_mo_exists = True if module.params['state'] == 'absent': if ls_mo_exists and ls_mo.assign_state != 'unassigned': # query pn then pn-req to find server association pn_dn = ls_dn + '/pn' pn_mo = ucs.login_handle.query_dn(pn_dn) if pn_mo: pn_mo_exists = True else: pn_dn = ls_dn + '/pn-req' pn_mo = ucs.login_handle.query_dn(pn_dn) if pn_mo: pn_mo_exists = True if pn_mo_exists: if not module.check_mode: ucs.login_handle.remove_mo(pn_mo) ucs.login_handle.commit() changed = True else: if ls_mo_exists: # verify logical server is assigned and associated # when server slots are being pre-provisioned (assigned is false), this module always tries to change state ucs.result['assign_state'] = ls_mo.assign_state ucs.result['assoc_state'] = ls_mo.assoc_state if ls_mo.assign_state == 'assigned' and ls_mo.assoc_state == 'associated': props_match = True if not props_match: if not module.check_mode: # create if mo does not already exist in desired state if module.params.get('server_dn'): mo = LsBinding( parent_mo_or_dn=ls_dn, pn_dn=module.params['server_dn'], restrict_migration=module.params['restrict_migration'], ) else: mo = LsRequirement( parent_mo_or_dn=ls_dn, name=module.params['server_pool_name'], restrict_migration=module.params['restrict_migration'], ) ucs.login_handle.add_mo(mo, True) ucs.login_handle.commit() ls_mo = ucs.login_handle.query_dn(ls_dn) if ls_mo: ucs.result['assign_state'] = ls_mo.assign_state ucs.result['assoc_state'] = ls_mo.assoc_state 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)
mo_mac_pool_block = MacpoolBlock(parent_mo_or_dn=mac_pool_default, r_from="00:25:B5:00:00:AA", to="00:25:B5:00:00:D9") handle.add_mo(mo_mac_pool_block, modify_present=True) handle.commit() # Associate the CommanderCode_Python_Server Service Profile to a blade service_profiles = handle.query_classid("lsServer") for service_profile in service_profiles: if service_profile.name == "CommanderCode_Python_Server": print(service_profile.dn, service_profile.name) mo_sp = service_profile break from ucsmsdk.mometa.ls.LsBinding import LsBinding mo_ls_binding = LsBinding(parent_mo_or_dn=mo_sp, pn_dn="sys/chassis-1/blade-2") handle.add_mo(mo_ls_binding, modify_present=True) handle.commit() # Query the Service Profile and the blade service_profile = handle.query_dn( "org-root/org-Python_Heroes/ls-CommanderCode_Python_Server") print(service_profile.name, service_profile.assign_state, service_profile.assoc_state) blade = handle.query_dn("sys/chassis-1/blade-2") print(blade.dn, blade.assigned_to_dn) # Logout of the UCS Manager handle.logout()
def set_ucs_server(): from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.ls.LsBinding import LsBinding from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock from ucsmsdk.mometa.org.OrgOrg import OrgOrg ucsm_login() if status['login'] != "success": message = ("there was an error connecting to the UCS Manager, " + "please check the access credentials or IP address") return message # Create Org DevNet mo_org = OrgOrg(parent_mo_or_dn="org-root", name="DevNet") handle.add_mo(mo_org, modify_present=True) handle.commit() # Create MacPool mac_pool_default = handle.query_dn("org-root/mac-pool-default") mo_mac_pool_block = MacpoolBlock(parent_mo_or_dn=mac_pool_default, r_from="00:25:B5:00:00:AA", to="00:25:B5:00:00:D9") handle.add_mo(mo_mac_pool_block, modify_present=True) handle.commit() # Add/Update Service Profile Template mo_sp_template = LsServer(parent_mo_or_dn="org-root/org-DevNet", type="initial-template", name="DevNet_Skill_Template") handle.add_mo(mo_sp_template, modify_present=True) handle.commit() # Retrive the MO for the created/updated Service Profile template filter_exp = '(name,"DevNet_Skill_Template")' mo_sp_templ = handle.query_classid("lsServer", filter_str=filter_exp) # Retrive MOs for any existing Service Profiles filter_exp = '(name,"DevNet_Skill_*", type="re") and (type,"instance")' mo_sp_instances = handle.query_classid("lsServer", filter_str=filter_exp) # Find the highest suffix for existing Service Profiles if len(mo_sp_instances) >= 1: sp_suffixes = [ int(sp_instance.name[sp_instance.name.rindex('_') + 1:]) for sp_instance in mo_sp_instances ] num_sp_instances = max(sp_suffixes) + 1 else: num_sp_instances = 1 # Create the next Service Profile name if num_sp_instances <= 9: service_profile_name = "DevNet_Skill_Server_0" + str(num_sp_instances) else: service_profile_name = "DevNet_Skill_Server_" + str(num_sp_instances) # Find an available compute blade response = handle.query_classid("computeBlade") for blade in response: if blade.admin_state == 'in-service' and blade.availability == 'available': break # Create the Service Profile mo_sp = LsServer(parent_mo_or_dn="org-root/org-DevNet", src_templ_name="DevNet_Skill_Template", name=service_profile_name) mo_sp_templ_ls_binding = LsBinding(parent_mo_or_dn=mo_sp, pn_dn=blade.dn) handle.add_mo(mo_sp, modify_present=True) handle.commit() ucsm_logout() message = ("For the requested UCS Manager Server Provisioning operation," + " server, " + blade.slot_id + ", " + " in chassis, " + blade.chassis_id + ", " + " has been provisioned with service profile, " + service_profile_name.replace('_', ' ') + "," + " in the Dev Net Organization.") return message
def sp_associate(handle, sp_dn, server_dn, wait_for_assoc_completion=True, assoc_completion_timeout=20 * 60): """ Associates a service profile to server Args: handle (UcsHandle) sp_dn (string): dn of service profile server_dn (string): dn of blade or rack wait_for_assoc_completion (bool): by default True. if Set to False, it will not monitor the completion of association. assoc_completion_timeout (number): wait timeout in seconds Returns: None Raises: ValueError: If LsServer is not present Or ComputeBlade or ComputeRack not present Or Service profile is already associated Or Example: sp_associate(handle, sp_dn="org-root/ls-chassis1-blade1", server_dn="sys/chassis-1/blade-1") """ from ucsmsdk.mometa.ls.LsBinding import LsBinding # check if sp exists sp = handle.query_dn(sp_dn) if sp is None: raise ValueError("Service profile '%s' does not exist." % sp_dn) # check if dn exists blade = handle.query_dn(server_dn) if blade is None: raise ValueError("Server '%s' does not exist." % server_dn) # check if sp is already associated with blade if sp.assoc_state == LsServerConsts.ASSOC_STATE_ASSOCIATED \ and sp.pn_dn == server_dn: raise ValueError("Service Profile is already associated with Server %s" "" % server_dn) # check if sp already has lsBinding with blade binding = handle.query_dn(sp_dn + "/pn") if binding is not None and binding.pn_dn == server_dn: raise ValueError("Service Profile is already administratively " "associated with Server %s" % server_dn) mo = LsBinding(parent_mo_or_dn=sp_dn, pn_dn=server_dn, restrict_migration="no") handle.add_mo(mo, modify_present=True) handle.commit() if wait_for_assoc_completion: wait_assoc_completion( handle, sp_dn=sp_dn, server_dn=server_dn, assoc_completion_timeout=assoc_completion_timeout)