Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using Molecule for Ansible Testing and Validation

Introduction

In this tutorial, we will explore how to use Molecule for testing and validating your Ansible roles. Molecule is a powerful tool that helps you ensure your roles work as expected across different environments and scenarios. This tutorial will guide you step-by-step from installation to running your first test suite.

Prerequisites

Before getting started with Molecule, ensure you have the following installed:

  • Python 3.6+
  • Ansible 2.9+
  • Docker (for containerized testing)

Installing Molecule

First, we need to install Molecule and its dependencies. You can do this using pip:

pip install molecule[docker]

If you plan to use other drivers (e.g., Vagrant, Podman), you may need to install additional dependencies. Refer to the official documentation for more details.

Creating a New Role with Molecule

To create a new Ansible role with Molecule support, use the following command:

molecule init role my_role

This command will create a new directory structure for your role, including a molecule directory with default configuration files.

Understanding the Directory Structure

After initializing a new role, you will see the following structure:

my_role/
├── defaults
│   └── main.yml
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── molecule
│   └── default
│       ├── converge.yml
│       ├── molecule.yml
│       └── verify.yml
├── tasks
│   └── main.yml
├── tests
│   └── test.yml
└── vars
    └── main.yml
                

The molecule/default directory contains the configuration and playbooks used for testing.

Configuring Molecule

The molecule.yml file in the molecule/default directory is the main configuration file for your tests. Here is an example configuration:

---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: instance
    image: molecule_local/ubuntu:latest
    pre_build_image: true
provisioner:
  name: ansible
verifier:
  name: ansible
                

Writing Test Scenarios

Molecule uses scenarios to define how your roles should be tested. By default, a scenario named default is created. The main playbook for the scenario is converge.yml.

Here is an example converge.yml playbook:

---
- name: Converge
  hosts: all
  roles:
    - role: my_role
                

This playbook applies your role to the test instances.

Running Tests

To run your tests, use the following command:

molecule test

This command will execute a series of steps including linting, dependency resolution, creating instances, applying the role, and verification.

Verifying Results

Verification is done using the verify.yml playbook. Here is an example:

---
- name: Verify
  hosts: all
  tasks:
    - name: Check if apache is installed
      command: apache2 -v
      register: result
      failed_when: result.rc != 0
                

This playbook checks if Apache is installed on the test instances.

Cleaning Up

After testing, you can clean up the test environment using:

molecule destroy

This command will remove all test instances created during the test process.

Conclusion

In this tutorial, we covered how to use Molecule for testing and validating Ansible roles. By following these steps, you can ensure your roles are reliable and work as expected across different environments.