Using Ansible to Clone a VMware Fusion Pro Virtual Machine
Disclosure. This page contains links to products that may earn us a small commission at no extra cost to you, should you click on them and make a purchase.
Read full disclosure.
This post will show you how to write an Ansible playbook to clone an existing Ubuntu 18.04 virtual machine, power it on without showing the UI and output its IP address.
VMware Fusion Pro* has the ability to clone existing virtual machines allowing you to deploy new instances without having to install the operating system each time.
There are two types of clone which are:
A full clone. Full clones take a complete copy of an existing VM. They use more disk space and take longer to create.
A linked clone. Linked clones only store the changes created in the new VM. They use less disk space and are quicker to create.
In this post we will look at using Ansible to perform a linked clone.
Why Clone VMs with Ansible?
Why would you want to clone a VM with Ansible? One reason might be if you are creating a playbook and you want a fresh VM each time the playbook is run. You could add a few tasks to clone a VM from a gold image, run the tasks, and then when delete the clone once you are finished.
Step 1: Create Base VM
The first thing you will want to do is create a base VM that will be used to create clones from. This VM should have an operating system installed with the latest patches and VMware tools.
Replace the vms_path variable with the path to your Virtual Machines folder.
Replace the src_vmx variable with the path to your base machines VMX file.
The dest_vmx variable is set to create a new VM called ansible.
Step 3: Run the Playbook
Run the playbook with the following command:
After running the playbook, the IP address of the VM should be printed in the last task as shown in the image below.
Since the IP address was printed out, we know the VM is running headless. If you open VMware Fusion*, you should see the cloned VM.
In this tutorial we learned how to create a linked clone of a VMware Fusion Pro* virtual machine using Ansible. This is made possible because the pro version supports clones and has a command line tool (vmrun) that lets you run tasks without the UI.
There’s a lot more you can do with the vmrun command like creating snapshots, modifying networks and virtual hardware etc. Run vmrun --help for a list of available commands and see how you can work them into your Ansible playbooks and workflows.