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.
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]
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
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.
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.
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.
1cat labconfig.yaml 2lab: 3 vsphere: 4 vcenter: 5 ip: 192.168.10.11 6 user: email@example.com 7 pw: vmware
1#!/usr/bin/python 2""" 3Using the pyVmomi python bindings for vSphere connect to a VCSA and report the version information 4""" 5 6import atexit 7import yaml 8import inspect 9 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() 17 18 def connect_to_vcenter(self): 19 from pyVim import connect 20 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) 30 31 32def main(): 33 34 config = yaml.load(open("labconfig.yaml")) 35 36 vc=Vcenter(config["lab"]["vsphere"]["vcenter"]); 37 38 return 0 39 40# Start program 41if __name__ == "__main__": 42 main()
$ ./vCenterVersion.py Connecting to 192.168.10.11 using username firstname.lastname@example.org /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 :