Ansible & VMware...and others

In short
  • Continuing the Ansible configuration to communicate with vCenter.
  • Static inventories (ini/yaml) or dynamic inventories via scripts, with the possibility of mixing them.
  • Controlling VMware VMs using Ansible based on simple tags.
  • Read-only service account on vCenter as an inventory source.

Introduction

After deploying Ansible on Windows and preparing a VMware template to be cloned, it's time to continue the Ansible configuration to allow it to interact with vCenter.

Ansible and Inventories

Ansible remains in its basic operation: an executable capable of connecting, primarily via SSH, to a remote target to execute commands.

Therefore, Ansible needs to be provided with one or more connection destinations.

We will use inventories for this purpose.

An inventory can be a simple flat file in ini or yaml format. It can also be dynamic through the use of Python scripts that query a third-party source to retrieve the list of targets to manage. It is even possible to mix inventories.

In the case of a dynamic inventory, you can write your own script, but the community also provides modules that add support for different sources.

Ansible: Inventory principle

Click the image to enlarge.

In the example that follows, the goal is to create the inventory from a vCenter. Ansible will then be able to retrieve the list of VMs, but also associate these VMs with groups based on various characteristics.

The advantage of creating groups is to be able to run commands on a set of servers performing the same role and therefore having identical components configured.

A VM can belong to multiple groups.

Several criteria can be used to create a group, such as the type of OS, membership in a network, or, as will be the case here, the presence of tags.

Still related to the acquisition of VMware by Broadcom, I may need to change platforms, and in that case, I would have to work on a new inventory source.

Although Ansible remains a leading platform in the market, there are numerous examples of inventory management available, and the community provides many scripts and modules. It is highly likely that I can transition to another platform without too much difficulty. In any case, the overall logic presented here remains valid and can serve as a basis in other situations.

Managing VMware with Ansible

Ansible is deployed on a Windows machine via WSL

Additional Components

To interact with the VMware ecosystem, Ansible requires additional modules, which themselves rely on third-party components.

We will start by installing the Python package manager, pip

sudo apt install python3-pip

Let's make sure we have the latest versions of the Python components:

pip3 install --upgrade pip setuptools

Updating Python dependencies

Click the image to enlarge.

We can now install the VMware Python SDK:

pip3 install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git

Installing the VMware Python SDK

Click the image to enlarge.

Even in its current state, Ansible would already be capable of functioning, but it is preferable to use the community plugin for more options.

A large number of plugins and other example resources are available on the Ansible Galaxy website, which has become a valuable resource for all Ansible users.

There is a native command to retrieve a module published on Galaxy:

ansible-galaxy collection install community.vmware

Installing the community Ansible module for VMware

Click the image to enlarge.

Update 08/18/2024 - Starting with Ubuntu 24.04

When I updated my Ubuntu instance to 24.04, I noticed an issue between the version of Python provided and the vSphere libraries included in the pyvmomi package.

If you are in this situation, you can follow the instructions given here and then return to this article to continue the configuration.

Ansible Configuration

Now that everything is ready, we will tell Ansible to use the VMware plugin as the inventory source.

We will edit our local configuration file vim ~/.ansible.conf

In the default section, we will specify the location of the inventory files (since I am using WSL, my Windows directory structure is accessible in /mnt).

Location of the inventory file

Click the image to enlarge.

In the inventory section, we add the option community.vmware.vmware_vm_inventory to the enable_plugin section

Adding support for the VMware inventory module

Click the image to enlarge.

Connection Configuration

We can now create the configuration file needed for Ansible to connect to vCenter.

It is highly recommended to create a dedicated service account. This account can be local to vCenter or sourced from an enterprise directory that can be queried by vCenter.

In my example, I have an Active Directory, so I will use it and create a specific account.

Creating an AD account

Click the image to enlarge.

The account must be added to vCenter. For our purposes, Ansible can connect in read-only mode.

Creating the service account

Click the image to enlarge.

We don't use Ansible to deploy VMware resources (I prefer to use terraform for that). Access to vCenter is only useful for retrieving the list of VMs that Ansible needs to connect to.

