home tools books contact

Using Ansible to Clone a VMware Fusion Pro Virtual Machine

Ansible DevOps Fusion

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.

Introduction

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.

Once the VM is ready, power it off and don’t use it for anything else, other than a base image for clones. Read How to Install Linux Ubuntu Server 18.04 on a Mac with VMware Fusion.

Step 2: Create Playbook

Create a new playbook called vmware.yml and add the following:

- hosts: localhost
  vars:
    vms_path: "/Users/tony/Virtual\ Machines.localized"
    src_vmx:  "{{ vms_path }}/ubuntu-18.04.vmwarevm/ubuntu-18.04.vmx"
    dest_vmx: "{{ vms_path }}/ansbile.vmwarevm/ansible.vmx"
  tasks:

  - name: check if ansible vm exists
    stat:
      follow: yes
      path: "{{ dest_vmx }}"
    register: stat_result

  - name: "clone virtual machine"
    command: vmrun -T fusion clone "{{ src_vmx }}" "{{ dest_vmx }}" linked -cloneName=ansbile
    when: stat_result.stat.exists == false

  - name: "start virtual machine"
    command: vmrun -T fusion start "{{ dest_vmx }}" nogui

  - name: "get ip address"
    command: vmrun -T fusion getGuestIPAddress "{{ dest_vmx }}" -wait
    register: ip_address

  - name: "print ip address"
    debug:
      msg: "{{ ip_address.stdout }}"
  • 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:

ansible-playbook vmware.yml

After running the playbook, the IP address of the VM should be printed in the last task as shown in the image below.

Clone VMware Fusion VM with Ansible

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.

Cloned VMware Fusion VM Called Ansible

Conclusion

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.

Written by: Tony Mackay

* GraspingTech participates in various affiliate marketing programs, which means we may earn a small commission at no additional cost to you, should you click on a link to a product and make a purchase.