Ansible Tower CLI

3.3.9 · active · verified Thu Apr 16

Ansible Tower CLI (tower-cli) is a command-line interface tool for interacting with Ansible Tower and AWX instances. It allows users to manage resources like job templates, inventories, and projects directly from the terminal, and also provides a programmatic interface for Python scripts. The library is currently at version 3.3.9 and receives regular updates, typically with several minor releases focusing on bug fixes and new features throughout the year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `ansible-tower-cli` programmatically within a Python script. It relies on environment variables (`TOWER_HOST`, `TOWER_TOKEN`, `TOWER_VERIFY_SSL`) for configuration, which is the recommended approach for secure and flexible scripting. It then fetches and prints the first five job templates from the configured Tower/AWX instance. Ensure you have your instance details and API token set as environment variables before running.

import os
import tower_cli

# Configure tower_cli using environment variables
# Recommended: set TOWER_HOST and TOWER_TOKEN for secure authentication.
# Example: export TOWER_HOST='https://your-awx-instance.com'
#          export TOWER_TOKEN='your_api_token'
#          export TOWER_VERIFY_SSL='False' # if using self-signed certs

host = os.environ.get('TOWER_HOST', 'https://localhost')
token = os.environ.get('TOWER_TOKEN', '')
verify_ssl_str = os.environ.get('TOWER_VERIFY_SSL', 'True').lower()
verify_ssl = verify_ssl_str == 'true' or verify_ssl_str == '1'

if not token:
    print("WARNING: TOWER_TOKEN environment variable not set. This example will likely fail.")
    print("Please set TOWER_HOST and TOWER_TOKEN (and optionally TOWER_VERIFY_SSL) environment variables.")

# Programmatically set configuration, this will override environment variables if set
# This also persists to a config file by default, which can be useful but might be unintended.
# For a single script run, relying solely on env vars is often preferred.
# If you need to set explicitly in code:
# tower_cli.get_resource('settings').modify(
#     TOWER_HOST=host,
#     TOWER_TOKEN=token,
#     TOWER_VERIFY_SSL=verify_ssl
# )

print(f"Attempting to connect to Tower/AWX at: {host}")
print("Listing the first 5 job templates...")

try:
    # Using tower_cli.get_resource() to interact with the API
    # Configuration will be picked up from environment variables.
    job_templates_resource = tower_cli.get_resource('job_templates')
    job_templates = job_templates_resource.list(page_size=5)

    if job_templates:
        print("Successfully retrieved job templates:")
        for jt in job_templates:
            print(f"- ID: {jt['id']}, Name: {jt['name']}")
    else:
        print("No job templates found or access denied. Check your permissions and connection.")

except tower_cli.exceptions.TowerCLIError as e:
    print(f"An error occurred while interacting with Tower/AWX: {e}")
    print("Please ensure your TOWER_HOST, TOWER_TOKEN, and TOWER_VERIFY_SSL environment variables are correctly set and accessible.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →