lost and found ( for me ? )

VMware ESX : clone VMs from a template VM with pysphere 0.1.7

Here’s a sample script to clone VMs from a template VM with Python pyshere.

about pysphere
https://code.google.com/p/pysphere/

# uname –ri
3.2.0-38-generic x86_64

# tail -1 /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"

# python --version
Python 2.7.3

pysphere 0.1.7

script
# cat -n clone_VMs_from_template_VM.py
    1  #!/usr/bin/env python
    2
    3  import sys
    4  import optparse
    5  from pysphere import VIServer
    6
    7  parser = optparse.OptionParser()
    8  parser.add_option('--prefix',type='string')
    9  parser.add_option('--number',type='int')
   10  parser.add_option('--template',type='string')
   11  parser.add_option('--vcenter',type='string')
   12  parser.add_option('--user',type='string')
   13  parser.add_option('--password',type='string')
   14  options, args = parser.parse_args()
   15
   16  opt_prefix = options.prefix
   17  opt_number = options.number
   18  opt_template = options.template
   19  opt_vcenter = options.vcenter
   20  opt_user = options.user
   21  opt_password = options.password
   22
   23  argvs = sys.argv
   24  argc = len(argvs)
   25
   26  if ( argc != 7):
   27          print "Usage : python clone_VMs_from_template_VM.py --vcenter=<vCenter IP> --user=<user> --password=<password>--prefix=<string> --number=<int> --template=<template VM name>"
   28          sys.exit(1)
   29
   30  # connect to vCenter
   31  server = VIServer()
   32  server.connect(opt_vcenter,opt_user,opt_password,trace_file="debug.txt")
   33
   34
   35  resource_pools = server.get_resource_pools()
   36  #print resource_pools
   37
   38  first_resource_pool = resource_pools.keys()[0]
   39  #print first_resource_pool
   40
   41  # specify the full path of a template VM
   42  template_vm = server.get_vm_by_name("%s" % opt_template)
   43
   44  i = 1
   45  while i <= opt_number:
   46          new_vm = "%s-%s" % ( opt_prefix,i )
   47          clonedVM = template_vm.clone(new_vm,resourcepool=first_resource_pool)
   48          print "VM name : %s : status %s" % (clonedVM.get_property("name") , clonedVM.get_status())
   49          i += 1
   50
   51  server.disconnect()
   53          i += 1
   54
   55  server.disconnect()

usage
# ./clone_VMs_from_template_VM.py
Usage : python clone_VMs_from_template_VM.py --vcenter=<vCenter IP> --user=<user> --password=<password> --prefix=<string> --number=<int> --template=<template VM name>

assume you have a vCenter Server and a template VM

--vcenter=<vCenter’s IP>
--template=<template VM name>
--prefix=<prefix>
--number=<number of VMs to clone>
--user=<user>
--password=<credentials>

template VM name : hat-template
clone three VMs from the template VM “hat-template”
# ./clone_VMs_from_template_VM.py --prefix=new --number=3 --template=hat-template --user='foo' --password='bar' --vcenter='192.168.0.100'
VM name : new-1 : status POWERED ON
VM name : new-2 : status POWERED ON
VM name : new-3 : status POWERED ON
#

debug info will be stored in debug.txt file.
# head -5 debug.txt
_________________________________ Thu Feb 28 22:39:54 2013 REQUEST:
<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ZSI="http://www.zolera.com/schemas/ZSI/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header></SOAP-ENV:Header><SOAP-ENV:Body xmlns:ns1="urn:vim25"><ns1:RetrieveServiceContent xsi:type="ns1:RetrieveServiceContentRequestType"><ns1:_this type="ServiceInstance">ServiceInstance</ns1:_this></ns1:RetrieveServiceContent></SOAP-ENV:Body></SOAP-ENV:Envelope>
_________________________________ Thu Feb 28 22:39:54 2013 RESPONSE:
200
OK

vsphere client



1 comment:

  1. Hi,

    Could you brief on it how to list the templates with pysphere .
    It simply showing the list of live vms and not inventorying templates.

    ReplyDelete

Note: Only a member of this blog may post a comment.