You already might have noticed, as part of vSphere 6.5, VMware introduced vCenter Server REST APIs. I really enjoyed playing around them using vCenter apiexplorer as well as Postman REST client. Recently, I wanted to code around these APIs using one of the programming languages and I am happy that I was able to do it using Python. I thought it is worth to share with you. In this blog post, I will take you through all the steps required to get started with vCenter REST API using python. Here we go.
Step 1. First important thing is to get familiar with vCenter server REST API documentation. Similar documentation is available from vCenter apiexplorer as well. I would recommend you to play with apiexplorer, which will not only make you familiar with documentation but also will enable you to quickly invoke these APIs against your vCenter server. I am sure you will be pretty impressed on REST API documentation when compared to older vSphere SOAP API documentation.
Step 2. Once you explore the first step , we can get started with environment creation for REST APIs using python. If you ask me, it is fairly easy and quick, we just need to have python installed machine, where we need to install “requests” python module as follows
$ pip install requests
Step 3. Now let us take a look at below python module developed to simplify REST API usage.
# Author: Vikas Shitole
# Website: www.vThinkBeyondVM.com
# Product: vCenter server
# Description: Python module for vCenter server REST APIs
# How to setup vCenter REST API environment?: Just have VM with python and install "requests" python library using pip
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# Function to get the vCenter server session
# Function to get all the VMs from vCenter inventory
#Function to power on particular VM
# Function to power off particular VM
Above vcrest.py module is available on my github repo.
Let us understand above code.
Line 8: Imported powerful “requests” python library required to make API calls
Line 9: Imported “json” library required to parse json response we get from REST APIs
Line 10/11: Here we are disabling warnings related to SSL connection. In production, we should not disable it.
Line 13/14: Here we are creating Session object to have session persisted during the current request. If you see “s.verify” is set to False, it does mean that we are ignoring verifying SSL certificates. If you want to set it to true, please take a look at SSL Cert Verification section
Line 16 to 32: I have added 4 methods i.e. get_vc_session(), get_vms(), poweron_vm() & poweroff_vm(). We would be calling these methods from below sample script. If you see, in all the methods, I have used REST API documentation and called these APIs using “requests” library.
Step 4. Now that we understood above “vcrest.py” module, let us import above module into our script to demonstrate its usage.
# Description: Python sample to get VMs and its moid using vCenter server REST API.
# Make sure you have "rest.py" file into your python directory.
vcip="10.192.23.143" # vCenter server ip address/FQDN
#Get vCenter server session and can be used as needed. pass vcenter username & password
vcsession = vcrest.get_vc_session(vcip,"Administrator@vsphere.local","VMware1!")
#Get all the VMs from inventory using below method from "vcrest" module.
vms = vcrest.get_vms(vcip)
# Parsing the JSON response we got from above function call (it has all the Vms present in inventory
print "VM names and its unique MOID"
for vm in json_data:
print vm.get("name")+" :: "+vm.get("vm")
#We are powering on all the VMs those are in powered off state
if vm.get("power_state") == "POWERED_OFF":
Above script i.e. vcrestsample.py is available on my github repo as well
vmware@localhost:~$ python vcrestsample.py
VM names and its unique MOID
NTP-India-1 :: vm-42
NTP-PA-2 :: vm-43
WebApp-1 :: vm-44
vThinkBeyondVM :: vm-45
Let us understand above script.
Line 5: Imported “vcrest” module we just discussed above.
Line 10: We are getting vCenter server session by calling function defined in “vcrest” module. We can use this session object as needed.
Line 13: We are getting all the VMs from inventory using “get_vms() function defined in “vcrest” module. Note that with this call, we will get JSON response as shown below, which we need to parse to fetch useful information.
Line 16/17: As we got JSON response as pointed above, here we parsed it so that we can easily access as part of python dictionary.
Line 21 to 25: Iterating through dictionary and printing vm names & its moid (managed object id). Finally powering on VMs those are off.
That is all, is not it cool? Since we have REST APIs available for vCenter VM life cycle, VCSA, content library, tagging etc, there is lot to learn and play around. I will keep adding more methods to vcrest.py module. If you are interested in contributing to this module, let me know, it would be really great. In case, you would like to explore vCenter SOAP based APIs, please refer my last post
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.