The Cisco UCS Python SDK is a Python module that helps automate all aspects of Cisco UCS management, including server, network, storage, and hypervisor management.
The bulk of the Cisco UCS Python SDK works on the UCS Manager’s Management Information Tree (MIT), performing create, modify, or delete actions on the managed objects (MO) in the tree.
Install Python 3 and above and pip before installing ucsmsdk. After Python and pip are installed, install the lastest version of the SDK from pypi using following command:
pip install ucsmsdk
You can also install the latest developer version from Github using the following commands:
git clone https://github.com/CiscoUcs/ucsmsdk/
cd ucsmsdk
sudo make install
For login and logout from the UCS Manager, you need to import the UCSHandle class. The following example shows how to create a connection handle before you can log in and log out from the server.
from ucsmsdk.ucshandle import UCSHandle
# Create a connection handle
handle = UcsHandle(“192.168.1.1”, “admin”, “password”)
# Login to the server
handle.login()
# Logout from the server
handle.logout()
The SDK provides APIs to enable CRUD operations:
Create an object: add_mo
Retrieve an object: query_dn, query_classid, query_dns, query_classids
Update an object: set_mo
Delete an object: delete_mo
The preceding APIs can be bunched together in a transaction (All or None). The commit_mo operation commits the changes made using the preceding APIs. All these methods are invoked on a UCSHandle instance.
The following example creates a new service profile (LsServer) object under the parent org-root. You create managed objects by using the add_mo API:
from ucsmsdk.mometa.ls.LsServer import LsServer
sp = LsServer(parent_mo_or_dn=”org-root”, name=”sp_demo”)
handle.add_mo(sp)
The following example shows how to query an existing Mo using a distinguished name (DN) and update it:
# Query for an existing Mo
sp = handle.query_dn(“org-root/ls-sp_demo”)
# Update description of the service profile
sp.descr = “demo_descr”
# Add it to the on-going transaction
handle.set_mo(sp)
The following example shows the use of remove_mo in removing an object:
# Query for an existing Mo
sp = handle.query_dn(“org-root/ls-sp_demo”)
# Remove the object
handle.remove_mo(sp)
Note
API operations are batched together by default until a commit() is invoked.
In the following code, the objects are created only when a commit() is invoked. If there is a failure in one of the steps, no changes are committed to the server.
from ucsmsdk.mometa.ls.LsServer import LsServer
sp1 = LsServer(parent_mo_or_dn=”org-root”, name=”sp_demo1”)
handle.add_mo(sp1)
sp2 = LsServer(parent_mo_or_dn=”org-root”, name=”sp_demo2”)
handle.add_mo(sp2)
# commit the changes to server
handle.commit()
Leave a Reply