You are on page 1of 4

Deploying vSphere Virtual Machines using Ansible

by admin

I wrote a few posts recently covering how to install Ansible on a Centos


machine, and create an Ansible playbook. The examples I used were
around using Ansible to configure Linux nodes. With this post I wanted to
have a look at what we can do with Ansible and vSphere.
Before we can start running Ansible playbooks against vCenter or ESXi,
there are a few things we need to do. Ansible provides modules for
interacting with vSphere, which are based on PyVmomi, which is the
Python SDK for the VMware vSphere API.
We can install PyVmomi using pip, but first we’ll need to enable the
EPEL repository so that we can install it. To do so:
$ sudo yum install epel-release

Once done, run the following to install Pip:

$ yum -y install python-pip

Finally, we can install PyVmomi using Pip by running:

$ pip install pyvmomi

With that done we can now use Ansible to interact with vSphere. The module I’ll be using in this post is called
vmware_guest, which allows us to use Ansible to create virtual machines, deploy from template, power virtual
machines on and off and modify, rename and remove virtual machines.

To deploy a virtual machine from template, we could use a playbook similar to the following:

---
# create a new VM from a template

- name: VM from template


hosts: localhost
gather_facts: false
connection: local
vars:
vcenter_hostname: vcenter-hostname
vcenter_user: administrator@vsphere.local
vcenter_pass: password
esxhost: 192.168.1.100
datastore: datastore01
vmtemplate: VM01
name: "newvm"
notes: Ansible Test
dumpfacts: False
tasks:
- name: Create VM from template
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
esxi_hostname: "{{ esxhost }}"
datacenter: dc
name: "{{ name }}"
template: "{{ vmtemplate }}"
disk:
- size_gb: "{{ disk_size | default(30) }}"
type: thin
datastore: "{{ datastore }}"
networks:
- name: VM Network
type: dhcp
hardware:
memory_mb: "{{ vm_memory | default(1024) }}"
wait_for_ip_address: True
state: present
register: newvm

- name: IP address info


debug:
msg: "{{ newvm.instance.ipv4 }} {{ name }}"
The playbook example above is fairly self-explanatory. After setting up the connection details and the variables, there
is a task using the vmware_guest module to provision a virtual machine from a template – note the Template
parameter, which uses the name of the VM template via a variable. There is some customization occuring as well,
with the disk and virtual memory allocation being set to 30GB and 1024MB respectively.

To run the playbook, use the ansible-playbook command:


$ ansible-playbook deployvm.yml

When it runs you should see the familiar Ansible feedback:

[root@centos playbooks]# ansible-playbook vmdeploy.yml

PLAY [VM from template]

**********************************************************************************************
**************************************************

TASK [Create VM from template]

**********************************************************************************************
*******************************************

changed: [localhost]

TASK [IP address info]

**********************************************************************************************
***************************************************

ok: [localhost] => {

"msg": "192.168.1.100 newvm"

PLAY RECAP

**********************************************************************************************
*********************************************************

localhost : ok=2 changed=1 unreachable=0 failed=0


In the vSphere Web Client you should be able to see the tasks as the new VM is created:

Notice that we are outputting the IP address attribute of the new virtual machine with the ‘IP address info’ task. With
the IP known we could use it to connect to the VM with Ansible to perform some OS configuration tasks, ultimately
enabling us to provision and new virtual machine, and configure its OS/applications etc in the same playbook. Next
time, I’ll cover a bit around Ansible Vault, which is used to avoid showing sensitive data such as usernames and
passwords in a playbook.

You might also like