MrPointy's journey designing, building and deploying private clouds

PyVmomi - Connecting to the VCSA

2015-10-13

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 :
[https://github.com/vmware/pyvmomi]

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 :

1
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

1
2
3
4
5
6
7
8
9
10
import atexit
from pyVim.connect import SmartConnect, Disconnect
SI = SmartConnect(host="192.168.10.11",
user="Administrator@vsphere.local",
pwd="vmware",
port="443")
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

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

vCenterVersion.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python
"""
Using the pyVmomi python bindings for vSphere connect to a VCSA and report the version information
"""
import atexit
import yaml
import inspect
class Vcenter(object):
def __init__(self, vcenter_params):
self.pyVmomi = __import__("pyVmomi")
self.server = vcenter_params['ip']
self.username = vcenter_params['user']
self.password = vcenter_params['pw']
self.connect_to_vcenter()
def connect_to_vcenter(self):
from pyVim import connect
print("Connecting to %s using username %s" % (self.server, self.username))
self.service_instance = connect.SmartConnect(host=self.server,
user=self.username,
pwd=self.password,
port=443)
self.content = self.service_instance.RetrieveContent()
about = self.service_instance.content.about
print("Connected to %s, %s" % (self.server, about.fullName))
atexit.register(connect.Disconnect, self.service_instance)
def main():
config = yaml.load(open("labconfig.yaml"))
vc=Vcenter(config["lab"]["vsphere"]["vcenter"]);
return 0
# Start program
if __name__ == "__main__":
main()
1
2
3
4
5
$ ./vCenterVersion.py
Connecting to 192.168.10.11 using username administrator@vsphere.local
/usr/lib/python2.7/site-packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
Connected to 192.168.10.11, 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