Automation is certainly where it’s at these days in provisioning infrastructure. Ansible is one of the most popular automation tools across many different verticals and is extremely versatile. It works well with VMware vSphere as well to provision, configure, and otherwise automate vSphere infrastructure. This post will take a look at Ansible provisioning VMware with vmware_guest example and see how this can be used to automate virtual machine deployment in your vSphere infrastructure for quick and easily spinning up and provisioning virtual machines.
Ansible vmware_guest module
The module we want to key in on for the purposes of provisioning VMware is the vmware_guest module. What is the Ansible vmware_guest module? It is a replacement for the legacy Ansible vsphere_guest module. What does it do? The Ansible vmware_guest module is used to create new virtual machines from vSphere templates or from cloning other virtual machines that exist in the vSphere inventory. It can be used to modify various aspects of the virtual machine, including:
- network
- disk
- customization specification
- …many others
It supports a long list of parameters for interacting with your vSphere environment such as the following. Some of these are used for connecting to the vSphere environment itself. To read all the details of the following vmware_guest parameters, look at the official parameter documentation found here.
- annotation
- cdrom
- cluster
- convert
- customization
- customization spec
- customvalues
- datacenter
- datastore
- disk
- esxi_host
- folder
- force
- guest_id
- hardware
- hostname
- is_template
- linked_clone
- name
- name_match
- networks
- password
- port
- resource_pool
- snapshot_src
- state
- state_change_timeout
- template
- use_instance_uuid
- username
- uuid
- validate_certs
- vapp_properties
- wait_for_customization
- wait_for_ip_address
Ansible Provisioning VMware with vmware_guest Example
So, let’s take a look at an Ansible Provisioning VMware with vMware_guest example of what we can do with the Ansible vmware_guest module. In the following example and setup of our Ansible environment, we will use the vmware_guest module to clone a Windows Server 2019 template to create three new Windows Server 2019 VMs.
There are two files that will need to be created for Ansible provisioning VMware with vmware_guest. These are:
- Variables YAML file
- Ansible playbook making use of vmware_guest
The variables YAML file will contain variables we will feed into the Ansible playbook that runs the vmware_guest module. Let’s see what we are including in the variables YAML file.
For now with just experimenting, I am hard coding the vCenter Server password in the variables file. However, will get this into Hashicorp Vault to secure the credentials. For production use cases, make sure you are using some type of credential manager for storing secrets and not hard coding them in your variables file.
vcenter_hostname: vcsa.cloud.local
username: administrator@vsphere.local
password: P@$$w0rd1$
notes: Automated VM creation test
folder: testansible
datastore: vsanDatastore
datacenter: CloudLocal
vmtemplate: Win2019clone
customization_spec: CloudLocalServerDHCP
vmcluster: vsancluster
state: poweredon
servers:
- win19clone01
- win19clone02
Now, let’s see what is in the actual Ansible playbook and how the vmware_guest module is called.
name: Clone VMs
hosts: localhost
gather_facts: false
vars_files:
multiple_vms.yml
tasks:
- name: Clone multiple VMs from template
local_action:
module: vmware_guest
hostname: "{{ vcenter_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
validate_certs: no
folder: "{{ folder }}"
template: "{{ vmtemplate }}"
name: "{{ item }}"
cluster: "{{ vmcluster }}"
datacenter: "{{ datacenter }}"
state: "{{ state }}"
customization_spec: "{{ customization_spec }}"
with_items: "{{ servers }}"
With the official documentation for the template parameter, this is a template or existing virtual machine used to create your new virtual machines. When this parameter is not set, virtual machines are created without using a template. It would be assumed that you are supplying all of the configurations for the virtual machine in the playbook if this is the case (disk, network, etc.) If the virtual machine already exists, this parameter will be ignored. This parameter is case sensitive. From Ansible version 2.8 onwards, you can also specify an absolute path to the virtual machine.
To run the ansible playbook after making sure you have your two files in place. Here I am running the following:
ansible-playbook vmware_guest.yml -v
As you can see it is cloning the first virtual machine.
First Windows Server 2019 server is cloned, booted, and customized.
The second Windows Server 2019 server is cloned, and in the process of rebooting after guest customization.
Just to confirm, I am able to login as a domain admin for the domain and the computer name is verified. This means the Windows Server 2019 guest operating system has been customized.
Below is an example of creating a new virtual machine from scratch and customizing.
name: Create a new VM and customize community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: testansible
name: win19test01
state: poweredon guest_id: windows9Server64Guest
esxi_hostname: "{{ esxi_hostname }}"
disk:
- size_gb: 60
type: thin
datastore: DS01
hardware:
memory_mb: 4096
num_cpus: 4
scsi: paravirtual
networks:
- name: VM Network
mac: aa:bb:dd:aa:00:14
ip: 10.1.149.50
netmask: 255.255.255.0
device_type: vmxnet3
wait_for_ip_address: true
wait_for_ip_address_timeout: 600
delegate_to: localhost
register: deploy_vm
Synchronous Asynchronous and Looping in Ansible
In the first example showing how to use vmware_guest to clone a template and create two new virtual machines (win19clone01, win19clone02), Ansible will create the virtual machines synchronously, in a serial fashion. In other words, you will see the first VM cloned over, powered up and customized, and then once it finishes out, you will see the other VM cloned, powered on, and then customized.
This approach will take longer to clone your VMs over. However, it will show the least amount of impact on performance for an environment since there is only one operation taking place at a time.
You can add the following instead of the with_items “{{ servers }}”:
loop: "{{ servers }}"
async: 60
poll: 10
When you use the loop, it will loop through the machines and use the async timing to kick off the next operation. This will allow operations to be kicked off and run in parallel to each other.
Wrapping Up
Ansible works great with VMware vSphere. Hopefully, as this Ansible provisioning VMware with vmware_guest example shows, with just a couple of simple YAML files, you can clone multiple VMs from template, create from scratch, and many other operations such as guest customization, etc.
0 Comments