Introduction to UCS Python SDK Part I Cisco DevNet Learning Labs
UCS Python SDK is a set of Python modules, each containing one or more classes, developed specifically to automate UCS Manager. UCS Python SDK is developed with PEP8 compliance and supports every Managed Object exposed by the UCS XML Object Model . A special function convert_to_ucs_python() enables automatic Python script generation from operations performed in the UCS Manager GUI. Use convert_to_ucs_python() to quickly script UCS tasks.
Cisco UCS is an Integrated Compute Infrastructure that has supported an XML API since its introduction. UCS Manager is the service that provides the XML API endpoint along with a graphical interface and a command line. The UCS components both physical and logical are represented as XML objects in a hierarchical Object Model format and can be manipulated via the XML API.
All UCS objects are described in an XML schema, this schema defines the objects their attributes and associated values. The schema is also used to generate a significant amount of the UCS Manager Python SDK. Because a UCS component must be part of the Object Model to be part of the UCS system all components of UCS are always available via the XML API.
This Introduction to UCS Python Learning Lab will guide you through the use of UCS Python SDK to connect to a UCS Manager, perform queries against UCS Manager objects and disconnect from the UCS Manager. UCS Manager Objects are representations of pools, policies, other logical constructs and physical entities.
Completion time: 30 minutes
Prior to starting this learning lab it would be helpful to understand UCS Manager and the functionality that it provides to manage UCS compute environments.
An understanding of Python would also be helpful.
UCS Python SDK
UCS Python SDK is a library of python code that enables the retrieval and manipulation of UCS Manager managed objects. UCS Manager API interaction can be categorized into several distinct sections.
- Queries and Filters
- Configurations and Transactions
- Event Subscription
UCS Python SDK provides modules, classes and functions for all the categorizations of UCS Manager API interactions, along with functions that allow for the inspection of object metadata and object hierarchical containment.
UCS Python SDK Download and Documentation
UCS Python SDK is a community supported project, the code, documentation and samples repositories are on github.com.
Event Infrastructure Utilization
If you are utilizing event provided infrastructure a script has been provided to reset the environment to pre-lab settings.
On the desktop Double-click the
Reset-Ucs-LL shortcut, provide the UCS Manager IP address that was assigned to you for the Learning Lab and hit Enter
Repeat this process for each assigned IP address, if you were assigned more than one.
Exercise 1a - Explore – list modules / packages in Python SDK
If you are going through this lab at an event like Cisco Live and using Cisco supplied desktop, Python 2.7 has been pre-installed, along with the Cisco UCS Python SDK. - Official Documentation for the UCS Python SDK
Open Windows File Manager or a Command prompt session to locate and view the Python modules and associated python scripts available with the Cisco UCS Python SDK (note: all third party Python modules are typically located in the subdirectory: \Lib\site-packages)
In addition to the core Python Modules, the SDK includes a comprehensive set of samples, grouped in subfolders by their use/function (ex: admin, firmware, network, reports, server)
Exercise 1b - Explore – list classes / managed objects in the UCS Python SDK
Under the ucsmsdk directory, you will find the "mometa" directory (Managed Object metadata). If you look inside any of the subdirectories (ex: fabric), you will see modules/scripts that map to all the classes available in the UCS Manager XML Model:
The UCS Manager Python SDK abstracts these low level modules/scripts.
- Ultimately, as administrators use the UCSM Python SDK, these low level modules/scripts are being leveraged to create/modify/delete and query objects in the UCS Manager XML Model.
Exercise 1c - Launch Python Interactive Shell (IDLE)
Click the Windows Start button and type "python" to search for the Python IDLE editor:
Exercise 1d - Getting help
To begin using the SDK, users will need to import classes into a running Python shell environment.
As mentioned earlier, all third party-modules are located under Lib\site-packages, and so you will see the ucsmsdk and ucsmsdk_samples directories below in C:\Python27\Lib\site-packages\
In this example, we will explore the ucshandle module, view associated classes/functions and then view the available help data.
Notice that there is a Compiled Python file and Python file (script) associated with each module:
To use the "ucshandle" module, we will need to import the module, but let’s first open the ucshandle Python File (script).
We can do this by right clicking on the ucshandle Python file, and selecting to open/edit this file in the IDLE Editor:
You will find that this modules defines the "UcsHandle" Class.
To import this module, and then view the available help, use the following usage:
from <directory>.<module_name> import <Class or Function>
In this example, we are going to import the "UcsHandle" function from the "ucshandle" module and view the help.
To view the help of the "ucshandle" module (used to login/logout of a UCS Domain), run the following:
from ucsmsdk.ucshandle import UcsHandle help(UcsHandle)
The help function provides information on all available arguments, including use examples.