Home DevOps Deploy VMware vSphere Virtual Machines using Ansible

Deploy VMware vSphere Virtual Machines using Ansible

by admin

This article will look at how to deploy vSphere virtual machines using Ansible. 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, as it is a great choice if you’re looking for a way to build some automation into your VMware vSphere environments.

Before we can start running Ansible playbooks against vCenter or ESXi, there are a few things we need to do in order to get set up. There is an Ansible VMware module for interacting with vSphere, which are based on PyVmomi, which is the Python SDK for the VMware vSphere API. To start with, we need to install PyVmomi.

Installing PyVmomi

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.

Deploy VMware vSphere Virtual Machines with Ansible Example

To deploy a VMware vSphere virtual machine from template, we could use a Ansible 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 VMware guest virtual machine from a template – note the Template parameter, which uses the name of the VM template via a variable. There is some customization occurring 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:

deploy vmware vsphere virtual machine with ansible

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.

Final Thoughts

Hopefully this has given you some tips on how to deploy vSphere virtual machines with Ansible. In this post we’ve covered how to use the VMware vSphere Ansible module using PyVmomi , then deployed a vSphere Guest Virtual Machine using an Ansible Playbook.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More