Getting Started with pSphere on OSX

by bunchc on July 9, 2012

Always on the search for new skills and new ways to work with vSphere & Virtualization, I’ve been turned on to pSphere (Python bindings for vSphere). However, getting this up and going on my Mac was not as straight forward as I’d have thought.

Note: I’m not sure what is strictly required here, these are the steps that worked for me.

Prerequisites

You’ll need to install the following:

Installing PIP:

sudo easy_install pip

Installing virtualenv

sudo pip install virtualenv

Installing libyaml

brew install libyaml

Installing pSphere

Now that you have the prerequisites going, lets install pSphere:

Create the Virtual Environment:

$ virtualenv psphere_test
New python executable in psphere_test/bin/python
Installing setuptools…………done.
Installing pip……………done.

Change into the Virtual Environment

$ pwd
/Users/provmware/psphere_test/bin
M102159AGY:binprovmware$ ls
activate        easy_install        python
activate.csh        easy_install-2.7    python2.7
activate.fish        pip
activate_this.py    pip-2.7
M102159AGY:bin provmware$ source activate
(psphere_test)M102159AGY:bin provmware$

Installing pSphere

$ pip install psphere

Connecting to vCenter

python
>>> from psphere.client import Client
>>> client = Client(vCenter_IP, vCenter_User, vCenter_Pass)

Listing VMs

First you need to import the object class for Virtual Machines:

>> from psphere.managedobjects import VirtualMachine

Now, you can collect a list of all VMs and check out the contents:

>>> vms = VirtualMachine.all(client)
>>> vms
[<psphere.managedobjects.VirtualMachine object at 0x111419f50>, <psphere.managedobjects.VirtualMachine object at 0x111419f10>, <psphere.managedobjects.VirtualMachine object at 0x111419fd0>, <psphere.managedobjects.VirtualMachine object at 0x1114353d0>, <psphere.managedobjects.VirtualMachine object at 0x1114352d0>, <psphere.managedobjects.VirtualMachine object at 0x111435290>, <psphere.managedobjects.VirtualMachine object at 0x111435350>, <psphere.managedobjects.VirtualMachine object at 0x111435310>, <psphere.managedobjects.VirtualMachine object at 0x111435390>, <psphere.managedobjects.VirtualMachine object at 0x111435410>, <psphere.managedobjects.VirtualMachine object at 0x111435450>, <psphere.managedobjects.VirtualMachine object at 0x111435490>, <psphere.managedobjects.VirtualMachine object at 0x1114354d0>, <psphere.managedobjects.VirtualMachine object at 0x111435550>, <psphere.managedobjects.VirtualMachine object at 0x111435510>, <psphere.managedobjects.VirtualMachine object at 0x1114355d0>, <psphere.managedobjects.VirtualMachine object at 0x111435610>, <psphere.managedobjects.VirtualMachine object at 0x111435650>]

Kinda boring, but it is indeed a list of all VMs. Now, let’s find out a bit more about them:

This first one provides the name of the VM.

>>> vms[0].name
twinstrata.provmware.local

Now things get a bit more… verbose:
>>> vms[0].summary
(VirtualMachineSummary){
   vm = <psphere.managedobjects.VirtualMachine object at 0x111419c50>
   runtime =
      (VirtualMachineRuntimeInfo){
         device[] =
            (VirtualMachineDeviceRuntimeInfo){
               runtimeState =
                  (VirtualMachineDeviceRuntimeInfoVirtualEthernetCardRuntimeState){
                     vmDirectPathGen2Active = False
                     vmDirectPathGen2InactiveReasonOther[] =
                        "vmNptIncompatibleNetwork",
                  }
               key = 4000
            },
            (VirtualMachineDeviceRuntimeInfo){
               runtimeState =

Summary

Like PowerCLI, vCO, and some of the other VMware provided SDKs, pSphere provides a 3rd party way of accessing the vSphere API via Python.

  • http://twitter.com/danjellz Dan Jellz

    Works great in Fedora 17 as well. Only difference was to YUM install libyaml.  Thanks for the great tutorial!

  • Patrick A

    So where’s the script and output to json ? You should start a tool on github, and release some codez

  • http://professionalvmware.com professionalvmware

    Interesting idea, and would you have some ideas? I had a few, then well, got writers block.

Previous post:

Next post: