Ansible: Automating esxcli using shell module

Automating your VMware infrastructure using Ansible

Abhijeet Kasurde
2 min readOct 7, 2020

Sometimes you find yourself in the situation where you want to automate some task in VMware ESXi host system and you do not find required Ansible module for the given task.

This article will focus on you can automate such tasks. Before proceeding, I would like to provide a disclaimer that Ansible modules provide idempotency with additional features such as check mode support and error handling, so this method should NOT be used general use cases but for the corner case only.

Photo by Olena Sergienko on Unsplash

Let us assume, you want to get information about all the virtual machines running on the given ESXi host system.

First, you will need to enable SSH service on ESXi host system. You can also use vmware_host_service_manager module to enable SSH service on ESXi host system as follows —

Once SSH service is enabled on ESXi, let us now do passwordless SSH configuration for ESXi. You can follow this document to do so.

Add ESXi host in your Ansible static inventory like —

[esxi1]
10.65.201.113 ansible_ssh_user=root

You can run esxcli using shell module like —

---
- hosts: esxi1
tasks:
- name: Get information about all the VMs
shell: esxcli vm process list
register: r

You will get the information about all VMs running on the given ESXi host system in register variable r and can be used in subsequent tasks.

For security reasons, it is not recommended to keep SSH enabled on ESXi host system. Please consider disabling SSH service after use or consider setting SSH idle timeout.

Thanks for reading and happy automating.

--

--