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!
Presented for my 8th Year at the UK VMUG Conference 12 weeks ago on all things automation. Always a big thanks to the VMUG team for allowing me to present. I brought my usual humour with me and even showed a prototype app I’ve been working on called Project Hermes for putting a UI to automation tools that use JSON or YAML files to send instructions. Anyway, my sponsors listed below coughed up a ton of prizes and in the end only 21 people entered my competition and most people won 2 prizes and some 3 as I had that many. However, if you missed out Vembu are giving you a 2nd chance by filling in a survey found by clicking >HERE<
Anyway big shout out to the sponsors who donated prizes to my sessions:
Team 18 years ago while I was still a Novell consultant and at work, I was very frustrated with the number of systems I had to log in too. We had NT domains + Novell Edirectory + numerous other dos based systems that had separate logins. I wanted to automate the login process because we had no SSO and I stumbled across AutoIT. Team AutoIT is still relevant today as it was back then. If you have software on a Windows system you can automate it regardless of APIs or not. You can create a small or large bot to automate stuff and its FREE. Here is a recent video where this chap demonstrates its power by automating gameplay. This could easily be an operations tool instead of a game >
I think its time to relearn this tool given my current interests at work…