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.
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.
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.
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.
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.