Lesson 7 of 9 · 28m
Testing Roles Before They Touch Production
Combine syntax checks, lint, Molecule scenarios, verification tests, and idempotence assertions to catch configuration defects before fleet rollout.
Configuration management fails quietly when testing stops at ansible-playbook --syntax-check. Syntax only proves the YAML can be parsed. It says nothing about whether the service starts, whether the role is idempotent, or whether a template renders valid configuration on a real operating system. Testing roles is cheaper than debugging configuration drift one host at a time.
Build the cheap gates first
Every role repository should run fast checks before expensive integration tests:
ansible-playbook -i inventory/test site.yml --syntax-check
ansible-lint roles/nginx
yamllint roles/nginxLint is not style vanity. It catches deprecated modules, risky shell use, missing modes, truthy YAML traps, and tasks without names. A no-name task is tolerable until it appears in an incident transcript as command with no context.
Molecule creates a disposable blast radius
Molecule runs a role against ephemeral containers or VMs and then verifies behavior:
# molecule/default/molecule.yml
driver:
name: docker
platforms:
- name: debian12
image: geerlingguy/docker-debian12-ansible:latest
provisioner:
name: ansible
verifier:
name: ansibleThe point is not that containers perfectly match production. The point is fast feedback on role behavior, idempotence, and basic service activation. Add platform scenarios for operating systems you genuinely support; do not pretend one Debian container proves correctness on RHEL, Ubuntu LTS, and an appliance image.
Verify externally visible truth
Verification should test what the role promised, not merely what tasks ran:
- name: Verify nginx is active
ansible.builtin.service_facts:
- name: Assert nginx is running
ansible.builtin.assert:
that:
- ansible_facts.services['nginx.service'].state == 'running'
- name: Verify TLS protocol configuration
ansible.builtin.uri:
url: https://127.0.0.1/
validate_certs: false
status_code: 200Service facts, port checks, HTTP responses, file mode checks, and command validation are stronger evidence than task recap colors. Test user-facing truth where possible.
Idempotence is a test requirement
Run the role twice. The second run should produce no unexpected changes:
molecule converge
molecule idempotenceDo not accept output that requires tribal knowledge to classify. If a shell command always changes, write changed_when based on meaningful output or replace it with a state-aware module. Permanent noise in Molecule becomes permanent noise in production.
Test failure paths as well as happy paths
What happens when the package repository is unavailable? When TLS files are absent? When a variable is empty? When service startup fails? You cannot reproduce every disaster, but you can ensure assertions fail early and messages are useful.
CI is part of the role contract
Pin Ansible versions, collection versions, lint rules, and test images. If CI passes on Ansible 2.15 but production runs 2.16 with different module behavior, the role contract is fiction. Keep the control node environment reproducible just as carefully as target configuration.
Field notes
- Containers usually lack full systemd behavior unless images are built for it; do not misread that as proof of service correctness.
--checkis not a substitute for integration testing. Modules differ in check-mode support.- Do not mock away the exact behavior you are trying to prove.
- A role that needs production secrets to test should use generated test secrets, never real ones.
- Keep tests fast enough that engineers actually run them before pushing.
Was this lesson helpful?