Introduction to UCS Python SDK Part II 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 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 more 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, create object and generate code from UCS Manager GUI interactions 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 2a - Review the generated script
Python - convert_to_ucs_python() generated code example 1h output from ucsm-python-sdk-101
##### Start-Of-PythonScript ##### from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name="vlan10", id="10", mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) handle.commit() ##### End-Of-PythonScript #####
Python - convert_to_ucs_python() generated code with UCS login and logout code added and vlan name and id changed.
# Import the "UcsHandle" class from the "ucshandle" module in the ucsmsdk directory from ucsmsdk.ucshandle import UcsHandle # init handle and login handle = UcsHandle("<ucs-manager-ip>","admin","password",secure=False) handle.login() # Insert Generated Script below this line ##### Start-Of-PythonScript ##### from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan mo = FabricVlan(parent_mo_or_dn="fabric/lan", sharing="none", name="vlan11", id="11", mcast_policy_name="", policy_owner="local", default_net="no", pub_nw_name="", compression_type="included") handle.add_mo(mo) handle.commit() ##### End-Of-PythonScript ##### # Logout after script is executed handle.logout()
Locate c:\ucs_scripts\python\skeleton1.py in Windows File Manager, right click and edit in the IDLE Editor:
Notice that the script contains the ability to login to the UCS Domain and logout of the UCS Domain.
Exercise 2b - Copy convert_to_ucs_python() output into skeleton script
The skeleton1.py script should be open in your Python IDLE editor for this next task, and you will need the Python code discovered after exercise 1h, from ucsm-python-sdk-101
If you do not have the code output or closed the shell session, the discovered code is available in c:\ucs_scripts\python\example_1h_output
Paste the code into your skeleton1.py file (open in the IDLE editor), in the defined area:
Exercise 2c - Edit parameters and execute script to create a new VLAN object
Since a VLAN named "vlan10" with ID "10" may already exist in the UCS Domain (created in example 1h from ucsm-python-sdk-101), we cannot execute the script as is or we will get an error, indicating that this object already exists.
Edit some properties before executing
- ensure the vlan name and id are changed
- ensure the UCS Manager IP address is correct
Change the VLAN Name to "vlan11" and the VLAN ID to "11":
Once the edits are made, Click "Run", then "Run Module":
You will be prompted that "Source Must Be Saved" – Click OK:
You will then see a new Python shell spawn, containing an indication that this code was executed, and you should then see your new VLAN object appear in the UCS Manager GUI:
Congratulations – now you can use this methodology to learn how to automate anything that you create in the in the UCS Manager Java GUI!