🧪 A Top 1% Hands-On: Automatically Testing Your Own Ansible Role With Molecule — Spinning Up Disposable Docker Containers and Testing Idempotency Itself
Introduction
- What You’ll Learn From This Article: The Error Handling Hands-On covered
changed_whenandfailed_when, mechanisms for controlling a single task’s own behavior. In this article, you’ll get hands-on and systematically understand Molecule, a framework for automatically testing an entire role on disposable Docker containers. - Intended Audience: Readers who can write Ansible roles, but don’t know how to automatically test the role itself.
- Estimated Reading Time: About 25 minutes
- Hands-On Prerequisite Environment: A Linux environment running Docker (or Podman), Python 3,
pip install molecule molecule-plugins[docker] pytest-testinfra
This article is part of the Top 1% Series Complete Article Guide, and article 11.3 in the Ansible/IaC Series.
Prerequisite Knowledge
- Error Handling Hands-On: Understanding the basic usage of
changed_whenandfailed_when. - Ansible Roles Hands-On: Understanding a role’s directory structure.
Getting the Big Picture
Molecule is a framework that spins up a disposable Docker container as a test environment, automating a full sequence — create, prepare, converge, idempotence test, verify, destroy — behind a single molecule test command. Among these, the “idempotence test” is a mechanism that verifies Ansible’s own design philosophy directly: it runs the same playbook a second time, and fails the test itself the instant even one single task gets reported as changed.
graph TB
Test["molecule test"] --> Create["create: provision the container"]
Create --> Converge["converge: run the playbook once"]
Converge --> Idempotence["idempotence: run the same playbook a second time"]
Idempotence -->|"0 changed"| Verify["verify: check actual state with Testinfra"]
Idempotence -->|"1+ changed"| Fail["Test fails (idempotence is broken)"]
Verify --> Destroy["destroy: tear down the container"]
Deep Dive Into the Fundamentals
Three Typical Causes of an Idempotence Test Failure
There are three typical causes behind an idempotence test failure. The first is a task using the command or shell module with no changed_when set, so it always gets reported as changed even when nothing actually changed. The second is a template containing a dynamic value, such as the execution timestamp, so the file’s content differs every single time. The third is a package-installation task specifying state: latest, which triggers an update every single time a newer version happens to exist. The changed_when covered in the Error Handling Hands-On is exactly the direct countermeasure for that first cause.
Hands-On: Testing an Nginx-Installing Role With Molecule
- Initialize a Molecule scenario (since
molecule init rolewas removed in Molecule 6.0 and later, create the role itself withansible-galaxy role init, and add only the test scenario separately).
ansible-galaxy role init nginx_role
cd nginx_role
molecule init scenario --driver-name docker
- In
molecule/default/converge.yml, write a playbook that applies the role under test.
- name: Converge
hosts: all
tasks:
- name: "Include nginx_role"
ansible.builtin.include_role:
name: "nginx_role"
- Run
molecule test, and container creation, playbook execution, the idempotence test, verification, and teardown all execute automatically, in sequence.
molecule test
- In
molecule/default/tests/test_default.py, write verification code using Testinfra.
def test_nginx_is_running_and_enabled(host):
nginx = host.service("nginx")
assert nginx.is_running
assert nginx.is_enabled
def test_nginx_listening_on_port_80(host):
assert host.socket("tcp://0.0.0.0:80").is_listening
A successful converge alone only tells you “it ran without an error.” Only the verify stage, confirming Nginx is actually running and listening on port 80, guarantees that the state is actually what you intended.
molecule test's Standard Test Sequence
The default molecule test automatically runs a full sequence in order: lint, dependency, syntax, create, prepare, converge, idempotence, verify, destroy. In real-world practice, it’s common to repeatedly run just molecule converge while developing and adjusting the role, then run the full sequence with molecule test at the end.
What a Pro Sees Here (Top 1% Understanding)
“The Playbook Succeeded” and “The Role Is Correct” Are Entirely Different Guarantees
The fact that a playbook ran without an error guarantees neither that it reached the intended state, nor that re-running it produces the same result. The habit of setting up a Molecule-based automated test environment at the same time you write a role is the perspective a top-1% engineer uses to escape the reactive pattern of only noticing a broken idempotence after hitting an unexpected changed in production.
Common Misconceptions and Pitfalls
- Misconception 1: “If the playbook runs without an error, the role is written correctly.” Running without an error, reaching the intended state, and reproducing the same result on a re-run are three entirely separate guarantees, and Molecule verifies all three at separate stages.
- Misconception 2: “
molecule init rolecreates the role and adds a test scenario at the same time.”molecule init rolewas removed in Molecule 6.0 and later — create the role itself withansible-galaxy role init, and add the test scenario separately withmolecule init scenario. - Misconception 3: “If converge (the first run) succeeds, the idempotence test is unnecessary.”
Even if the first run succeeds, the second run can still report a
changedtask, and the idempotence test is a separate, independent stage that catches exactly this automatically.
Troubleshooting Perspective
molecule test’s idempotence test fails: Check for missingchanged_whenoncommand/shelltasks, dynamic values in templates, and use ofstate: latest.convergesucceeds butverifyfails: Even though the playbook ran without an error, the intended state (a service’s running state, a port’s listening state) may not actually have been reached.- The
molecule init rolecommand can’t be found: It was removed in Molecule 6.0 and later — useansible-galaxy role initcombined withmolecule init scenarioinstead.
Summary
- Molecule is a framework that spins up a disposable Docker container as a test environment and automatically tests an entire role.
- The idempotence test runs the same playbook twice and fails if the second run reports a
changedtask, verifying Ansible’s own design philosophy directly. - The three typical causes of an idempotence test failure are a
command/shelltask missingchanged_when, a dynamic value in a template, and use ofstate: latest. - The
verifystage uses Testinfra to confirm a service has actually reached its intended state.
Takeaways to Apply Today
- When you write a new role, set up a Molecule-based automated test environment for it at the same time.
- When an idempotence test fails, suspect a missing
changed_whensetting first.