def create_kube_macs(handle, org): from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock mo = MacpoolPool(parent_mo_or_dn=org, policy_owner="local", descr="KUBAM MAC Pool A", assignment_order="default", name="kubamA") MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:88:8A:FF", r_from="00:25:B5:88:8A:00") handle.add_mo(mo) mo = MacpoolPool(parent_mo_or_dn=org, policy_owner="local", descr="KUBAM MAC Pool B", assignment_order="default", name="kubamB") MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:88:8B:FF", r_from="00:25:B5:88:8B:00") handle.add_mo(mo) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\tKUBAM MAC Pools already exist" else: return 1, err.error_descr return 0, None
def createKubeMacs(handle, org): print "Creating Kubernetes MAC Pools" from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock print "org: %s" % org mo = MacpoolPool(parent_mo_or_dn=org, policy_owner="local", descr="Kubernetes MAC Pool A", assignment_order="default", name="kubeA") mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:88:8A:FF", r_from="00:25:B5:88:8A:00") handle.add_mo(mo) mo = MacpoolPool(parent_mo_or_dn=org, policy_owner="local", descr="Kubernetes MAC Pool B", assignment_order="default", name="kubeB") mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:88:8B:FF", r_from="00:25:B5:88:8B:00") handle.add_mo(mo) try: handle.commit() except UcsException as err: if err.error_code == "103": print "\tKubernetes MAC Pools already exist"
def setup_macpool(server, module): from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock ansible = module.params args_mo = _get_mo_params(ansible) changed = False for mac in args_mo['mac_list']: exists = False dn = args_mo['org_dn'] + '/mac-pool-' + mac['name'] mo = server.query_dn(dn) if mo: # check top-level mo props kwargs = {} kwargs['assignment_order'] = mac['order'] if (mo.check_prop_match(**kwargs)): # top-level props match, check next level props if (mac['to'] <> '' and mac['from'] <> ''): block_dn = dn + '/block-' + mac['from'] + '-' + mac['to'] mo_1 = server.query_dn(block_dn) if mo_1: exists = True else: exists = True if ansible['state'] == 'absent': if exists: changed = True if not module.check_mode: server.remove_mo(mo) server.commit() else: if not exists: changed = True if not module.check_mode: # create if mo does not already exist if not "description" in mac: mac["description"] = "" mo = MacpoolPool(parent_mo_or_dn=args_mo['org_dn'], name=mac['name'], descr=mac['description'], assignment_order=mac['order']) if (mac['to'] <> '' and mac['from'] <> ''): mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to=mac['to'], r_from=mac['from']) server.add_mo(mo, True) server.commit() return changed
def mac_pool_create(handle, name, assignment_order, r_from, to, descr="", parent_dn="org-root"): """ Creates MAC Pool Args: handle (UcsHandle) name (String) : Network Control Policy Name assignment_order (String) : ["default", "sequential"] r_from (String) : Beginning MAC Address to (String) : Ending MAC Address descr (String) : parent_dn (String) : Returns: MacpoolPool: Managed Object Raises: ValueError: If OrgOrg is not present Example: mac_pool_create(handle, "sample_mac_pool", "default", "00:25:B5:00:00:00", "00:25:B5:00:00:03") """ from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock obj = handle.query_dn(parent_dn) if obj: mo = MacpoolPool(parent_mo_or_dn=obj, policy_owner="local", descr=descr, assignment_order=assignment_order, name=name) MacpoolBlock(parent_mo_or_dn=mo, to=to, r_from=r_from) handle.add_mo(mo, modify_present=True) handle.commit() return mo else: raise ValueError("org '%s' is not available" % parent_dn)
def main(): handle = UcsHandle("192.168.254.200","ucspe","ucspe", secure=False) handle.login() ## Acknolwedge Chassis mo = EquipmentChassis(parent_mo_or_dn="sys", admin_state="re-acknowledge", id="5") handle.add_mo(mo, True) handle.commit() ## Update MAC Pool mo = MacpoolBlock(parent_mo_or_dn="org-root/mac-pool-default", r_from="00:25:B5:00:00:00", to="00:25:B5:00:00:C7") handle.add_mo(mo) handle.commit() ## Update UUID Pools handle.query_dn("org-root/uuid-pool-default") mo = UuidpoolBlock(parent_mo_or_dn="org-root/uuid-pool-default", r_from="0000-000000000001", to="0000-0000000000C8") handle.add_mo(mo) handle.commit() ## Setup Fabric Ethernet Uplink A Side mo = FabricEthLan(parent_mo_or_dn="fabric/lan", id="A") mo_1 = FabricEthLanEp(parent_mo_or_dn=mo, admin_speed="10gbps", admin_state="enabled", auto_negotiate="yes", eth_link_profile_name="default", fec="auto", flow_ctrl_policy="default", name="", port_id="1", slot_id="1", usr_lbl="") handle.add_mo(mo, True) handle.commit() ## Setup Fabric Ethernet Uplink A Side mo = FabricEthLan(parent_mo_or_dn="fabric/lan", id="B") mo_1 = FabricEthLanEp(parent_mo_or_dn=mo, admin_speed="10gbps", admin_state="enabled", auto_negotiate="yes", eth_link_profile_name="default", fec="auto", flow_ctrl_policy="default", name="", port_id="1", slot_id="1", usr_lbl="") handle.add_mo(mo, True) handle.commit() ## Setup Service Profile Template mo = LsServer(parent_mo_or_dn="org-root", ident_pool_name="default", name="globotemplate", type="initial-template", uuid="00000000-0000-0000-0000-000000000000") handle.add_mo(mo) handle.commit() handle.logout()
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)
## Mission: Programming Cisco Compute - Python Basic and Advanced ## Mission Exercise 3 - Solution # Get a handle and login to UCS Manager from ucsmsdk.ucshandle import UcsHandle handle = UcsHandle("198.18.133.91", "admin", "password") handle.login() # Create a MAC Pool Block from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock 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() # Create a Service Profile Template and associate to a Server Pool from ucsmsdk.mometa.ls.LsServer import LsServer from ucsmsdk.mometa.ls.LsRequirement import LsRequirement mo_sp_template = LsServer(parent_mo_or_dn="org-root", type="initial-template", name="CommanderCode_PY_Mission_Templ") handle.add_mo(mo_sp_template, modify_present=True) handle.commit() filter_exp = '(name,"CommanderCode_PY_Mission_Templ")' mo_sp_templ = handle.query_classid("lsServer",filter_str=filter_exp) mo_sp_templ_ls_requirement = LsRequirement(parent_mo_or_dn=mo_sp_templ[0].dn, name="Python_Heroes_Server_Pool") handle.add_mo(mo_sp_templ_ls_requirement, modify_present=True) # Instantiate a Service Profile from the Service Profile template
handle.commit() #Create Power Policy mo = PowerPolicy(parent_mo_or_dn=my_Full_Path_Org, fan_speed="any", policy_owner="local", name="No_Cap", prio="no-cap", descr="No Cap") handle.add_mo(mo) handle.commit() #Create IP Pool mo = IppoolPool(parent_mo_or_dn=my_Full_Path_Org, is_net_bios_enabled="disabled", name="ext_mgmt", descr="KVM", policy_owner="local", ext_managed="internal", supports_dhcp="disabled", assignment_order="sequential") mo_1 = IppoolBlock(parent_mo_or_dn=mo, prim_dns=my_Primary_DNS, r_from=my_kvm_pool_first, def_gw=my_KVM_Gateway, sec_dns=my_Secondary_DNS, to=my_kvm_last_addr) handle.add_mo(mo) handle.commit() #Create MAC Pools mo = MacpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", descr="Management FI-A", assignment_order="sequential", name="MGMT-A") mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:A0:00:0F", r_from="00:25:B5:A0:00:00") handle.add_mo(mo) handle.commit() mo = MacpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", descr="Management FI-B", assignment_order="sequential", name="MGMT-B") mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:B0:00:0F", r_from="00:25:B5:B0:00:00") handle.add_mo(mo) handle.commit() mo = MacpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", descr="Production FI-A", assignment_order="sequential", name="VM-A") mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:A1:00:0F", r_from="00:25:B5:A1:00:00") handle.add_mo(mo) handle.commit() mo = MacpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", descr="Production FI-B", assignment_order="sequential", name="VM-B") mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to="00:25:B5:B1:00:0F", r_from="00:25:B5:B1:00:00")
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
if row['org']: org = row['org'] else: org = 'org-root' if row['MAC_pool'] and row['MAC_From'] and row['MAC_To']: # create MAC pool from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock mo = MacpoolPool(parent_mo_or_dn=org, policy_owner="local", descr="", assignment_order="default", name=row['MAC_pool']) mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to=row['MAC_To'], r_from=row['MAC_From']) handle.add_mo(mo) handle.commit() if row['VLAN'] and row['VLAN_ID']: # create vLANs from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan # create a range if needed based on all VLANs given in VLAN_ID vlanList = row['VLAN_ID'].split('-') vlansStart = int(vlanList[0]) if len(vlanList) > 1: vlansEnd = int(vlanList[1]) else: vlansEnd = vlansStart
def mac_pool(input): name = input['name'] descr = input['descr'] to = input['to'] r_from = input['r_from'] state = input['state'] ip = input['ip'] username = input['username'] password = input['password'] mo = "" mo_block = "" results = {} ucs_handle = pickle.loads(str(ucs_login.main(ip, username, password))) ###-------CHECK IF MO EXISTS--------------------------------- try: mo = ucs_handle.query_dn("org-root/mac-pool-" + name) mo_block = ucs_handle.query_dn("org-root/mac-pool-" + name + "/block-" + r_from + "-" + to) except: print("Could not query children of macpool") ###----if expected state is "present"------------------------ if state == "present": if mo: if (mo.descr == descr): if (to <> "" and r_from <> ""): if (mo_block): results['name'] = name results['expected'] = True results['changed'] = False results['present'] = True else: #modified_mo = MacpoolPool(parent_mo_or_dn="org-root", name=name, descr=descr) mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to=to, r_from=r_from) ucs_handle.add_mo(mo, True) ucs_handle.commit() results['name'] = name results['present'] = True results['removed'] = False results['changed'] = True else: results['name'] = name results['expected'] = True results['changed'] = False results['present'] = True else: try: modified_mo = MacpoolPool(parent_mo_or_dn="org-root", name=name, descr=descr) if (to <> "" and r_from <> ""): mo_1 = MacpoolBlock(parent_mo_or_dn=modified_mo, to=to, r_from=r_from) ucs_handle.add_mo(modified_mo, True) ucs_handle.commit() results['name'] = name results['present'] = True results['removed'] = False results['changed'] = True except Exception, e: print(e) ###----------if not, create boot policy with desired config ---------------- else: try: mo = MacpoolPool(parent_mo_or_dn="org-root", name=name, descr=descr) if (to <> "" and r_from <> ""): mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to=to, r_from=r_from) ucs_handle.add_mo(mo) ucs_handle.commit() results['name'] = name results['present'] = False results['created'] = True results['changed'] = True except: print("Mac-pool creation failed")
def main(): argument_spec = ucs_argument_spec argument_spec.update(mac_list=dict(required=True, type='list'), org_dn=dict(type='str', default='org-root'), state=dict(default='present', choices=['present', 'absent'], type='str')) module = AnsibleModule(argument_spec, supports_check_mode=True) ucs = UCSModule(module) err = False from ucsmsdk.mometa.macpool.MacpoolPool import MacpoolPool from ucsmsdk.mometa.macpool.MacpoolBlock import MacpoolBlock changed = False try: for mac in module.params['mac_list']: exists = False dn = module.params['org_dn'] + '/mac-pool-' + mac['name'] mo = ucs.login_handle.query_dn(dn) if mo: # check top-level mo props kwargs = {} if 'order' in mac: kwargs['assignment_order'] = mac['order'] if 'descr' in mac: kwargs['descr'] = mac['descr'] if (mo.check_prop_match(**kwargs)): # top-level props match, check next level mo/props if 'last_addr' in mac and 'first_addr' in mac: block_dn = dn + '/block-' + mac['first_addr'].upper( ) + '-' + mac['last_addr'].upper() mo_1 = ucs.login_handle.query_dn(block_dn) if mo_1: exists = True else: exists = True if module.params['state'] == 'absent': if exists: if not module.check_mode: ucs.login_handle.remove_mo(mo) ucs.login_handle.commit() changed = True else: if not exists: if not module.check_mode: # create if mo does not already exist if 'order' not in mac: mac['order'] = 'default' if 'descr' not in mac: mac['descr'] = '' mo = MacpoolPool( parent_mo_or_dn=module.params['org_dn'], name=mac['name'], descr=mac['descr'], assignment_order=mac['order']) if 'last_addr' in mac and 'first_addr' in mac: mo_1 = MacpoolBlock(parent_mo_or_dn=mo, to=mac['last_addr'], r_from=mac['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)