Using Ansible to deploy a new VMware vSphere VM
This guide explains how to install Ansible and create a playbook to automate the deployment and configuration of VMs running on VMware vSphere 6.7.
Introduction
The following steps were tested on a MacBook Pro running VMware vSphere 6.7 on VMware Fusion 11.5. Ansible was installed on the Mac but the instructions should also work on Linux.
Install Ansible and pyVmomi
Python should already be installed on Mac but you might need to install pip
by running this command.
sudo easy_install pip
Once pip
is installed, we can use it to install Ansible with the following command.
sudo pip install ansible
We also need to install pyVmomi
which is the Python SDK for the VMware vSphere API that allows you to manage ESX, ESXi, and vCenter.
sudo pip install pyvmomi
That’s all the dependencies installed, we’re now ready to create our Ansible playbook.
Create Ansible Playbook
Ansible playbooks are YAML configuratiom files that describe what actions to run on a remote host. For this example, we’ll create a simple playbook called deploy-vms.yml
that will use the vmware_guest
module to deply a VM from template.
Create the file.
vim deploy-vms.yml
Add the following contents to the file. You’ll want to change the variables in the vars section to match the details of your vCenter.
---
- hosts: localhost
gather_facts: no
vars:
vcenter_server: "10.1.1.100"
vcenter_user: "administrator@vsphere.local"
vcenter_pass: "Pa$$w0rd"
datacenter_name: "Datacenter"
cluster_name: "Cluster"
tasks:
- name: Clone the template
vmware_guest:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
validate_certs: False
name: web02
template: template-ubuntu-18.04
datacenter: "{{ datacenter_name }}"
folder: /{{ datacenter_name }}/vm
cluster: "{{ cluster_name }}"
datastore: "iscsi-datastore01"
networks:
- name: VM Network
ip: 10.1.1.22
netmask: 255.255.255.0
gateway: 10.1.1.2
type: static
dns_servers: 10.1.1.2
customization:
hostname: "web02"
dns_servers:
- 10.1.1.2
- 1.1.1.1
state: poweredon
wait_for_ip_address: yes
delegate_to: localhost
The playbook above will create a new VM called web02
and place it on a datastore called iscsi-datastore01
. It will be cloned from a template called template-ubuntu-18.04
.
I wrote about creating templates in my last post: Converting a VM to a template with VMware vSphere vCenter 6.7
Here’s a link to the vmware_guest section of the Ansible documentation for more information on what options are available.
Run the playbook
After making the required changes to the deploy-vms.yml
file, save it, then run the following command to deploy the VM.
ansible-playbook deploy-vms.yml
The screenshot below shows the playbook in action.

The playbook might take awhile to complete because we used the wait_for_ip_address: yes
option, which means the Ansible command finishes once the VM has been cloned and the network is configured with the static IP address specified.
The screenshot below shows the VM has been created in vCenter.

We should also be able to ping the new VM.

Final thoughts
This was just a simple introduction into automation with Ansible and using it to deploy VMware virtual machines from existing templates.
The example can be expanded with new playbooks and options to create complex workflows that deploy thousands of VMs and configure them to run whatever applications you need at rapid speed.