Using psphere and Python to automate your vSphere tasks

For almost the last year I have been working heavily with vSphere automation. Recently I have found that there is a way to do a lot of the tasks with Python, taking advantage of a fairly newish python library called psphere. I have created a github project where I am keeping tools I make outside of work. These tools do common tasks that administrators and engineers who manage small and large scale environments might find useful.. Its a new project and I should be adding things to it as often as I can. There you will find tools written in Java and Groovy/Grails using vijava. In time Perl scripts using the VMWare provided Perl SDK will show up, and there are already some Python scripts in there using the psphere library. I hope that you will fork the project and add your own stuff to it and share it back with everyone. The github project I started can be found here:

In my first example I am going to show you how to use a few lines of simple python code along with psphere to get the total number of hosts in a vCenter. This is a handy report that you can send out to your management, or even put into some graphing program so you can watch for trends in your environment. If you run vCenter in linked mode you only need to hit the master vCenter to get ALL the hosts for that vCenter cluster.

Lets being with setting up our system. Im using an Ubuntu 11.10 server setup. I did a minimal install and selected OpenSSH Server for my server type. Once the system finished installing I logged in and and got the system up to date. I am of course running this Ubuntu server as a Virtual Machine, so the next thing I want to do after I install the base system and get it updated is to install the open-vm-tools package. Once that is done I reboot. Now I move on to setting up my python environment. Psphere requires Python Yaml, and Python Suds, and Python pip, once these packages are installed you can move on to the pip install -U psphere And now your environment should be all ready to go for coding. Not to much to it. This should also work on Windows. For a reference Ubuntu 11.10 comes with Python 2.7.2, Suds 0.4.1, and Yaml 3.10, and at the time of this I am using psphere 0.5.1 As a note I first tried this using Debian 6.04 with Python 2.6.6 and it did not work, so for best results I can say I got it working with the above Ubuntu configuration..

Now lets cover the code it takes to connect to vCenter, and print all the hosts and at the end give the total number of HostSystem objects stored in this vCenter. Please note this code was tested on both 4.1 and 5.0 vCenter systems.

Open your favorite text editor. and enter the following:

#!/usr/bin/env python

from psphere.client import Client
from psphere.managedobjects import HostSystem
from getpass import getpass

usr = raw_input("vCenter user name: ")
passwd = getpass(prompt="vCenter password: ")
server = raw_input("vCenter server hostname or ip: ")

client = Client(server,usr,passwd)
hs = HostSystem.all(client)
for host in hs:
print "There were: " + str(len(hs)) + " HostSystems found in vCenter"

(Please note in the above code the indent is wrong so dont copy and paste, for some reason my wordpress theme is messing with the indents)
The code is pretty simple and wouldnt take much to modify to output into a csv with some more properties. You could also make it so you did not need to be prompted for user names and passwords or vcenters and just pass them in as an arg, or use the config file ~/.psphere/config.yaml

Leave a Reply

Your email address will not be published. Required fields are marked *