Using Ansible for Managing VMware Infrastructure

1
25266

This article is an introduction to different Ansible modules, which can be used to manage various VMware objects in VMware infrastructure.

Ansible is an IT automation tool. It can configure systems, deploy software and orchestrate advanced IT tasks such as continuous deployments or zero downtime rolling updates. Many of you already know that you can manage your VMware infrastructure using Ansible.

A recent development branch of Ansible, i.e., 2.4-devel (https://github.com/ansible/ansible/tree/devel), has brought in a lot of improvements in both its performance and its reliability.

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 Virtualisation Management Object Management Infrastructure’s (VMOMI) network exposed API. VMOMI can be used by various programming language bindings like PyVmomi (https://github.com/vmware/pyvmomi).

Getting started with Ansible

You can read more about Ansible official documentation at http://docs.ansible.com. 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 administrating 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 environment. 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

vmware_guest:

datacenter: datacenter01

esxi_hostname: esxi_host_01

hostname: 127.0.0.1

name: testvm01

guest_id: centos64guest

username: user

password: pass@123

state: present

disk:

- size_gb: 1

type: thin

datastore: LocalDS_0

hardware:

memory_mb: 512

num_cpus: 1

osid: centos64guest

scsi: paravirtual

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

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 information about a virtual machine. You can chain the information 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

vmware_guest_facts:

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.ansible.com/ansible/latest/vmware_guest_facts_module.html.

vmware_guest_find: This module provides information about the folder path in which the virtual machine is located. This information 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: 127.0.0.1

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_find.py.

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 functionality to manage various operations related to a snapshot such as create, delete, revert and restore. This module requires information on the folder path along with the virtual machine name. Here, we can use the information 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

vmware_guest_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 infrastructure. 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 infrastructure:

---

- 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.ansible.com/ansible/latest/vmware_cluster_module.html.

Managing data centres

vmware_datacenter: This module allows you to manage a data centre in your VMware infrastructure. You can read more about this module at http://docs.ansible.com/ansible/latest/vmware_datacenter_module.html.

Managing VMware networks

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

You can read more about these various modules at http://docs.ansible.com/ansible/latest/list_of_cloud_modules.html#vmware.

Ansible VMware modules are undergoing heavy development; you can take active part in this by developing or testing various modules. You can read more about hacking Ansible modules at https://github.com/ansible/ansible/blob/devel/hacking/README.md.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here