{"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+.","language":"python","status":"active","last_verified":"Fri May 15","install":{"commands":["pip install pytest-ansible"],"cli":null},"imports":["import pytest # Fixtures like ansible_adhoc are made available by the plugin"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-15","installed_version":"4.1.1","pypi_latest":"26.4.0","is_stale":true,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":5.2,"avg_import_s":null,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"71.2M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":6.3,"import_time_s":null,"mem_mb":null,"disk_size":"72M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"81.1M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":6,"import_time_s":null,"mem_mb":null,"disk_size":"82M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"71.6M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":5.6,"import_time_s":null,"mem_mb":null,"disk_size":"72M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"71.6M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":5.5,"import_time_s":null,"mem_mb":null,"disk_size":"72M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"21.5M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"pytest-ansible","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.4,"import_time_s":null,"mem_mb":null,"disk_size":"22M"}]}}