Ansible Role tests/ Directory: Legacy Travis CI Artifact vs Modern Testing
Bottom line
The tests/ directory inside an Ansible role is a legacy artifact from 2015 created for Travis CI integration with Ansible Galaxy 2.0. It's not mentioned in the official Ansible role documentation, is not consumed by any modern testing tool by default. Is widely treated as optional boilerplate that can be deleted. The modern standard for testing Ansible roles is Molecule (for integration testing with provisioned infrastructure) and ansible-test (for collection sanity/unit tests). If you aren't using Travis CI or a custom runner that explicitly looks at tests/test.yml, the directory serves no purpose.
Key findings
- Finding: The
tests/directory was introduced in December 2015 via Ansible PR #13489 ("Galaxy 2.0") specifically to support Travis CI. The generatedtests/test.ymlwas designed to runansible-playbook --syntax-checkinside Travis builds. No other tool uses it by default. - Finding: The official Ansible documentation for role directory structure lists
tasks/,handlers/,templates/,files/,vars/,defaults/,meta/,library/,module_utils/, andlookup_plugins/- but doesn't listtests/. This is source-stated omission. - Finding:
ansible-galaxy role initstill scaffoldstests/test.ymlandtests/inventoryin 2025, but multiple independent guides (PyNet Labs, Spacelift, ComputingForGeeks) explicitly advise deleting it if you aren't using it. - Finding: Molecule is the community-accepted standard for role integration testing. It uses its own
molecule/directory, nottests/. Molecule supports Docker, Podman, VMs, and cloud instances, and can verify idempotence, run side-effect tests, and assert final state withverify.yml.
Background
Ansible roles follow a conventional directory structure to organize reusable automation. In 2015, Ansible Galaxy 2.0 added ansible-galaxy role init scaffolding that included a tests/ folder containing:
tests/test.yml- a minimal playbook applying the role tolocalhosttests/inventory- an inventory file containing justlocalhost
This was bundled with a .travis.yml template that ran:
ansible-playbook tests/test.yml -i tests/inventory --syntax-check
The intent was to give role authors on Galaxy a free CI smoke-test. Travis CI would import the GitHub repo, find .travis.yml, and run the syntax check.
Current state
As of 2025–2026:
ansible-galaxy role initstill createstests/, but the directory is orphaned. Travis CI's free tier for open source is largely discontinued, and no other tool automatically consumestests/.- The official Ansible "Role directory structure" docs don't describe
tests/as a standard role directory. - Molecule (maintained under the Ansible project umbrella) is the active testing framework. Molecule scenarios live under
molecule/<scenario_name>/and includeconverge.yml,verify.yml,molecule.yml, etc. - For collections,
ansible-testis the primary tool for sanity, unit, and integration tests. It usestests/integration/targets/inside collections, not role-leveltests/.
Technical or implementation details
| Aspect | tests/ directory |
Molecule |
|---|---|---|
| Location | roles/<name>/tests/ |
roles/<name>/molecule/ |
| Default contents | test.yml, inventory |
molecule.yml, converge.yml, verify.yml, prepare.yml |
| What it does | Runs a playbook against localhost |
Provisions infra, applies role, checks idempotence, verifies state, destroys infra |
| Drivers | None (manual/ansible-playbook only) | Docker, Podman, Delegated, Vagrant, cloud |
| Tooling support | Travis CI (legacy) | molecule test, molecule verify, CI matrixes |
| Assertion support | None built-in | verify.yml with assert, ansible.builtin.uri, etc. |
The typical tests/test.yml is:
- name: Test role
hosts: localhost
remote_user: root
roles:
- myrole
This only proves the role can be parsed and applied to the local machine as root. It doesn't verify system state, test across OS versions, or enforce idempotence.
Evidence, comparisons, and related context
- Molecule vs
tests/: Molecule was designed to replace ad-hoc local testing. Red Hat's official blog (2020) and ComputingForGeeks (2026) both present Molecule as the standard. The Ansible forum discussion (May 2024–Jan 2026) shows active community consensus that Molecule is the right tool for role integration tests.ansible-testis preferred for collection sanity/unit tests. - Collections vs standalone roles: In collections, integration tests use
tests/integration/targets/<target>/withansible-test. This is unrelated to the roletests/directory. Ansible core dev mattclay noted in 2019 that collections were migrating to usetests/(plural) at the collection root to matchroles/andplugins/- but this refers to collection-level testing, not role-leveltests/. - Galaxy 2.0 origin: Stack Overflow and GitHub PR #13489 confirm the
tests/directory was added for Galaxy 2.0 + Travis CI. The.travis.ymltemplate was part of the same PR.
Limitations and critiques
- The
tests/directory is not integrated withansible-lint,ansible-test, or Molecule. It's invisible to modern CI unless you manually wire it. tests/test.ymlhardcodesremote_user: rootandhosts: localhost, making it unsuitable for most real-world testing scenarios (e.G., SSH-based cloud VMs, unprivileged users).- There is no official documentation explaining what the
tests/directory is for inside a role. The Ansible forum thread from 2024 explicitly asks this question and receives no authoritative answer beyond "it's for Travis CI." - Keeping
tests/in a role can confuse newcomers who expect it to be automatically executed or validated by modern tooling.
Open questions
- Will a future version of
ansible-galaxy role initremove thetests/directory or replace it with a Molecule scaffold? - Is there any active effort to make
ansible-testconsume role-leveltests/for integration testing, or has that path been permanently ceded to Molecule?
Practical takeaways
- If you aren't using Travis CI: Delete the
tests/directory. It provides no value and isn't used by Molecule oransible-test. - For proper role testing: Use Molecule. Initialize scenarios with
molecule init scenario, writeverify.ymlto assert system state, and runmolecule testin CI (GitHub Actions, GitLab CI, etc.). - For collections: Use
ansible-testfor sanity/unit tests and Molecule for integration tests that need multi-OS container/VM coverage.
Sources used
- Roles - Ansible Community Documentation - https://docs.ansible.com/projects/ansible/latest/playbook_guide/playbooks_reuse_roles.html
- Which tools use the "tests" directory in an Ansible role by default? - Stack Overflow - https://stackoverflow.com/questions/70898834/which-tools-use-the-tests-directory-in-an-ansible-role-by-default
- Galaxy 2.0 PR #13489 - GitHub - https://github.com/ansible/ansible/pull/13489
- How to use roles/../tests directory - Ansible Forum - https://forum.ansible.com/t/how-to-use-roles-tests-directory/37778
- Ansible Molecule - Official Documentation - https://docs.ansible.com/projects/molecule/
- Testing Ansible - Ansible Community Documentation - https://docs.ansible.com/projects/ansible/latest/dev_guide/testing.html
- Allow use of 'tests' directory for storage of ansible-test tests - GitHub Issue #60218 - https://github.com/ansible/ansible/issues/60218
- Should we be using molecule or ansible-test for unit/integration tests? - Ansible Forum - https://forum.ansible.com/t/should-we-be-using-molecule-or-ansible-test-for-unit-integration-tests/5539
- Test Ansible Roles with Molecule (2026) - ComputingForGeeks - https://computingforgeeks.com/ansible-molecule-testing/
- Developing and Testing Ansible Roles with Molecule and Podman - Red Hat - https://www.redhat.com/en/blog/developing-and-testing-ansible-roles-with-molecule-and-podman-part-1
- Ansible Roles: Step-by-Step Guide (2025) - PyNet Labs - https://www.pynetlabs.com/ansible-roles/