Ansible / Python Cross vcenter clone

During the process of automating VMware template creation, I came across a requirement to clone said templates to different vcenters. After checking out the ansible vmware modules, the vmware REST API documentation, I found that there was no out of box solution to clone to different vcenters.

After doing a bit of research, I was able to write a python module that became an Ansible module to complete the task. I referenced, used, hacked and scraped together my modules after reading the following articles:

William Lam @ virtuallyGhetto

Gitlab issue

VMware code samples

Vincen Shens blog

The module is essentially doing a clone operation, except there are 2 connections made, 1 for each vcenter and a relocation spec needs to be built out and added to the clone operation. My module is available here.

Currently it is limited to cloning a vmware template to a VM in a different vcenter.

The module can be used with ansible, or run directly using python and a config.json file like so:

Create a config.json file that looks something like:


      "src_username": "username",
      "src_password": "pass",
      "dest_username": "username",
      "dest_password": "pass",
      "src_vcenter": "",
      "dest_vcenter": "",
      "cluster_name":  "CLUSTER",
      "datastore_name":  "my_datastore",
      "dest_esx": "",
      "template_name":  "win2k16_template",
      "vm_name":  "ben_vm_test",
      "vm_folder": "/Templates",
      "datacenter_name": "A_DATACENTER"

Python –m config.json

Usage in Ansible, either copy the python script into your global Ansible library path, or place it within a library directory inside your playbook folder. Use the following style syntax to kick off a task.

  src_username: vcenter_admin
  src_password: password1
  dest_username: vcenter_admin
  dest_password: password2
  cluster_name: vcluster1
  datastore_name: datastore1
  template_name: win2k16
  vm_name: ben_testvm
  vm_folder: "/Templates"
  datacenter_name: DATACENTER1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s