The SSH connection to the VMs is not made via this service account, but via a local account present in the base Linux template (and therefore inherited in all cloned VMs).

It is necessary to create the configuration file in the location where Ansible expects to find its inventory files.

Within this directory, you must create a YAML file named vcenter.vmware.yml that will contain the connection information for vCenter

    
plugin: community.vmware.vmware_vm_inventory
strict: False
hostname: vcenter.coolcorp.priv
username: [email protected]
password: mon_password_non_ce_nest_pa_ça
validate_certs: False
with_tags: True
hostnames:
- 'config.name'
keyed_groups:
- key: tags
separator: ''
  
Contents of the inventory configuration file

Click the image to enlarge.

The keyed_groups section allows you to add criteria to define the groups of VMs

As you can see, the concept of "tags" is included so that Ansible can use them

In hostname, we tell Ansible to consider the config.name information as the VM name. This is important because, by default, Ansible will consider the VM's UUID, which is a more complex variable to manipulate. It will be simpler to run a playbook on a VM by specifying its name in the vCenter inventory rather than its UUID.

The most critical part is adding the credentials associated with the service account used to communicate with vCenter. This is sensitive information.

Fortunately, Ansible includes the ability, via the ansible-vault command, to encrypt this file to make it unreadable in case of theft or loss.

There are different ways to use ansible-vault that I will not detail here.

We will simply encrypt the inventory so that each time you read it, Ansible asks for a decryption password:

ansible-vault encrypt /mnt/mon_emplacement_d_inventaire/vcenter.vmware.yml

Encrypting the inventory file with Ansible Vault

Click the image to enlarge.

If we reread the inventory file, it is now encrypted and unreadable without the password entered during the previous command.

Contents of the encrypted file

Click the image to enlarge.

We can run an Ansible command to verify that everything is working.

Let's request the list of VMs with their information.

ansible-inventory vcenter.vmware.yml --list --ask-vault-pass

Retrieving the inventory

Click the image to enlarge.

(If we do not specify the –ask-vault-pass option, Ansible will not ask for the decryption password and the inventory file cannot be read).

The same command with the graph option allows you to display a tree view of your inventory

ansible-inventory --ask-vault-pass --graph

Presenting the inventory in a graph

Click the image to enlarge.

Since two VMs have a common tag, we can see that Ansible has associated them with a group named after the tag "TAG_COOLCORP_APPS_CRYPTO".

The case of tdnf

To definitively validate the configuration, we will try to deploy a package on the VM template.

First, we need to perform a small additional installation.

Indeed, as I mentioned in the section about my template, the operating system used, PhotonOS, is somewhat particular.

By default, it uses a "lightweight" version of the package manager dnf called tdnf. However, this package manager cannot be directly manipulated by Ansible.

VMware nevertheless offers an additional module to support tdnf.

Let's check if Ansible expects to find its modules by default using the following command:

ansible-config dump |grep DEFAULT_MODULE_PATH

Location of the modules

Click the image to enlarge.

Create the folder specific to your profile if necessary (normally ~/.ansible/plugins/modules)

Then, copy the tdnf.py module available here

(the following command simplifies this operation)

wget https://raw.githubusercontent.com/vmware/photon/master/SPECS/ansible/tdnf.py

Let's ask Ansible to deploy the iotop package in its latest version on the VM template, using the newly added tdnf module

ansible nom_template -m tdnf -a "name=iotop state=latest" --become --ask-vault-pass

Installing a tdnf package

Click the image to enlarge.

In summary

The VMware infrastructure that I use has a preconfigured template based on an operating system optimized for running containers.

Ansible is deployed on a Windows machine via the WSL layer and has read-only access to vCenter, allowing it to use the latter as an inventory source.

The template contains the SSH credentials that allow Ansible to connect to it.

Therefore, after deploying VMs via terraform based on this template, we can pass control to Ansible so that it can subsequently deploy all the prerequisites and configuration files specific to installing a Kubernetes cluster.