1 year ago

#299537

test-img

AKSHAY KADAM

how to copy a vmware template to a given vcenter using pyVmomi module

I am familiar with cloning a VM from template and copying the template within the same vcenter, but when i try to copy it to a specified vcenter i am unable to achieve this.. I am not sure whether this is possible or not using pyVmomi module I am new to pyVmomi module, Below is my code, any help or suggestions would be greatly appreciated.

from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
import ssl,time,argparse,sys
 
def get_obj(content, vimtype, name):
    obj = None
    container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
    for c in container.view:
        if c.name == name:
            obj = c
            break
    return obj
 
def main():
   parser = argparse.ArgumentParser()
   parser.add_argument('-s', '--source',dest='source', help="source vcenter of the template")
   parser.add_argument('-t', '--target',dest='target',default=False, help="target vcenter to which the template should be copied")
   parser.add_argument('-tmp', '--template',dest='template',default=False, help="the template passed by the user")
   args = parser.parse_args()
   source = args.source
   target = args.target
   template = args.template
   if source == None or target == None or template == None:
      print("please provide the source/target/template, Exiting..")
      sys.exit(1)
 
   # Connecting to source vcenter
   username='username'
   password='password'
   si = None
   context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
   context.verify_mode = ssl.CERT_NONE
   si = SmartConnect(host=source,user=username,pwd=password,sslContext=context)
   content = si.content
 
   #get template object
   template_vm = get_obj(content, [vim.VirtualMachine], template)
   print(template_vm)
 
   # Connecting to target vcenter
   # Disconnect(si)
   si2 = SmartConnect(host=target,user=username,pwd=password,sslContext=context)
   content2 = si2.content
   
   datacenter = get_obj(content2, [vim.Datacenter], 'mydatacenter')
   destfolder = datacenter.vmFolder
   datastore = get_obj(content2, [vim.Datastore], 'mydatastore')
    
   # Relocation spec
   relospec = vim.vm.RelocateSpec()
   relospec.datastore = datastore 
 
   clonespec = vim.vm.CloneSpec()
   clonespec.location = relospec
   clonespec.template = True
 
   task = template_vm.Clone(folder=destfolder, name='template_new', spec=clonespec)
  
if __name__== "__main__":
   main()
 
 

When i switch connection to a target vcenter i get the below error

Traceback (most recent call last):
  File "clone_template2.py", line 74, in <module>
    main()
  File "clone_template2.py", line 70, in main
    task = template_vm.Clone(folder=destfolder, name='template_new', spec=clonespec)
  File "/home/akshay/.local/lib/python3.8/site-packages/pyVmomi/VmomiSupport.py", line 706, in <lambda>
    self.f(*(self.args + (obj,) + args), **kwargs)
  File "/home/akshay/.local/lib/python3.8/site-packages/pyVmomi/VmomiSupport.py", line 512, in _InvokeMethod
    return self._stub.InvokeMethod(self, info, args)
  File "/home/akshay/.local/lib/python3.8/site-packages/pyVmomi/SoapAdapter.py", line 1397, in InvokeMethod
    raise obj # pylint: disable-msg=E0702
pyVmomi.VmomiSupport.ManagedObjectNotFound: (vmodl.fault.ManagedObjectNotFound) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
   msg = "The object 'vim.Datastore:datastore-360' has already been deleted or has not been completely created",
   faultCause = <unset>,
   faultMessage = (vmodl.LocalizableMessage) [],
   obj = 'vim.Datastore:datastore-360'
}

python-3.x

templates

vmware

vsphere

vcenter

0 Answers

Your Answer

Accepted video resources