Ansible Collection Role Testing with Molecule

Ansible Collection Testing with Molecule Converge Action.

Luca Berton

--

Introduction

When developing Ansible roles within a collection, it is crucial to ensure that your code functions as expected and integrates seamlessly with the broader infrastructure. Molecule, a testing framework for Ansible roles, allows developers to automate the testing process, making it easier to catch issues early in the development cycle. In this article, we will explore how to test an Ansible role within a collection using Molecule, with a focus on the converge action.

Molecule in Ansible Collection

Adding Molecule to your Ansible collection is a straightforward process that enhances your development and testing workflow. Start by creating a new directory within your collection named “extensions.” Navigate to this newly created directory using the command line and then initialize a new default Molecule scenario with the following:

cd <path to your collection>/extensions/
molecule init scenario

This step sets the foundation for incorporating Molecule into your collection, allowing you to seamlessly integrate testing and development practices. The newly created scenario within the “extensions” directory becomes a pivotal component in orchestrating Molecule’s testing lifecycle for your Ansible roles and playbooks.

Example Role Structure

To illustrate the testing process, consider the structure of a sample role located at myrole/tasks/main.yml:

---
- name: Task running inside the role
ansible.builtin.debug:
msg: "This is a task from my_role"

This simple task, when executed, prints a debug message to provide a basic demonstration of the role’s functionality.

Configuring Molecule with molecule.yml

The configuration file molecule.yml outlines the testing environment and Ansible provisioner settings. Below is an example configuration:

---
platforms:
- name: instance

provisioner…

--

--

Luca Berton

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday