If you have read any of my posts, you will quickly discover that I use Ansible a lot, for deploying virtual machines and VMware OVA appliances, on vSphere.
Ansible support for VMware is constantly growing and in the latest versions, it has become an essential tool that I use as part of my development process for standing up required infrastructure that is easy and quick to deploy or tear down. The key part of using Ansible for my deployments is that the process is repeatable and consistent.
In this post, I am going to cover some of the core Ansible modules that I use to perform these deployments and provide various use case examples. Once you understand these modules and lay down the groundwork, you’ll be deploying virtual machines or appliances in mere minutes, with the simple editing of some configuration files.
If you are not too familiar with what Ansible is, or what it’s used for, then I recommend that you check out the official documentation. You can also get a brief overview of what Ansible is at cloudacademy.com, and there is a wealth of online training and other material available to get you up to speed.
All examples used in this post, including a fully working Ansible solution, can be found on my Ansible-VMware Github.
You will need to have the following packages installed (through PIP) on your Ansible control machine:
I have also provided a requirements.txt file that you can install using PIP
pip install -r requirements.txt
Deploying Virtual Machines
The most basic task that you are ever likely to perform on any vSphere environment, is the deployment of a virtual machine. In this section, I am going to show you how Ansible can make the task of spinning up dozens of virtual machines a breeze.
Ansible provides the core module vmware_guest that can be used to create a new virtual machine or clone from an existing virtual machine template.
In these examples, I am going to demonstrate how you can create new virtual machines or clone an existing virtual machine from a template for both Windows and Linux, perform customization and configure hardware and advanced settings.
Create a New Virtual Machine (no template)
This is an example play that will create a virtual machine with no OS installed (not from a template). When the virtual machine is powered on, it will automatically try and PXE boot from the network, which can be useful in deployment pipelines where VMs are bootstrapped in this way.
I have a simple play called ‘vmware_create_virtual_machine.yml‘, which includes the tasks to create a virtual machine in VMware vSphere.
--- - hosts: local become: no gather_facts: False vars: tasks: - name: Create a New Virtual Machine vmware_guest: hostname: <vcenter hostname> username: <vcenter username> password: <vcenter password> validate_certs: no name: Linux_VM datacenter: SG1 cluster: SG1-CLS-MGMT-01 folder: /SG1/vm guest_id: centos7_64Guest disk: - size_gb: 20 type: thin datastore: vsanDatastore hardware: memory_mb: 2048 num_cpus: 1 scsi: paravirtual networks: - name: Management device_type: vmxnet3 state: poweredon delegate_to: localhost
Many of the properties should be self-explanatory, but we’re creating a virtual machine called Linux_VM, with 1 CPU, 2GB of Memory, a 20GB think hard disk, etc.
Because we are creating a new virtual machine, the guest_id needs to be provided, which sets the Guest Operating system profile on the VM. You can get the full list of supported guest_ids from the VMware developer support page.
To run the playbook, I invoke the ansible-playbook command.
You can see that the execution was successful (ok=1) and that a change was made (changed=1), which means the virtual machine was created. If we take a look at vCenter we can see the virtual machine now exists, with the specified configuration:
Update Virtual Machine
The great thing about Ansible is that if you were to run this play again, it would not try and create another VM. Instead, it will simply exit with a status of OK, if it discovers that the specified virtual machine already exists and is powered on.
But what if we made some changes to the configuration that we want to apply to the virtual machine? Well, Ansible will only make these changes to the virtual machine if the ‘state‘ parameter has been set to ‘present‘ in the play. Also, if you are making configuration changes to hardware, then the virtual machine may also need to be powered off first (Ansible will display an error if this is required).
So let’s assume that the virtual machine is powered off and we want to enable CPU and memory hot add support. We simply add these configurations under the hardware section:
Now if we run the play again:
And we can see that a reconfigure task is performed on the VM in vCenter server.
Make sure to check the documentation for all the parameters that can be configured. Read more “Automate vSphere Virtual Machine and OVA Appliance Deployments Using Ansible”