OpenSource For You

Using Ansible for Managing VMware Infrastruc­ture

This article is an introducti­on to different Ansible modules, which can be used to manage various VMware objects in VMware infrastruc­ture.

- By: Abhijeet Kasurde The author works at Red Hat and is a FOSS evangelist. He loves to explore new technologi­es and software. You can contact him at abhijeetka­

Ansible is an IT automation tool. It can configure systems, deploy software and orchestrat­e advanced IT tasks such as continuous deployment­s or zero downtime rolling updates. Many of you already know that you can manage your VMware infrastruc­ture using Ansible.

A recent developmen­t branch of Ansible, i.e., 2.4-devel (, has brought in a lot of improvemen­ts in both its performanc­e and its reliabilit­y.

Managing the ESXi host with different tools

The vSphere Client and the vSphere Web Client provide simple ways to manage your ESXi host and operate its virtual machines.

Another way of managing this is the

Virtualisa­tion Management Object Management Infrastruc­ture’s (VMOMI) network exposed API. VMOMI can be used by various programmin­g language bindings like PyVmomi (

Getting started with Ansible

You can read more about Ansible official documentat­ion at Also, Open Source For You has covered various Ansible related topics in past editions.

Let us discuss a few Ansible VMware modules that are essential for managing and administra­ting various VMware objects.

Managing virtual machines

vmware_guest: This module is required in order to manage a virtual machine in your vCenter or standalone ESXi environmen­t. The module can create, modify, rename and remove a virtual machine. It can also power on, power off, restart and suspend a virtual machine.

The following Ansible Playbook example shows how to create a virtual machine:

- name: Example Playbook for creating virtual machine hosts: localhost tasks:

- name: Create a virtual machine with name testvm01


datacenter: datacenter­01 esxi_hostname: esxi_host_01


name: testvm01

guest_id: centos64gu­est username: user

password: pass@123

state: present disk:

- size_gb: 1

type: thin

datastore: LocalDS_0

hardware: memory_mb: 512 num_cpus: 1 osid: centos64gu­est scsi: paravirtua­l

You can read more about this module at http://docs.

vmware_guest_facts: You often want to get details about a virtual machine like its IP address, folder path, MAC address, etc. This module does exactly that. vmware_guest_ facts gives you all the vital informatio­n about a virtual machine. You can chain the informatio­n received from this module to various other VMware modules.

--name: Example playbook to get facts about virtual machine hosts: localhost tasks:

- name: get list of facts about virtual machines


validate_certs: False

hostname: esxi_hostname_01

username: user

password: pass@123

datacenter: datacenter_01

name: testvm01 folder: /vm register: guest_facts_0001

- debug: msg=”{{ guest_facts_0001 }}”

You can read more about this module at http://docs.

vmware_guest_find: This module provides informatio­n about the folder path in which the virtual machine is located. This informatio­n is required in various other VMware modules.

The following example will find the folder path related to a virtual machine.

--name: Example playbook to find the details of folder related to virtual machine hosts: localhost tasks:

- name: Get folder path for virtual machine vmware_guest_find: validate_certs: false hostname: username: user password: pass name: testvm01 datacenter: datacenter_01 register: folder_details

- debug: msg=”{{ folder_details }}”

You can read more about this module at https://github. com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/ vmware/

vmware_guest_snapshot: When we want to preserve the state of the virtual machine at a given point of time, we take a snapshot of the virtual machine. This module provides the functional­ity to manage various operations related to a snapshot such as create, delete, revert and restore. This module requires informatio­n on the folder path along with the virtual machine name. Here, we can use the informatio­n provided by vmware_guest_find on the folder path.

The following playbook example will show how to take a simple snapshot of a virtual machine:

--name: Example to take a snapshot of virtual machine hosts: localhost tasks:

- name: Take a snapshot


hostname: esxi_hostname_01

username: root

password: esxi@123

name: testvm01

folder: /vm

snapshot_name: my_secure_stable_snapshot

register: snap_state_001

- debug: msg=”{{ snap_state_001 }}”

You can read more about this module at http://docs.ansible. com/ansible/latest/vmware_guest_snapshot_module.html.

Managing clusters

vmware_cluster: This module allows you to manage a cluster in your VMware infrastruc­ture. It also allows you to set various cluster properties such as HA, vSAN and DRS.

The following example will add a sample cluster in a given VMware infrastruc­ture:

--name: Example playbook to add a cluster hosts: localhost tasks:

- name: Add cluster

vmware_cluster: validate_certs: false

hostname: esxi_hostname_01

username: root

password: esxi@123

datacenter_name: datacenter_01 cluster_name: esx_cluster_002 register: new_cluster_details - debug: msg=new_cluster_details

You can read more about this module at http://docs.

Managing data centres

vmware_datacenter: This module allows you to manage a data centre in your VMware infrastruc­ture. You can read more about this module at

Managing VMware networks

You can manage various VMware network objects such as a distribute­d virtual switch, virtual switch, port group, distribute­d virtual switch port group, etc.

You can read more about these various modules at html#vmware.

Ansible VMware modules are undergoing heavy developmen­t; you can take active part in this by developing or testing various modules. You can read more about hacking Ansible modules at devel/hacking/



[2] module.html

[3] facts_module.html

[4] ansible/modules/cloud/vmware/

[5] snapshot_module.html

[6] module.html

[7] datacenter_module.html

[8] modules.html#vmware


 ??  ??

Newspapers in English

Newspapers from India