It’s quite easy to get started, the pyVmomi website has detailed instructions but in practical terms it’s as simple as :
pip install --upgrade pyvmomi
Key concepts for pyVmomi?
It’s important to remember that pyVmomi is the python bindings for the vSphere API. It is quite a comprehensive set of bindings but it can take a little while to get used to it. The same is true for any of the other language bindings. Once you understand the concepts you will find it to be very well thought out and executed. It is beyond the scope of this blog post to perform an in-depth study of the vSphere API however through the examples you will begin to increase your ability to interact with the API to create novel solutions to problems you may have.
Connecting to the vSphere API
from pyVim.connect import SmartConnect, Disconnect
SI = SmartConnect(host="192.168.10.11",
In the above example the connection is to a VCSA appliance using the default credentials, this could’ve easily been directly to an ESXi host but it’s important to note that while the interface uses the same mechanism the data returned is unlikely to be as extensive as connecting to the VCSA.
Creating a more robust way to interact with the vSphere API
While you can certainly access the pyVmomi SDK in a purely procedural manner I personally prefer an object oriented approach.
For this first example in the series we’ll create a vCenter object that we’ll in turn use to interact with the environment.
The ‘requirements’ for this program is to utilize a small YAML file that contains configuration information and connect to the vSphere environment and implement this configuration. For the first installment we’ll simply establish the connection to the VCSA and print out the version details.
YAML configuration file
Using the pyVmomi python bindings for vSphere connect to a VCSA and report the version information
def __init__(self, vcenter_params):
self.pyVmomi = __import__("pyVmomi")
self.server = vcenter_params['ip']
self.username = vcenter_params['user']
self.password = vcenter_params['pw']
from pyVim import connect
print("Connecting to %s using username %s" % (self.server, self.username))