Chapter 14. Quality Assurance with Molecule

If you want to develop a role, then you need test infrastructure. Using disposable Docker containers is a perfect fit for testing with multiple distributions, or versions, of Linux without touching the machines others use.

Molecule is a Python testing framework for Ansible roles. Using it, you can test multiple instances, operating systems, and distributions. You can use a couple of test frameworks and as many testing scenarios as you need. Molecule is extensible in its support for various virtualization platforms, using a type of plug-in called a driver. A driver, for a provider, is a Python library that is used to manage test hosts (that is, to create and destroy them).

Molecule encourages an approach that results in consistently developed roles that are well-written and easily understood and maintained. Developed as open source on GitHub since 2015 by @retr0h, Molecule is now community-maintained as part of the Ansible by Red Hat project.

Installation and Setup

Molecule depends on Python version 3.6 or greater and Ansible version 2.8 or greater. Depending on your operating system, you might need to install additional packages. Ansible is not a direct dependency but is called as a command-line tool.

For Red Hat, the command is:

# yum install -y gcc python3-pip python3-devel openssl-devel python3-libselinux

For Ubuntu, use:

# apt install -y python3-pip libssl-dev

After installing the required dependencies, you can install Molecule ...

Get Ansible: Up and Running, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.