I usually enjoy working on vSphere Java SDK & PowerCLI but recently I thought to explore pyVmomi. What is “pyVmomi”? It is an open source Python SDK for the VMware vSphere API that allows you to manage ESXi and vCenter server. I was able to get started with pyvmomi on ubuntu distro successfully and I thought it is worth to share the steps I performed with you. This guide is specific to setting up pyvmomi enviornment on ubuntu distro. Here we go.
On high level we need to have below prerequisites
– Ubuntu distro installed VM
You can see, python version available is 3.5.2. Since we have python already available, next step is to install pip. What is pip? As per Wikipedia, pip is a package management system used to install and manage software packages written in Python.
There is a python script which installs pip module, hence first we need to download that python script i.e. get-pip.py as follows
Now that we have downloaded get-pip.py script, lets run it to install latest pip as follows
you could see, there was existing pip installation i.e. pip 8.1.1, which is uninstalled now and script installed latest pip-9.0.1. I think, it is better to have latest pip. Above step also confirms that our python environment is running fine.
As per above screenshot, pyVmomi, pyvim and all related modules got installed successfully.
Now its time to write a sample vSphere API script : I leveraged one of samples available in community & simplified it for your easy understanding.
# VMware vSphere Python SDK, pyvmomi
# Simple script to get vCenter server product details
from pyVim.connect import SmartConnect
si= SmartConnect(host="10.192.1.2", user="Administrator@vsphere.local", pwd="$h1vKamal", sslContext=s)
print ("Product Name:",aboutInfo.fullName)
print("Product Unique Id:",aboutInfo.instanceUuid)
print("Product Base OS:",aboutInfo.osType)
vmware@localhost:~$ python3 About.py
Product Name: VMware vCenter Server 6.5.0 build-5973321
Product Build: 5973321
Product Unique Id: ef56e4fd-5705-40da-93e7-d1cc57700d41
Product Version: 6.5.0
Product Base OS: linux-x64
Product vendor: VMware, Inc.
Lets take a look at this sample script.
Line 1: pyVim is part of pyvmomi and is responsible for connection creation and deletion. What else it is responsible can be learned here
Line 5,6,7: These lines are responsible for handling SSL certificate stuff, more on this in later posts.
Line 8: It will get us a connection with vCenter or ESXi server. It will in fact get us the top level vSphere API object i.e. ServiceInstance. Please take a look at vSphere API reference.
Line 9 onwords: We are fetching a “about” property of “ServiceContent” object
To clone all the pyvmomi API samples repo into your local environment, run below command
-You may have to deal with Python 2.7.x and 3.x syntax differences.
-If you want to make pyVmomi work with python 2.x version, just replace “python3” with “python” for commands used to install pyvim and pyvmomi modules. Also “pip3” with “pip”. My environment works for both python versions.
1. To have more fun with vSphere APIs, it is must that you should be friendly with vSphere API reference
2. Update: Please take a look at tutorial on how to get hold of vSphere objects and play around
I am enjoying exploring further on pyvmomi, let me know about your experience. Please stay tuned for more pyvmomi API sample posts.
Vikas Shitole is a Staff engineer 2 at VMware India R&D. He currently focusses on vCenter server in general, vSphere with Tanzu, VCF and partly VMware cloud. He is passionate about VMware customers & automation around VMware technologies. He has been a vExpert since last 9 years (2014-22) in row for his significant community contributions around VMware. He is also part of BU driven vExpert sub-programs i.e. vExpert App modernization, vExpert VMware Cloud on AWS & vExpert PRO as well. He is author of 2 VMware flings & holds multiple technology certifications. He has been speaker at International conferences such as VMworld Europe, VMworld USA & was designated VMworld 2018 blogger as well.
In addition, he is passionate cricketer, bicycle rider & aspiring Ironman 70.3. He also enjoys learning about fitness & nutrition.