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:
- Homebrew – http://mxcl.github.com/homebrew/
- PIP
- virtualenv
- libyaml
- xcode Command Line Tools
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.
Works great in Fedora 17 as well. Only difference was to YUM install libyaml. Â Thanks for the great tutorial!
So where’s the script and output to json ? You should start a tool on github, and release some codez
Interesting idea, and would you have some ideas? I had a few, then well, got writers block.
I get this far: (psphere_test)jfreeman$pip install psphere
Collecting psphere
Collecting PyYAML (from psphere)
Collecting suds (from psphere)
Installing collected packages: PyYAML, suds, psphere
Successfully installed PyYAML-3.11 psphere-0.5.2 suds-0.4
(psphere_test)jfreeman$
however when i go into python and type: “from psphere.client import Client” I receive this error: No handlers could be found for logger “psphere.config” thoughts?
i should add that I did follow on with “client = Client(‘160.100.0.10’, ‘root’, ‘client_password’)” … (I had to put them in single quotes or python interpreter threw an error)… the most interesting thing I saw was at the end of the results:
urllib2.URLError:
we have an out of date self signed cert (I’m going to remedy this shortly).. how can I tell psphere to accept whatever cert it encouters?