Getting Started With PyVmomi

It does seem that there is always a new programming language to learn. I do wish that I had done some real programming courses when I was a student. My Physics degree from the 1990’s didn’t prepare me well for needing to write a lot of scripts which seem to get more complicated every month. I have been working on a project which requires a Linux virtual appliance be used to build a bunch of Linux VMs. I did start by looking at the option of running PowerCLI on Linux and quickly came to the conclusion that it was too soon for me to use that technology. So, I fell back to Python and the Python bindings for the VMware vSphere API called pyVmomi. This Python module allows me to interact with vCenter to get tasks done.

What is pyVmomi?

Hopefully, you know that VMware has a public API that you can use to interact with your vSphere environment. Along with the API, there are a variety of tools that integrate the API with particular programming languages. The one that I, and a lot of vSphere administrators, are familiar with is PowerCLI which turns the vSphere API into PowerShell cmdlets and objects. PyVmomi does the same thing, but for Python and (for me at least) not nearly as well. As we assess the usefulness of pyVmomi, remember that it far predates PowerShell and PowerCLI. A lot of the lessons learned on pyVmomi shaped the development of PowerCLI. Unfortunately, those lessons have not been back-ported to pyVmomi which requires that you know a lot about the vSphere API. While there is great documentation for PowerCLI, the pyVmomi documentation is a link to the vSphere API documentation, which in turn is very hard to read. There are a bunch of samples on GitHub, but pyVmomi is definitely not an easy tool to use.

Getting Started

The first thing is that you need Python installed, I have done all my work with Python 2.7, although Python 3 is more recent, it has some quite different syntax in places. Installing pyVmomi is a single line:

pip install --upgrade pyvmomi

I did this install as part of the Ansible playbook that does the rest of the appliance configuration for this Centos VM.

Next, you will want to connect to a vCenter server:

s = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s.verify_mode = ssl.CERT_NONE
  c = SmartConnect(host=vchost, user=vcuser, pwd=vcpassword, sslContext=s)
  print("Failed to connect to vCenter at " + vchost)

This yields a connection object, the next snippet retrieves the contents of that connection, lists out the data centers and selects the data center object that matches the data center name I already have. Finally it retrieves the VM folder object that corresponds to the data center.

content = c.RetrieveContent()
datacenters = [entity for entity in content.rootFolder.childEntity if hasattr(entity, 'vmFolder')]
for dc in datacenters:
  if ( == dcname):
    datacenter = dc
    vmFolder = datacenter.vmFolder

Next, I needed to retrieve every VM in that data center, using a function:

def GetVMs(vmFolder):
vms = vmFolder.childEntity
for vm in vms:
  if hasattr(vm, 'childEntity'):
    GetVMs (vm)
  elif isinstance(vm, vim.VirtualApp):
    for c in vm.vm:
      if c not in myvmlist:
      if vm not in myvmlist:

Notice that the function calls itself for recursive search through multiple folders under the one I give it. The VM objects are listed in the global variable “myvmlist” and I can list their names with a simple loop:

for vm in myvmlist:
  print (

That is enough for getting started, all of that code started as samples on the community sample repository and didn’t need any significant modification to fit my needs. Getting the rest of my script together took a bit more work, but that is a storey for another few blog posts.

So, what else did you manage to do?

I have managed to make do all of the VM manipulation that I wanted work in PyVmomi, although I don’t feel like I know a lot more about how to make it work than when I started. I plan to write blog posts about each of the parts that I got to work:

  • Create new VM
  • Add SCSI controller and hard drives to an existing VM
  • Set resource controls, particularly CPU and RAM reservations

© 2018, Alastair. All rights reserved.

About Alastair

I am a professional geek, working in IT Infrastructure. Mostly I help to communicate and educate around the use of current technology and the direction of future technologies.
This entry was posted in General. Bookmark the permalink.