Showing results for 
Search instead for 
Did you mean: 


Python SDK handle.commit() Practice


I'm working on an initial UCS manager configuration script for a client using UCSMSDK.

When using handle.commit(), is it a better practice to use handle.commit() for frequent small changes. Or less frequent larger changes?

For an example when creating multiple VLANS, a managed object is created for each VLAN and handle.add_mo() is used to stage the new mo. Would it be better to commit the new mo immediately or wait until managed objects have been created and staged and commit all the new VLAN mo's at once?


The Cisco UCS Object Model is a representation of all UCS objects logical or physical. The objects are all very similar, so much so that the code that makes up the UCS Python SDK and the UCS PowerTool Suite are ninety-nine percent generated. 99% is a lot of similarities but you would never mistake a VLAN object for a Service Profile object, or a Boot Policy object for a MAC Pool object. The similarities come in the form of structure, properties, privileges, processing, etc. These similarities and the generated UCS Python SDK code are what I am going to focus on to show you how to write less code.
Mostly we write code that is object-specific. For example, if you want to add a VLAN to a UCS Manager using the UCS Python SDK you need to:

Query for the FabricLanCloud so you know where to put the new VLAN
Import the FabricVlan module
instantiate an instance of the FabricVlan class, minimally setting these attributes
Add the VLAN object instance to the UCS connection handle
Commit the handle
If all went well the new VLAN is added to the Fabric LAN Cloud. The code below shows this process including the UCS handle creation code.
>from ucsmsdk.ucshandle import UcsHandle
>from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan
>handle = UcsHandle("", "admin", "password")
>fabric_lan_cloud = handle.query_dn("fabric/lan")
>vlan_100 = FabricVlan(parent_mo_or_dn=fabric_lan_cloud,
There is nothing wrong with this code. It could be enhanced with exception handling and parameterization of username, password, UCS IP, VLAN ID, and VLAN Name. You could also add validation of those parameters as well as some command line options for the parameters. Then, after you did all that, you would have a Python program that adds a VLAN to UCS Manager. Considering there are thousands and thousands of UCS Manager objects you better stock up on whatever fuels your development sessions.

Recognize Your Peers
Content for Community-Ad