Custom Search

Saturday, May 12, 2012

Howto psphere Python interface for VMware vSphere Web Services SDK

psphere is a Python interface for the VMware vSphere Web Services SDK, a powerful API for programatically managing your VMware infrastructure.

1)
Install  psphere

$ pip install -U psphere


2)
Configure  psphere

$ cd ~
$ mkdir .psphere
$ cd .psphere
$ vim config.yaml
general:
    server: 192.168.0.144
    username: root
    password: xxxxx
    template_dir: ~/.psphere/templates/
logging:
    destination: ~/.psphere/psphere.log
    level: INFO # DEBUG, INFO, etc


3)
Open a Python Terminal and try following examples. 
IP of ESXi-5 Hots : 192.168.0.144

>>> from psphere.client import Client
>>>
>>> clnt = Client(server="192.168.0.144", username="root", password="xxxxx")
Logging to ~/.psphere/psphere.log at INFO level
>>>
>>> from psphere.managedobjects import VirtualMachine
>>>
>>> vms = VirtualMachine.all(clnt)
>>>
>>> for vm in vms:
...     vm.name
...
ubuntu 12.04 real
vCenter Server Appliance5
vShield Manager
vCloud Director
vCD-1
cli-vm
>>>
>>>
>>>
>>> vm = VirtualMachine.get(clnt, name="ubuntu 12.04 real")
>>>
>>> vm.name
ubuntu 12.04 real
>>>
>>> vm = VirtualMachine.get(clnt, name="vCenter Server Appliance5")
>>>
>>> vm.name
vCenter Server Appliance5
>>>
>>> vm.datastore
[psphere.managedobjects.Datastore object at 0xa61f610>]
>>>
>>> vm.datastore[0].name
datastore1
>>>
>>> vm.datastore[0].parent.name
datastore
>>>
>>>
>>> vm.datastore[0].host
[(DatastoreHostMount){
   key = psphere.managedobjects.HostSystem object at 0xa721750>
   mountInfo =
      (HostMountInfo){
         path = "/vmfs/volumes/4fa76f8a-f40f17b6-8aba-0030674106dd"
         accessMode = "readWrite"
         accessible = True
      }
 }]
>>>
>>>
>>> vm.datastore[0].host[0]
(DatastoreHostMount){
   key = psphere.managedobjects.HostSystem object at 0xa721750>
   mountInfo =
      (HostMountInfo){
         path = "/vmfs/volumes/4fa76f8a-f40f17b6-8aba-0030674106dd"
         accessMode = "readWrite"
         accessible = True
      }
 }
>>>
>>>
>>> vm.datastore[0].host[0].key.name
localhost.localdomain
>>>
>>>
>>> host = vm.datastore[0].host[0].key
>>>
>>> for vm in host.vm:
...     vm.name
...
ubuntu 12.04 real
vCenter Server Appliance5
vShield Manager
vCloud Director
vCD-1
cli-vm
>>>
>>>

No comments:

Post a Comment