def test_003_fabricvlan_id_5000(): # type: uint # range: ["1-3967", "1-4029", "4048-4091", "4048-4093"] from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan vlan = FabricVlan(parent_mo_or_dn="parent_dn", name="my_vlan") vlan.id = 5000
def assign_vlan_to_sp_vnic(handle, args): # Remove any existing ironic-<vlan> vifs from this UCSM server existing_ironic_vifs = handle.query_classid( 'VnicEtherIf', filter_str=('(name, ".*ironic-.*") and (dn, ".*{0}.*")'.format( args.sp_name))) for vif in existing_ironic_vifs: handle.remove_mo(vif) handle.commit() # Add the vlan to UCSM globally if it doesn't already exist vlan = handle.query_dn('fabric/lan/net-ironic-{0}'.format(args.vlan)) if not vlan: vp1 = handle.query_dn("fabric/lan") handle.add_mo( FabricVlan(vp1, name="ironic-{0}".format(args.vlan), id=args.vlan)) handle.commit() # Add the the VLAN as the default network for the first NIC on the server eth0 = handle.query_classid('VnicEther', filter_str='(dn, ".*{0}.*")'.format( args.sp_name))[0] VnicEtherIf(parent_mo_or_dn=eth0, default_net="yes", name="ironic-{0}".format(args.vlan)) handle.set_mo(eth0) handle.commit()
def add_ucs_vlan(vlan_name, vlan_id): from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan handle = ucs_login() fabric_lan_cloud = handle.query_classid("FabricLanCloud") mo = FabricVlan(parent_mo_or_dn=fabric_lan_cloud[0], name=vlan_name, id=vlan_id) handle.add_mo(mo, True) handle.commit() vlans = handle.query_classid("fabricVlan") for vlan in vlans: if vlan.name == vlan_name and vlan.id == vlan_id: vlan_found = True break if vlan_found: response = "Vlan: " + vlan.name + " with Vlan ID: " + vlan.id + " has been created/updated" else: response = "Vlan: " + vlan.name + " with Vlan ID: " + vlan.id + " creation/update failed" ucs_logout(handle) print(response) return response
def create_vlan_global(vlan_id): from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name=vlan_id, id=vlan_id, mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) handle.commit()
def ucs_remove_vlan_from_pc(module): vlan_name = module.params.get('vlan_name') vlan_id = module.params.get('vlan_id') ucsm_ip = module.params.get('ip') ucsm_pw = module.params.get('password') ucsm_login = module.params.get('login') pc_id = module.params.get('pc_id') admin_speed = module.params.get('admin_speed') is_native = module.params.get('is_native') ucsm = UCS(ucsm_ip, ucsm_login, ucsm_pw) results = {} #Login to UCSM try: ucsm.login() results['logged_in'] = True except Exception as e: module.fail_json(msg=e) FILIST = ['A','B'] for FI in FILIST: obj = ucsm.handle.query_dn("fabric/lan") lancloud = ucsm.handle.query_classid(class_id="FabricLanCloud") try: mo = FabricEthLan(parent_mo_or_dn=obj, id=FI) mo_1 = FabricVlan(parent_mo_or_dn=mo, sharing="none", name=vlan_name, id=vlan_id, mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") mo_1_1 = ucsm.handle.query_children( in_mo=mo_1, class_id="FabricEthVlanPc", filter_str='(vlan_name + + str(vlan_id), %s, type="eq")' % (vlan_name,vlan_id) ) #mo_1_1 = FabricEthVlanPc(parent_mo_or_dn=mo_1, name=vlan_name + + str(vlan_id), descr="", is_native=is_native, admin_speed=admin_speed, switch_id=FI, admin_state="enabled", oper_speed=admin_speed, port_id=pc_id) ucsm.handle.remove_mo(mo_1_1) ucsm.handle.commit() results['changed'] = True except Exception as e: results['changed'] = False try: ucsm.handle.logout() results['logged_out'] = True except Exception as e: module.fail_json(msg=e) try: ucsm.handle.logout() results['logged_out'] = True except Exception as e: module.fail_json(msg=e) return results
def vlan_create(handle, name, vlan_id, sharing="none", mcast_policy_name="", compression_type="included", default_net="no", pub_nw_name="", parent_dn="fabric/lan"): """ Creates VLAN Args: handle (UcsHandle) sharing (String) : ["community", "isolated", "none", "primary"] name (String) : VLAN Name vlan_id (String): VLAN ID mcast_policy_name (String) : Multicast Policy Name compression_type (string) : ["excluded", "included"] default_net (String) : ["false", "no", "true", "yes"] pub_nw_name (String) : parent_dn (String) : Returns: FabricVlan: Managed Object Raises: ValueError: If FabricLanCloud is not present Example: vlan_create(handle, "none", "vlan-lab", "123", "sample_mcast_policy", "included") """ from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan obj = handle.query_dn(parent_dn) if obj: mo = FabricVlan(parent_mo_or_dn=obj, sharing=sharing, name=name, id=vlan_id, mcast_policy_name=mcast_policy_name, policy_owner="local", default_net=default_net, pub_nw_name=pub_nw_name, compression_type=compression_type) handle.add_mo(mo, modify_present=True) handle.commit() return mo else: raise ValueError("lan '%s' is not available" % parent_dn)
def vlan_create(handle, name, vlan_id, sharing="none", mcast_policy_name="", compression_type="included", default_net="no", pub_nw_name="", parent_dn="fabric/lan"): """ # LAN # - LAN Cloud Creates VLAN Args: handle (UcsHandle) sharing (String) : ["community", "isolated", "none", "primary"] name (String) : VLAN Name vlan_id (String): VLAN ID mcast_policy_name (String) : Multicast Policy Name compression_type (string) : ["excluded", "included"] default_net (String) : ["false", "no", "true", "yes"] pub_nw_name (String) : parent_dn (String) : Returns: None Example: vlan_create(handle, "none", "vlan-lab", "123", "sample_mcast_policy", "included") """ from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan obj = handle.query_dn(parent_dn) if obj: vlan = FabricVlan(parent_mo_or_dn=obj, sharing=sharing, name=name, id=vlan_id, mcast_policy_name=mcast_policy_name, policy_owner="local", default_net=default_net, pub_nw_name=pub_nw_name, compression_type=compression_type) handle.add_mo(vlan, modify_present=True) handle.commit() else: log.info(parent_dn + " MO is not available")
def ucs_add_vlan_to_pc(module): vlan_name = module.params.get('vlan_name') vlan_id = module.params.get('vlan_id') ucsm_ip = module.params.get('ip') ucsm_pw = module.params.get('password') ucsm_login = module.params.get('login') pc_id = module.params.get('pc_id') admin_speed = module.params.get('admin_speed') is_native = module.params.get('is_native') ucsm = UCS(ucsm_ip, ucsm_login, ucsm_pw) results = {} #Login to UCSM try: ucsm.login() results['logged_in'] = True except Exception as e: module.fail_json(msg=e) FILIST = ['A', 'B'] for FI in FILIST: obj = ucsm.handle.query_dn("fabric/lan") # Attaches a single Global Vlan to Port Channel try: # mo = FabricEthLan(parent_mo_or_dn=obj, id=FI) mo_1 = FabricVlan(parent_mo_or_dn=obj, sharing="none", name=vlan_name, id=vlan_id, mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") mo_1_1 = FabricEthVlanPc(parent_mo_or_dn=mo_1, name="", descr="", is_native=is_native, admin_speed=admin_speed, switch_id=FI, admin_state="enabled", oper_speed=admin_speed, port_id=pc_id) ucsm.handle.add_mo(mo_1, modify_present=True) ucsm.handle.commit() results['changed'] = True except Exception as e: results['changed'] = False try: ucsm.handle.logout() results['logged_out'] = True except Exception as e: module.fail_json(msg=e) try: ucsm.handle.logout() results['logged_out'] = True except Exception as e: module.fail_json(msg=e) return results
def updateVlans(module): ucs_ip = module.params['ucs_ip'] ucs_user = module.params['ucs_user'] ucs_pass = module.params['ucs_pass'] vlans = module.params['vlans'] state = module.params['state'] results = {} results['changed'] = False results['created'] = [] results['removed'] = [] # Login to UCS try: handle = UcsHandle(ucs_ip, ucs_user, ucs_pass) except: module.fail_json(msg="Could not login to UCS") try: # Obtain a handle for the LAN Cloud lancloud = handle.query_classid(class_id="FabricLanCloud") defined_vlans = handle.query_children(in_mo=lancloud[0], class_id="FabricVlan") for key, val in vlans.iteritems(): filter_str = '(name, "%s", type="eq") and (id, %s, type="eq")' \ % (key, val) obj = handle.query_children(in_mo=lancloud[0], class_id="FabricVlan", filter_str=filter_str) if state == 'present' and len(obj) > 0: pass elif state == 'present' and len(obj) == 0: vlan = FabricVlan(lancloud[0], name=key, id=str(val)) handle.add_mo(vlan) handle.commit() results['created'].append(key) results['changed'] = True elif state == 'absent' and len(obj) > 0: handle.remove_mo(obj[0]) handle.commit() results['changed'] = True results['removed'].append(key) except Exception, e: module.fail_json(msg="Could not create or validate VLANs; %s" % e)
def add_ucs_vlan(vlan_id): vlan_id_num = int(vlan_id) from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan if vlan_id == "1": message = ( "For the requested UCS Manager V Lan add operation, " + "V Lan 1 can be given additional names, however, this skill does not allow for that procedure." ) return message elif ((vlan_id_num <= 1) or (vlan_id_num >= 4030 and vlan_id_num <= 4048) or (vlan_id_num > 4093)): message = ("For the requested UCS Manager V Lan add operation, " + "the provided V Lan I D " + vlan_id + ", is not allowed.") return message ucsm_login() if status['login'] == "success": response = handle.query_dn("fabric/lan/net-vlan" + vlan_id) else: message = ("there was an error connecting to the UCS Manager, " + "please check the access credentials or IP address") return message if not response: fabric_lan_cloud = handle.query_dn("fabric/lan") vlan = FabricVlan(parent_mo_or_dn=fabric_lan_cloud, name="vlan" + vlan_id, id=vlan_id) handle.add_mo(vlan) handle.commit() response = handle.query_dn("fabric/lan/net-vlan" + vlan_id) if response and response.name == "vlan" + vlan_id: message = "V Lan " + vlan_id + " has been added to UCS Manager." else: message = "V Lan " + vlan_id + " was not added to UCS Manager." else: message = "V Lan " + vlan_id + " already exists on UCS Manager." ucsm_logout() return "For the requested UCS Manager V Lan add operation, " + message
def create_UCS_VLAN(vl_range): handle = UcsHandle(credentials.UCS_login['ipaddr'], credentials.UCS_login['username'], credentials.UCS_login['password']) handle.login() for vlan_id in vl_range: mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name="ACI-" + vlan_id, id=vlan_id, mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) handle.commit()
def main(): handle = UcsHandle("192.168.254.200", "ucspe", "ucspe", secure=False) handle.login() # Get Parrent Object lan_cloud = handle.query_classid("FabricLanCloud") # Create new VLAN Object for vlan 100 vlan_mo = FabricVlan(parent_mo_or_dn=lan_cloud[0], name="vlan100", id="100") # Add the object to be ready to be committed handle.add_mo(vlan_mo) # Commit changes back to UCS handle.commit() handle.logout()
#skeleton script #import ucshandle class from ucshandle from ucsmsdk.ucshandle import UcsHandle #init handle handle = UcsHandle("192.168.157.153", "admin", "password", secure=False) # Login handle.login() #####IPNUT GENERATED CODE from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name="vlan123", id="123", mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) handle.commit() # Logout after execution handle.logout()
# Here the environment variable is called 'MY_SECURE_PASSWORD' which # contains your secret password. username = '******' # UCS Manager username password = '******' # UCS Manager password # Create a connection handle # You can reserve the Cisco DEVNET UCS sanbox and use it # for the following example. handle = UcsHandle("10.10.20.113", username, password) # Login to the server handle.login() # Query the class FabricLanCloud which contains VLANs info fabric_lan_cloud = handle.query_classid("FabricLanCloud") # Create the VLAN 123 # 'id' and 'name' are mandatory arguments vlan123 = FabricVlan(parent_mo_or_dn=fabric_lan_cloud[0], name="vlan123", id="123") # Add the VLAN 123 object to the object handle using the add_mo method. handle.add_mo(vlan123) # Commit the change to add the VLAN 123 to the UCS Manager handle.commit() # Logout from the server handle.logout()
In addition to importing the UcsHandle module you will also need to import the FabricVlan module. Think of the module as a recipe for creating a VLAN. Create vlan100. Use these python statements. There is no output indicating successful completion of the operation, no error is an indication of a successful operation. However, if you wanted to ensure that the object was created just query for it. In this operation the add_mo() handle method was used with two arguments. The first argument, like set_mo() is the object, the second argument could have been written as modify_present=True. Since the modify_present is the second argument in the add_mo method definition Python lets up just provide the value without having to provide the argument name. modify_present allows the add_mo() method to act like a set_mo() and update the object if it already exists. By default modify_present is False meaning that if add_mo() was being used to create an object that already exists then the add operation would fail. ''' handle = UcsHandle("192.168.193.138", "ucspe", "ucspe", secure=False) lan_cloud = handle.query_classid("FabricLanCloud") vlan_mo = FabricVlan(parent_mo_or_dn=lan_cloud[0], name="vlan_1001", id="1001") handle.add_mo(vlan_mo, modify_present=True) handle.commit() ################ Create UCS Manager VLANs in a Transaction ############## ''' UCS Manager Python SDK commit() can commit more than one object at a time, in fact the objects can be of different classes. Objects can be continually added to the handle and a single call to commit() will commit them all. This is called a Transaction, a few caveats apply. If one object fails to be created everything up to that point is rolled back and nothing is created. Objects in the Transaction cannot have dependencies on each other. Objects only become visible to others users of UCS Manager when the Transaction is complete. UCS Manager is Transaction oriented by default adding objects to the handle and committing them is the standard process. Using Transactions for processes that create many objects is far more efficient than processing each object one at a time. Create vlan200, vlan201, vlan202 in a Transaction. Use these python statements.
Cisco Systems, Inc. """ from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan # Create a Login Handle and Login HANDLE = UcsHandle("13.58.22.56", "admin", "password") HANDLE.login() VLANS = ['200', '300', '400', '500'] # Iterate over the VLANS list instantiating a vlan_mo and adding # the vlan_mo to the HANDLE. The UCS Python SDK is transactional # by default. for vlan in VLANS: vlan_mo = FabricVlan(parent_mo_or_dn="fabric/lan", name="vlan" + vlan, id=vlan) # Add the current instantiated vlan_mo Object to the HANDLE # This action does not overwrite previous vlan_mo ojects in # the uncommited HANDLE HANDLE.add_mo(vlan_mo, modify_present=True) # Commit the HANDLE to add the VLANS to UCS Manager HANDLE.commit() # Logout HANDLE.logout()
i=1 while i < len(VLAN_ID): print VLAN_ID[i] i = i + 1 else: print "Bad Robot" #Create Sub Organization mo = OrgOrg(parent_mo_or_dn="org-root", name=my_Org, descr="Sub Organization") handle.add_mo(mo) handle.commit() #Create Production VLANs k = 1 while k < len(VLAN_ID): mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name=VLAN_Name[k], id=VLAN_ID[k], mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) handle.commit() print k k = k+1 #Create UUID Pool mo = UuidpoolPool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", prefix="derived", descr="UUID Pool", assignment_order="sequential", name="UUID_POOL") mo_1 = UuidpoolBlock(parent_mo_or_dn=mo, to="0001-000000000100", r_from="0001-000000000001") handle.add_mo(mo) handle.commit() #Create a Server Pool mo = ComputePool(parent_mo_or_dn=my_Full_Path_Org, policy_owner="local", name="Server_Pool", descr="Server Pool") mo_1 = ComputePooledSlot(parent_mo_or_dn=mo, slot_id="1", chassis_id="1") mo_2 = ComputePooledSlot(parent_mo_or_dn=mo, slot_id="2", chassis_id="1")
#mo = handle.query_dn("org-root/org-Sub_Org_Name") #handle.remove_mo(mo) #handle.commit() ##### End-Of-PythonScript ##### #create a vlan ##### Start-Of-PythonScript ##### #create vMotion VLAN (VLAN ID 50) from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name="vMotion", id="50", mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) handle.commit() #Create Management VLAN (VLAN ID 51) mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name="Mgmt", id="51", mcast_policy_name="", policy_owner="local", default_net="no",
def vlan(input): name = input['name'] sharing = "none" id = input['id'] mcast_policy_name = "" policy_owner = "local" default_net = input['default_net'] pub_nw_name = "" compression_type = "included" state = input['state'] ip = input['ip'] username = input['username'] password = input['password'] results = {} ucs_handle = pickle.loads(str(ucs_login.main(ip, username, password))) ###-------CHECK IF MO EXISTS--------------------------------- try: mo = ucs_handle.query_dn("fabric/lan/net-" + name) except: print("Could not query children of vlan") ###----if expected state is "present"------------------------ if state == "present": if mo: if (mo.name == name and mo.sharing == sharing and mo.id == id and mo.mcast_policy_name == mcast_policy_name and mo.policy_owner == policy_owner and mo.default_net == default_net and mo.pub_nw_name == pub_nw_name and mo.compression_type == compression_type): results['name'] = name results['expected'] = True results['changed'] = False results['present'] = True #results['mo_bootpolicy'] = json.dumps(json.loads(jsonpickle.encode(mo))); else: try: mo.sharing = sharing mo.id = id mo.mcast_policy_name = mcast_policy_name mo.policy_owner = policy_owner mo.default_net = default_net mo.pub_nw_name = pub_nw_name mo.compression_type = compression_type results['name'] = name results['expected'] = False results['changed'] = True results['present'] = True ucs_handle.set_mo(mo) ucs_handle.commit() #results['mo_bootpolicy'] = json.dumps(json.loads(jsonpickle.encode(mo))); except: module.fail_json(msg="Modification of vlan mo failed") ###----------if not, create boot policy with desired config ---------------- else: try: mo = FabricVlan(parent_mo_or_dn="fabric/lan", name=name, sharing=sharing, id=id, mcast_policy_name=mcast_policy_name, policy_owner=policy_owner, default_net=default_net, pub_nw_name=pub_nw_name, compression_type=compression_type) results['name'] = name results['present'] = False results['created'] = True results['changed'] = True #results['mo_bootpolicy'] = json.dumps(json.loads(jsonpickle.encode(mo))); ucs_handle.add_mo(mo) ucs_handle.commit() except: print("Vlan creation failed") ###------if expected state is "absent"---------------------------- if state == "absent": if mo: try: ucs_handle.remove_mo(mo) results['name'] = name results['present'] = False results['removed'] = True ucs_handle.commit() except: print("Remove Vlan mo failed") else: results['name'] = name results['removed'] = False results['present'] = False ucs_handle = pickle.dumps(ucs_handle) ucs_logout.main(ucs_handle) return results
def ucs_add_vlan_range(module): ucsm_ip = module.params.get('ip') ucsm_pw = module.params.get('password') ucsm_login = module.params.get('login') vlan_name = module.params.get('vlan_name') vlan_range = module.params.get('vlan_range') mcast_policy_name = module.params.get('mcast_policy_name') policy_owner = module.params.get('policy_owner') configure_lan_seperate = module.params.get('configure_lan_seperate') vlan_a_range = module.params.get('vlan_a_range') vlan_b_range = module.params.get('vlan_b_range') results = {} ucsm = UCS(ucsm_ip, ucsm_login, ucsm_pw) results = {} # Login to UCSM try: ucsm.login() results['logged_in'] = True except Exception as e: module.fail_json(msg=e) if vlan_range: vlan_range_split = vlan_range.split('-') vlan_start = int(vlan_range_split[0]) vlan_end = int(vlan_range_split[1]) + 1 vlan_list = list(range(vlan_start, vlan_end)) elif vlan_a_range: vlan_a_range_split = vlan_a_range.split('-') vlan_a_start = int(vlan_a_range_split[0]) vlan_a_end = int(vlan_a_range_split[1]) + 1 vlan_a_list = list(range(vlan_a_start, vlan_a_end)) if vlan_b_range: vlan_b_range_split = vlan_b_range.split('-') vlan_b_start = int(vlan_b_range_split[0]) vlan_b_end = int(vlan_b_range_split[1]) + 1 vlan_b_list = list(range(vlan_b_start, vlan_b_end)) if configure_lan_seperate == 'no': for vlan in vlan_list: mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name=vlan_name + str(vlan), id=str(vlan), mcast_policy_name=mcast_policy_name, policy_owner=policy_owner, default_net="no", pub_nw_name="", compression_type="included") try: ucsm.handle.add_mo(mo) results['changed'] = True except Exception as e: module.fail_json(msg=e) results['changed'] = False else: for vlan in vlan_a_list: mo = FabricVlan(parent_mo_or_dn="fabric/lan/A", sharing="none", name=vlan_name + str(vlan), id=str(vlan), mcast_policy_name=mcast_policy_name, policy_owner=policy_owner, default_net="no", pub_nw_name="", compression_type="included") try: ucsm.handle.add_mo(mo) results['changed'] = True except Exception as e: module.fail_json(msg=e) results['changed'] = False for vlan in vlan_b_list: mo = FabricVlan(parent_mo_or_dn="fabric/lan/B", sharing="none", name=vlan_name + str(vlan), id=str(vlan), mcast_policy_name=mcast_policy_name, policy_owner=policy_owner, default_net="no", pub_nw_name="", compression_type="included") try: ucsm.handle.add_mo(mo) results['changed'] = True except Exception as e: module.fail_json(msg=e) results['changed'] = False #Committing Changes ucsm.handle.commit() try: ucsm.handle.logout() results['logged_out'] = True except Exception as e: module.fail_json(msg=e) return results
# Add Vlans in a transaction from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan handle = UcsHandle("198.18.133.91", "admin", "C1sco12345") handle.login() fabric_lan_cloud = handle.query_classid("FabricLanCloud") vlan_ids = ['300', '301', '302', '303', '304', '305'] for vlan_id in vlan_ids: add_vlan = FabricVlan(parent_mo_or_dn=fabric_lan_cloud[0], name="vlan" + vlan_id, id=vlan_id) handle.add_mo(add_vlan) handle.commit() handle.logout() input("Vlans Added - Press Enter to Continue to Set Vlans...") # Set Vlans is a transaction from ucsmsdk.ucshandle import UcsHandle handle = UcsHandle("198.18.133.91", "admin", "C1sco12345") handle.login() vlan_ids = ['300', '301', '302', '303', '304', '305']
from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan handle = UcsHandle('192.168.174.168', 'ucspe', 'ucspe') mo = FabricVlan(parent_mo_or_dn='fabric/eth-estc', id='101', name='datacenter', sharing='none') handle.add_mo(mo) handle.commit()
def ucs_add_vlan(module): vlan_name = module.params.get('vlan_name') vlan_id = module.params.get('vlan_id') mcast_policy_name = module.params.get('mcast_policy_name') policy_owner = module.params.get('policy_owner') configure_lan_seperate = module.params.get('configure_lan_seperate') vlan_a = module.params.get('vlan_a') vlan_b = module.params.get('vlan_b') ucsm_ip = module.params.get('ip') ucsm_pw = module.params.get('password') ucsm_login = module.params.get('login') ucsm = UCS(ucsm_ip, ucsm_login, ucsm_pw) results = {} #Login to UCSM try: ucsm.login() results['logged_in'] = True except Exception as e: module.fail_json(msg=e) # # Checking if configured for lan seperate per FI or not. # if configure_lan_seperate == 'no': mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name=vlan_name + str(vlan_id), id=vlan_id, mcast_policy_name=mcast_policy_name, policy_owner=policy_owner, default_net="no", pub_nw_name="", compression_type="included") try: ucsm.handle.add_mo(mo) ucsm.handle.commit() results['changed'] = True except Exception as e: module.fail_json(msg=e) results['changed'] = False else: mo = FabricVlan(parent_mo_or_dn="fabric/lan/A", sharing="none", name=vlan_name + str(vlan_a), id=str(vlan_a), mcast_policy_name=mcast_policy_name, policy_owner=policy_owner, default_net="no", pub_nw_name="", compression_type="included") ucsm.handle.add_mo(mo) mo = FabricVlan(parent_mo_or_dn="fabric/lan/B", sharing="none", name=vlan_name + str(vlan_b), id=str(vlan_b), mcast_policy_name=mcast_policy_name, policy_owner=policy_owner, default_net="no", pub_nw_name="", compression_type="included") ucsm.handle.add_mo(mo) try: ucsm.handle.commit() results['changed'] = True except Exception as e: module.fail_json(msg=e) results['changed'] = False try: ucsm.handle.logout() results['logged_out'] = True except Exception as e: module.fail_json(msg=e) return results
Purpose: UCS Manager VLAN creation example Author: John McDonough ([email protected]) github: (@movinalot) Cisco Systems, Inc. """ from ucsmsdk.ucshandle import UcsHandle from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan # Create a Login Handle and Login HANDLE = UcsHandle("13.58.22.56", "admin", "password") HANDLE.login() # Query the FabricLanCloud, under which VLAN Objects are inserted FABRIC_LAN_CLOUD = HANDLE.query_classid("FabricLanCloud") # Instantiate a VLAN Object, minimally "id" and "name" are required VLAN = FabricVlan(parent_mo_or_dn=FABRIC_LAN_CLOUD[0], name="vlan100", id="100") # Add the instantiated VLAN Object to the HANDLE HANDLE.add_mo(VLAN, modify_present=True) # Commit the HANDLE to add the VLAN to UCS Manager HANDLE.commit() # Logout HANDLE.logout()
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( vlan_list=dict(type='list'), name=dict(type='str'), id=dict(type='str'), native=dict(type='str', choices=['no', 'yes']), fabric=dict(type='str', choices=['common', 'A', 'B']), multicast_policy=dict(type='str'), sharing=dict(type='str', choices=['none', 'primary', 'isolated', 'community']), state=dict(type='str', default='present', choices=['present', 'absent'])) module = AnsibleModule(argument_spec, supports_check_mode=True, required_one_of=[['vlan_list', 'name']], mutually_exclusive=[['vlan_list', 'name']], required_together=[['name', 'id']]) ucs = UCSModule(module) err = False from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan changed = False try: if module.params['vlan_list']: # directly use the list (single resource and list are mutually exclusive vlan_list = module.params['vlan_list'] else: # single resource specified, create list from the current params vlan_list = [module.params] for vlan in vlan_list: exists = False # set default params. Done here to set values for lists which can't be done in the argument_spec if not vlan.get('native'): vlan['native'] = 'no' if not vlan.get('fabric'): vlan['fabric'] = 'common' if not vlan.get('sharing'): vlan['sharing'] = 'none' if not vlan.get('multicast_policy'): vlan['multicast_policy'] = '' # 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 vlan['fabric'] != 'common': dn_base += '/' + vlan['fabric'] dn = dn_base + '/net-' + vlan['name'] mo = ucs.login_handle.query_dn(dn) if mo: # check top-level mo props kwargs = {} kwargs['id'] = vlan['id'] kwargs['default_net'] = vlan['native'] kwargs['sharing'] = vlan['sharing'] kwargs['mcast_policy_name'] = vlan['multicast_policy'] if (mo.check_prop_match(**kwargs)): 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 mo = FabricVlan( parent_mo_or_dn=dn_base, name=vlan['name'], id=vlan['id'], default_net=vlan['native'], sharing=vlan['sharing'], mcast_policy_name=vlan['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)