Tests
The philosophy to test is:
- Test multiple distributions.
- Test on every change and on a schedule, because distributions, vendors and Ansible change over time.
- Test the intent of the role, not just that it runs.
Where roles are tested
Every role is tested twice, on GitHub Actions (.github/workflows/molecule.yml) and on GitLab CI (.gitlab-ci.yml). GitLab is a mirror of GitHub, it picks up every push a few minutes later and runs its own pipeline, on amd64 runners. A difference between the two is interesting, it usually points at the CI platform and not at the role.
These files are generated by ansible-generator from meta/main.yml and a few other files, so all roles are tested the same way.
Distributions
The matrix of distributions comes from the platforms in a role’s meta/main.yml. A role only lists the platforms it supports. These are the container images that can be used:
- Alpine (
alpine,latest) - Amazon Linux (
amazonlinux,latest) - Enterprise Linux (
enterpriselinux,latest) - Debian (
debian,latestandbookworm) - Fedora (
fedora,44andlatest) - Ubuntu (
ubuntu,latestandjammy)
See distribution relations for the images, tags and branches.
Unit tests
To test an Ansible role, Molecule runs it in containers. The containers run systemd (OpenRC on Alpine), so services can really be started. Molecule runs these steps:
dependency: install the roles and collections the role needs (requirements.yml).create: start a container.prepare: run the roles that have to run before this role (molecule/default/prepare.yml).converge: run the role (molecule/default/converge.yml).idempotence: run the role again, nothing may change.verify: test the intent of the role, for example that a port is open or that the installed software shows its version (molecule/default/verify.yml).destroy: remove the container.
The tests use the latest release of ansible-core. This verifies that the role does its job, but does not ensure that it works in combination with all other roles, only with the roles in prepare.yml.
Lint
Every pull request is linted with ansible-lint (and the pre-commit hooks). Lint has to pass before the Molecule job runs. See the style guide.
Time based unit tests
Because distributions, Molecule, and Ansible change over time, a monthly test is done to all roles. The first letter of the role determines the day of the month it’s tested: a runs on the 1st, b on the 2nd, up to z on the 26th. For example ansible-role-example starts with an e, so it runs on the 5th of the month.
When does what run
| Event | GitHub Actions | GitLab CI |
|---|---|---|
| Pull request | lint only | - |
| Push to master | lint and Molecule | lint and Molecule (after the mirror syncs) |
| Monthly schedule | Molecule | Molecule |
| Manually | workflow_dispatch |
run pipeline |
Known limits
- The containers share the kernel of the CI runner. Roles that load kernel modules (
modprobe) can only really do that where the runner kernel has them. GitHub does, the GitLab shared runners do not. - Some vendors only publish packages for some CPU architectures or distribution releases, then that platform is left out of
meta/main.yml.