Managing vSphere with pyVmomi - Part 1

This blog post is the first is a small series of articles about how to use pyVMomi to build, configure and manage vmware vsphere environments.

What is pyVmomi?

pyVmomi is the Python SDK for the VMware vSphere API that allows you to manage ESX, ESXi, and vCenter.

The website for pyVmomi can be found at : []

Getting started with pyVmomi?

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

import atexit

from pyVim.connect import SmartConnect, Disconnect

SI = SmartConnect(host="",

atexit.register(Disconnect, SI)

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

1cat labconfig.yaml
3    vsphere:
4        vcenter:
5                   ip:
6                   user: administrator@vsphere.local
7                   pw: vmware

 3Using the pyVmomi python bindings for vSphere connect to a VCSA and report the version information
 6import atexit
 7import yaml
 8import inspect
10class Vcenter(object):
11    def __init__(self, vcenter_params):
12        self.pyVmomi =  __import__("pyVmomi")
13        self.server = vcenter_params['ip']
14        self.username = vcenter_params['user']
15        self.password = vcenter_params['pw']
16        self.connect_to_vcenter()
18    def connect_to_vcenter(self):
19        from pyVim import connect
21        print("Connecting to %s using username %s" % (self.server, self.username))
22        self.service_instance = connect.SmartConnect(host=self.server,
23                                                user=self.username,
24                                                pwd=self.password,
25                                                port=443)
26        self.content = self.service_instance.RetrieveContent()
27        about = self.service_instance.content.about
28        print("Connected to %s, %s" % (self.server, about.fullName))
29        atexit.register(connect.Disconnect, self.service_instance)
32def main():
34    config = yaml.load(open("labconfig.yaml"))
36    vc=Vcenter(config["lab"]["vsphere"]["vcenter"]);
38    return 0
40# Start program
41if __name__ == "__main__":
42    main()
$ ./
Connecting to using username administrator@vsphere.local
/usr/lib/python2.7/site-packages/urllib3/ InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See:
Connected to, VMware vCenter Server 5.5.0 build-2183111

Other parts in the series can be found below :

  1. Part 1 :- Connecting to the VCSA
  2. Part 2 :- Creating Datacenters and Clusters
  3. Part 3 :- Adding Hosts to Clusters
  4. Part 4 :- Adding a NFS Share to a Cluster of Hosts