{"id":5030,"library":"pytest-ansible","title":"pytest-ansible","description":"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+.","status":"active","version":"26.4.0","language":"en","source_language":"en","source_url":"https://github.com/ansible/pytest-ansible","tags":["pytest","ansible","testing","fixtures","automation","molecule"],"install":[{"cmd":"pip install pytest-ansible","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core testing framework that pytest-ansible extends.","package":"pytest","optional":false},{"reason":"Provides the core Ansible functionality that pytest-ansible interacts with.","package":"ansible-core","optional":false}],"imports":[{"note":"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.","symbol":"ansible_adhoc","correct":"import pytest # Fixtures like ansible_adhoc are made available by the plugin"}],"quickstart":{"code":"import pytest\nimport os\n\n# --- Required Setup: inventory file (e.g., hosts.ini) ---\n# [local]\n# localhost ansible_connection=local\n# ---------------------------------------------------------\n\ndef test_local_setup_module(ansible_adhoc):\n    \"\"\"\n    Tests the 'setup' module on 'localhost' using the ansible_adhoc fixture.\n    Requires an Ansible inventory file (e.g., hosts.ini) in the current\n    working directory or specified via `--ansible-inventory` when running pytest.\n    \"\"\"\n    # Run the 'setup' module on 'localhost'\n    contacted = ansible_adhoc(\"setup\", host_pattern=\"localhost\")\n\n    # Assert that 'localhost' was contacted and 'changed' is not in its result\n    assert 'localhost' in contacted\n    assert 'changed' not in contacted['localhost']\n    assert 'ansible_facts' in contacted['localhost']\n\ndef test_ansible_ping_module(ansible_adhoc):\n    \"\"\"\n    Tests the 'ping' module on 'localhost'.\n    \"\"\"\n    contacted = ansible_adhoc(\"ping\", host_pattern=\"localhost\")\n    assert 'localhost' in contacted\n    assert contacted['localhost']['ping'] == 'pong'\n\n# To run this example:\n# 1. Save the above content as 'test_ansible_module.py'.\n# 2. Create a file named 'hosts.ini' in the same directory with the content:\n#    [local]\n#    localhost ansible_connection=local\n# 3. Execute pytest from your terminal: `pytest --ansible-inventory=hosts.ini test_ansible_module.py`","lang":"python","description":"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."},"warnings":[{"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.","message":"Version 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.","severity":"breaking","affected_versions":"<2.0.0"},{"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.","message":"Version 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.","severity":"deprecated","affected_versions":">=1.4.0"},{"fix":"Uninstall `pytest-molecule` and install `pytest-ansible`. Update test configurations to leverage the integrated Molecule scenario testing within `pytest-ansible`.","message":"The `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.","severity":"gotcha","affected_versions":"All versions of `pytest-molecule`"},{"fix":"Ensure your environment uses Python 3.10 or newer and `ansible-core` 2.14 or newer to maintain compatibility and receive full support.","message":"`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.","severity":"gotcha","affected_versions":"<3.10 (Python), <2.14 (ansible-core)"},{"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.","message":"There are reported conflicts when `pytest-ansible` is used alongside `testinfra`. This can lead to unexpected test failures or behavior.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}