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:

config.json

{
    "ANSIBLE_MODULE_ARGS": {
      "src_username": "username",
      "src_password": "pass",
      "dest_username": "username",
      "dest_password": "pass",
      "src_vcenter": "vcenter1.example.com",
      "dest_vcenter": "vcenter2.example.com",
      "cluster_name":  "CLUSTER",
      "datastore_name":  "my_datastore",
      "dest_esx": "esx.example.com",
      "template_name":  "win2k16_template",
      "vm_name":  "ben_vm_test",
      "vm_folder": "/Templates",
      "datacenter_name": "A_DATACENTER"
    }
} 

Python –m vmware_cross_vcenter_clone.py 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.

vcenter_cross_clone:
  src_username: vcenter_admin
  src_password: password1
  dest_username: vcenter_admin
  dest_password: password2
  src_vcenter: vcenter1.example.com
  dest_vcenter: vcenter2.example.com
  cluster_name: vcluster1
  datastore_name: datastore1
  dest_esx: esx1.example.com
  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:

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s