tox-ansible
tox-ansible is a plugin for the tox testing automation tool that dynamically generates a full matrix of test environments for Ansible collections. It creates combinations of Python interpreters and ansible-core versions, running integration, sanity, and unit tests across all of them — both locally and in GitHub Actions.
Installation
Section titled “Installation”# Via the full suite (recommended)pip install ansible-dev-tools
# Standalonepip install tox-ansibleVerify installation:
tox --version# tox-ansible should appear in the plugin listCore concepts
Section titled “Core concepts”Dynamic environment generation
Section titled “Dynamic environment generation”Unlike standard tox where you manually define every test environment, tox-ansible automatically discovers your collection structure and generates environments based on:
- Available Python interpreters on the system
- Supported ansible-core versions
- Test types found in the collection (integration, unit, sanity)
This means your tox.ini can be minimal — tox-ansible handles the combinatorial explosion.
Test types
Section titled “Test types”tox-ansible generates environments for three test types:
| Type | What it tests | How |
|---|---|---|
| integration | Molecule scenarios | Discovers extensions/molecule/ scenarios and runs them |
| unit | Python unit tests | Runs pytest against tests/unit/ |
| sanity | Ansible sanity checks | Runs ansible-test sanity |
Environment naming
Section titled “Environment naming”Generated environments follow the pattern:
{python_version}-{ansible_core_version}-{test_type}[-{scenario}]Examples:
py312-2.17-integration-defaultpy311-2.16-unitpy312-2.17-sanitypy312-2.17-integration-failoverConfiguration
Section titled “Configuration”Minimal tox.ini
Section titled “Minimal tox.ini”For most collections, a minimal configuration is sufficient:
[tox]requires = tox>=4.0 tox-ansible>=26.0
[ansible]# tox-ansible reads this sectionskip = # Skip specific scenarios or test types 2.16-integrationConfiguration options
Section titled “Configuration options”The [ansible] section in tox.ini supports:
| Key | Description |
|---|---|
skip |
List of environment patterns to skip |
python |
Python versions to test (defaults to discovered interpreters) |
ansible-core |
ansible-core versions to test |
Integration with ansible-dev-environment
Section titled “Integration with ansible-dev-environment”tox-ansible delegates collection installation to ade (ansible-dev-environment), ensuring:
- Collections are installed in editable mode within each tox environment
- Python and collection dependencies are properly resolved
- Each environment is fully isolated
CLI usage
Section titled “CLI usage”List generated environments
Section titled “List generated environments”# See all environments tox-ansible will generatetox listRun all tests
Section titled “Run all tests”# Run the full matrixtoxRun specific environments
Section titled “Run specific environments”# Run only integration tests on Python 3.12 with ansible-core 2.17tox -e py312-2.17-integration
# Run only unit teststox -e py312-2.17-unit
# Run sanity checkstox -e py312-2.17-sanity
# Run a specific molecule scenariotox -e py312-2.17-integration-defaultParallel execution
Section titled “Parallel execution”# Run environments in paralleltox -p auto
# Run with a specific number of workerstox -p 4GitHub Actions integration
Section titled “GitHub Actions integration”--gh-matrix flag
Section titled “--gh-matrix flag”tox-ansible can output its generated environment list as a GitHub Actions matrix JSON, enabling dynamic CI matrices:
tox --gh-matrixThis outputs JSON suitable for use in a GitHub Actions matrix strategy.
Workflow example
Section titled “Workflow example”name: Collection Testson: [push, pull_request]
jobs: generate-matrix: runs-on: ubuntu-latest outputs: matrix: ${{ steps.matrix.outputs.envlist }} steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: "3.12" - name: Install tox-ansible run: pip install tox-ansible - name: Generate matrix id: matrix run: | echo "envlist=$(tox --gh-matrix)" >> $GITHUB_OUTPUT
test: needs: generate-matrix runs-on: ubuntu-latest strategy: fail-fast: false matrix: env: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }} steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: python-version: ${{ matrix.env.python }} - name: Install dependencies run: pip install ansible-dev-tools molecule-plugins[docker] - name: Run tests run: tox -e ${{ matrix.env.name }} env: PY_COLORS: "1" ANSIBLE_FORCE_COLOR: "1"Benefits over static matrices
Section titled “Benefits over static matrices”| Static matrix | tox-ansible matrix |
|---|---|
| Must manually list all Python × Ansible combinations | Auto-discovers available combinations |
| Breaks when new versions are released | Adapts to new versions automatically |
| Duplicates information between tox.ini and workflow | Single source of truth in tox.ini |
| Hard to keep in sync | Always consistent |
Collection testing workflow
Section titled “Collection testing workflow”Typical project structure
Section titled “Typical project structure”my_collection/├── galaxy.yml├── tox.ini├── plugins/│ └── modules/│ └── my_module.py├── tests/│ └── unit/│ └── test_my_module.py└── extensions/ └── molecule/ ├── default/ │ ├── molecule.yml │ ├── converge.yml │ └── verify.yml └── failover/ ├── molecule.yml ├── converge.yml └── verify.ymlRunning locally
Section titled “Running locally”# Install the collection in development modeade install -e .[test] --venv .venvsource .venv/bin/activate
# Run all tests via toxtox
# Run just the default molecule scenariotox -e py312-2.17-integration-default
# Run unit tests onlytox -e py312-2.17-unitWhat tox-ansible does for each environment
Section titled “What tox-ansible does for each environment”- Creates an isolated virtual environment
- Installs the specified ansible-core version
- Uses
adeto install the collection in editable mode - Installs test dependencies
- Runs the appropriate test command:
- Integration:
molecule test -s <scenario> - Unit:
pytest tests/unit/ - Sanity:
ansible-test sanity
- Integration:
Additional test runners
Section titled “Additional test runners”tox-ansible also supports:
- galaxy-importer — validates the collection for Galaxy publishing
- Custom test commands via standard tox
[testenv]configuration
Editor integration
Section titled “Editor integration”Collections scaffolded by ansible-creator include a pre-configured tox.ini that works with tox-ansible out of the box. The VS Code extension recognizes tox configurations and provides task integration for running tests.
Next steps
Section titled “Next steps”Your collection is tested across the matrix. Now run it for real and package it:
- Run playbooks with ansible-navigator — execute in containers with the same isolation as production
- Build an execution environment with ansible-builder — package your collection and its dependencies into a container image