Getting started with VMwares ESXi/vSphere API in Python
In 2013 VMware dropped their Python library for accessing the API for ESXi/vSphere on GitHub. This is great, however it isn’t the easiest library in the world to use. This quick guide will show you how to connect to an ESXi host or vSphere cluster and get some info about a virtual machine.
# Install newest version from pypi pip install pyvmomi # Install latest source from GitHub pip install git+https://github.com/vmware/pyvmomi.git
Now in Python you will be able to import the library.
Connecting to a host or cluster
To connect to your host or cluster you must identify the API end point, this will usually be port 443 on the ip of your host or vSphere appliance.
from pyVim import connect my_cluster = connect.Connect("192.168.2.171", 443, "username", "password")
If you are running your host without SSL configured correctly, in a home lab for example, you will get the error
[SSL: CERTIFICATE_VERIFY_FAILED]. To work around this ensure you have installed the latest source from GitHub, or a version greater than
v22.214.171.1246.4, and use the
connect.ConnectNoSSL() method instead.
You should also disconnect your session when you are done with it.
Querying a VM
Once you are connected to your cluster you will be able to find your VMs, get their information, issue commands and more. It is a very powerful api so be careful.
To get a VM object or a list of objects you can use the searchIndex class. The class had methods to search for VMs by UUID, DNS name, IP address or datastore path.
from pyVim import connect # Connect to a cluster or host my_cluster = connect.Connect("192.168.2.171", 443, "username", "password") # Get a searchIndex object searcher = my_cluster.content.searchIndex # Find a VM vm = searcher.FindByIp(ip="192.168.70.54", vmSearch=True) # Print out vm name print vm.config.name # Disconnect from cluster or host connect.Disconnect(my_cluster)
In the above example we connected to a host or cluster, found a VM and printed out it’s name. We could easily have started, stopped or even destroyed it.
Once you have run through this guide I’m sure you’ll be itching to explore the power of this API. You can find a full list of all the objects included in the library here.
Let me know what you think of this article on twitter @_jacobtomlinson or leave a comment below!