Python Tips Corner
So I’m making a big push to learn Python. In IT don’t think you can survive without it. For those who don’t know me, I’ve never been a professional programmer. I learned enough C#.net 13 years ago to be dangerous. I did it so I could learn how to automate VMware by building my own tools. Thirteen years later and a plethora of tools later including vDisk Informer, vSphere Plugin Wizard and vRealize Automation icon changer I’m looking at starting again. This time with python. The first job as last time, the VMware programmer’s equivalent of “Hello World” which is to list the VMs on your vSphere environment. To make my life easier, I decided upon the Python SDK wrapper pyvmomi . So here’s how to do it in my favourite python IDE PyCharm.
Step pip install pyvmomi using the below command: pip install –upgrade pyvmomi
Now fire up Pycharm and create a new project.
Now the next step caught me out as a novice. It’s not enough to import the pyvmomi library in your code, you also have to add it through pycharm in your project.
Click on File > Settings >
Now expand your Project and click on “Project Interrupter”
Click on the + symbol on the right
Now in the search box search for pyvmomi and select it.
Now click on Install Package
Once it’s finished installing it should look like this:
Now underneath your project, create a new python file and let’s get coding.
Cut and paste the following code into your newly created python file. Make sure you change the hostname, port, username and password to reflect your environment. I have added comments to explain how to code works inline with the code.
# first import the sdk wrapper library from pyVim.connect import SmartConnect, Disconnect from pyVmomi import vim # import the ssl library to handle http import ssl # now setup your connection properties host = "vcsa6.demo.local" port = 443 user = "firstname.lastname@example.org" password = "PassW0rd123" # now connect to your vCenter/vSphere context = None if hasattr(ssl, '_create_unverified_context'): context = ssl._create_unverified_context() si = SmartConnect(host=host, user=user, pwd=password, port=port, sslContext=context) if not si: print("Could not connect to the specified host using specified " "username and password") # no grab the data model of the SDK tree content = si.RetrieveContent() # now loop through any object that is tagged as "vmFolder" for child in content.rootFolder.childEntity: if hasattr(child, 'vmFolder'): datacenter = child vmFolder = datacenter.vmFolder vmList = vmFolder.childEntity # now grab the child nodes of any vmFolder object for vm in vmList: if hasattr(vm, 'childEntity'): vmList2 = vm.childEntity # now loop through the child ojbects printing their user friendly name for c in vmList2: vmproperties = c.summary print(vmproperties.config.name)
It can be difficult to visualize what is going on since you don’t have a picture of the data model. I learned this many years ago and to give you help, you should watch my video on automating VMware using the REST API. Pyvmomi is not using the REST API, but my video also gives some clue to how the data model is laid out. You find it useful. Click HERE to watch. Remember MoB is your friend!