Ansible Compat

26.3.0 · active · verified Thu Apr 09

Ansible Compat is a Python library providing essential compatibility layers and utilities for Ansible projects, primarily benefiting Ansible collection developers. It helps in managing forward and backward compatibility across different Ansible core versions by abstracting away API differences and simplifying interactions with Ansible's runtime. The current version is 26.3.0, and releases are typically aligned with major Ansible core updates or significant feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `AnsibleCompatConfig` and `AnsibleRuntime` to access Ansible's configuration and runtime environment. It shows how to query the Ansible version, configuration file path, collection paths, and find a specific collection. It also includes an example of using the `AnsiblePath` primitive.

from ansible_compat.config import AnsibleCompatConfig
from ansible_compat.runtime import AnsibleRuntime
from ansible_compat.primitives import AnsiblePath
import os

# Initialize configuration (can be empty for defaults, or load from ansible.cfg)
config = AnsibleCompatConfig()

# Initialize runtime, which discovers Ansible paths and settings
runtime = AnsibleRuntime()

print(f"Ansible core version: {runtime.ansible_version}")
print(f"Ansible config file: {runtime.config_file or 'Not found'}")
print(f"Ansible collection paths: {runtime.ansible_collection_paths}")

# Example: Get path for a specific collection
collection_name = os.environ.get('ANSIBLE_TEST_COLLECTION', 'community.general')
collection_path = runtime.get_collection_path(collection_name)
if collection_path:
    print(f"Collection '{collection_name}' found at: {collection_path}")
else:
    print(f"Collection '{collection_name}' not found. Try setting it via ANSIBLE_TEST_COLLECTION.")

# Example: Using an AnsiblePath primitive
some_path = AnsiblePath("my_ansible_artifact.yml")
print(f"AnsiblePath object for 'my_ansible_artifact.yml': {some_path}")

view raw JSON →