Registry / testing / pytest-ansible

pytest-ansible

JSON →
library26.8.0pypypi✓ verified 24d ago

The `pytest-ansible` plugin integrates pytest with Ansible, enabling efficient testing of Ansible-related tasks and scenarios. It facilitates unit testing for Ansible collections, integrates with Molecule scenarios, and allows direct Ansible integration within pytest tests. It supports Python 3.10+ and Ansible-core 2.14+.

pip install pytest-ansible
INSTALL
IMPORT
SIG · PYTEST-ANSIBLE
P
pytest-ansible
testingpythonv26.8.0
Install
5.2s avg
Import
Disk
65MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v26.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 72.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 5.2s · import 0.000s · 73MB
65MB installed
● package 65MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ansible_adhoc
import pytest # Fixtures like ansible_adhoc are made available by the plugin
pytest-ansible primarily provides fixtures (e.g., `ansible_adhoc`, `ansible_module`) that are automatically discovered by pytest. Direct imports from `pytest_ansible` are generally not needed for typical test usage.

This quickstart demonstrates how to write a basic pytest test using the `ansible_adhoc` fixture provided by `pytest-ansible`. It shows how to execute an Ansible module (like `setup` or `ping`) against a local host and assert its results. A minimal Ansible inventory file (`hosts.ini`) is required for the tests to run successfully, which can be specified via the `--ansible-inventory` pytest option.

import pytest import os # --- Required Setup: inventory file (e.g., hosts.ini) --- # [local] # localhost ansible_connection=local # --------------------------------------------------------- def test_local_setup_module(ansible_adhoc): """ Tests the 'setup' module on 'localhost' using the ansible_adhoc fixture. Requires an Ansible inventory file (e.g., hosts.ini) in the current working directory or specified via `--ansible-inventory` when running pytest. """ # Run the 'setup' module on 'localhost' contacted = ansible_adhoc("setup", host_pattern="localhost") # Assert that 'localhost' was contacted and 'changed' is not in its result assert 'localhost' in contacted assert 'changed' not in contacted['localhost'] assert 'ansible_facts' in contacted['localhost'] def test_ansible_ping_module(ansible_adhoc): """ Tests the 'ping' module on 'localhost'. """ contacted = ansible_adhoc("ping", host_pattern="localhost") assert 'localhost' in contacted assert contacted['localhost']['ping'] == 'pong' # To run this example: # 1. Save the above content as 'test_ansible_module.py'. # 2. Create a file named 'hosts.ini' in the same directory with the content: # [local] # localhost ansible_connection=local # 3. Execute pytest from your terminal: `pytest --ansible-inventory=hosts.ini test_ansible_module.py`
Debug
Known issues
breakingVersion 2.0.0 introduced major changes to allow Ansible-style inventory indexing and improved results processing, shifting from dictionaries to Python objects. Tests written for older versions might require updates to handle these changes in result structures.
fix
Review and update test assertions to accommodate the new Python object-based result structures and Ansible-style inventory indexing introduced in 2.0.0 and later versions.
affects: <2.0.0
deprecatedVersion 1.4.0 started raising `DeprecationWarnings` for `scope=class` fixtures. While they might still function, it's recommended to migrate away from them for future compatibility.
fix
Refactor fixtures from `scope=class` to a more appropriate scope (e.g., `module` or `function`) or use alternative patterns for resource setup/teardown within your test suite.
affects: >=1.4.0
gotchaThe `pytest-molecule` plugin's functionality has been merged into `pytest-ansible`. Users of `pytest-molecule` should migrate to `pytest-ansible` for ongoing support and future updates.
fix
Uninstall `pytest-molecule` and install `pytest-ansible`. Update test configurations to leverage the integrated Molecule scenario testing within `pytest-ansible`.
affects: All versions of `pytest-molecule`
gotcha`pytest-ansible` only guarantees support for actively maintained versions of Python (>=3.10) and `ansible-core` (>=2.14). Using older versions may lead to unexpected behavior or lack of support.
fix
Ensure your environment uses Python 3.10 or newer and `ansible-core` 2.14 or newer to maintain compatibility and receive full support.
affects: <3.10 (Python), <2.14 (ansible-core)
gotchaThere are reported conflicts when `pytest-ansible` is used alongside `testinfra`. This can lead to unexpected test failures or behavior.
fix
If encountering issues, investigate specific interactions between the two plugins. Consider isolating tests that use `pytest-ansible` from those that use `testinfra` or consult the project's issue tracker for potential workarounds or fixes.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'ansible_collections'
This error typically occurs during unit testing of Ansible collections with `pytest-ansible` when the collection's directory structure is incorrect, the `galaxy.yml` file has an inaccurate namespace or name, or `pytest` is not run from the collection's root directory.
fix
Ensure that your `galaxy.yml` file contains the correct `namespace` and `name` for your collection. Run `pytest` from the collection's root directory, adjacent to the `galaxy.yml` file. If issues persist, try removing `__pycache__` and `.pyc` files.
ModuleNotFoundError: No module named 'ansible.constants'
This error occurs when the `pytest-ansible` plugin is installed in the system and conflicts with `ansible-test units` due to `pytest-ansible` unconditionally importing Ansible controller code.
fix
To resolve this when running `ansible-test units`, you need to explicitly disable the `pytest-ansible` plugin. This can often be done by adding `-p no:pytest_ansible` to the `pytest` command-line arguments.
fixture 'ansible_adhoc' not found
This is a common `pytest` error indicating that a fixture (like `ansible_adhoc`, `ansible_module`, `localhost`, or `ansible_facts` provided by `pytest-ansible`) is not being discovered. This can happen if the `pytest-ansible` plugin is not installed, if `conftest.py` (where custom fixtures might be defined) is misplaced, or if the test file itself is not discoverable by `pytest`.
fix
First, ensure `pytest-ansible` is installed (`pip install pytest-ansible`). For built-in fixtures, ensure your test file is properly recognized by `pytest` (e.g., named `test_*.py` or `*_test.py`). If defining custom fixtures, place them in a `conftest.py` file in the test directory or a parent directory, ensuring `conftest.py` is discoverable. You can run `pytest --fixtures [testpath]` to see available fixtures.
ERROR! Specified inventory, host pattern and/or --limit leaves us with no hosts to target.
This Ansible error, often encountered when using `pytest-ansible` to run tests against an inventory, means that the combination of your inventory file, the host pattern specified (e.g., `all`, `webservers`), and any `--limit` parameters resulted in an empty list of target hosts.
fix
Verify that your inventory file is correctly defined and accessible. Check the `--inventory` and `--host-pattern` arguments for `pytest` or `ansible-playbook` to ensure they match existing hosts and groups in your inventory. Also, examine any `--limit` flags for correct syntax and ensure they don't exclude all desired hosts.
Upgrade
Version history
26.8.0latest on PyPI · released Aug 12, 2026
Audit
Dependencies
pytestrequiredCore testing framework that pytest-ansible extends.
ansible-corerequiredProvides the core Ansible functionality that pytest-ansible interacts with.
Agent activity
8 hits · last 30 days
node
6
Amazon
1
Resources
pytest-ansible — pip install pytest-ansible · libregistry