An Ansible role to obtain and manage OAuth2 API tokens for Ansible Automation Platform (AAP).
This role automates the process of authenticating with Ansible Automation Platform and obtaining an OAuth2 token for API operations. It intelligently detects the AAP Controller version and uses the appropriate Ansible collection (ansible.controller for legacy versions or ansible.platform for newer versions) to handle token management.
- Automatic version detection – Probes the AAP API to determine whether legacy or modern API paths are needed
- Multi-platform support – Works with both AAP Gateway (2.5+) and standalone Automation Controller
- Environment variable integration – Reads credentials from environment variables by default
- Token lifecycle management – Create tokens for operations and clean them up afterwards
- Secure by design – Sensitive data is protected with
no_logoptions
- Ansible >= 2.15.0
- One of the following collections:
ansible.controller– For AAP Controller versions < 4.6.0ansible.platform– For AAP Controller versions >= 4.6.0
Note on Variable Handling: This role uses role-prefixed variables internally (e.g.,
aap_api_token_aap_hostname) to avoid precedence conflicts. Users should provide variables viaextra_varsor environment variables using the standard names (e.g.,aap_hostname,aap_username). The role automatically normalizes these values (stripshttp://andhttps://prefixes) and converts them to role-specific variables internally.
| Variable | Description | Default | Required |
|---|---|---|---|
aap_hostname |
The hostname for the AAP instance (e.g., controller.example.com or https://controller.example.com). URLs are constructed as https://{{ aap_hostname }}. Priority: extra_vars > AAP_HOSTNAME > CONTROLLER_HOST > TOWER_HOST env vars. Note: http:// and https:// prefixes are automatically stripped during normalization. |
$AAP_HOSTNAME or $CONTROLLER_HOST or $TOWER_HOST |
Yes |
aap_validate_certs |
Whether to validate SSL/TLS certificates. Priority: extra_vars > AAP_VALIDATE_CERTS > CONTROLLER_VERIFY_SSL > TOWER_VERIFY_SSL env vars. |
true (or $AAP_VALIDATE_CERTS) |
No |
| Variable | Description | Default | Required |
|---|---|---|---|
aap_username |
Username for AAP authentication. Priority: extra_vars > AAP_USERNAME > CONTROLLER_USERNAME > TOWER_USERNAME env vars. |
$AAP_USERNAME or $CONTROLLER_USERNAME or $TOWER_USERNAME |
Yes (if no token) |
aap_password |
Password for AAP authentication. Priority: extra_vars > AAP_PASSWORD > CONTROLLER_PASSWORD > TOWER_PASSWORD env vars. |
$AAP_PASSWORD or $CONTROLLER_PASSWORD or $TOWER_PASSWORD |
Yes (if no token) |
aap_token |
Existing OAuth2 token (skips token creation if provided). Priority: extra_vars > AAP_TOKEN > CONTROLLER_OAUTH_TOKEN > TOWER_OAUTH_TOKEN env vars. |
$AAP_TOKEN or $CONTROLLER_OAUTH_TOKEN or $TOWER_OAUTH_TOKEN |
No |
Note: Either provide
aap_tokenOR bothaap_usernameandaap_password.
| Variable | Description | Default |
|---|---|---|
aap_api_token_no_log |
Suppress sensitive output in logs (actually used in tasks) | true |
aap_controller_full_version |
AAP Controller version (auto-detected if not set) | Auto-detected |
Obtains an OAuth2 token from AAP using username/password credentials.
Sets the following facts:
aap_token– The OAuth2 token for API operationscontroller_oauthtoken– Alias for compatibility withansible.controllercollectionaap_controller_full_version– Detected AAP Controller versionaap_controller_needs_legacy_paths– Boolean indicating if legacy API paths are required
Revokes/deletes the OAuth2 token created during the session.
Usage: Include this entry point at the end of your playbook to clean up temporary tokens.
This role requires one of the following collections based on your AAP version:
# For AAP 2.5+ (Gateway)
collections:
- ansible.platform
# For Automation Controller < 4.6.0
collections:
- ansible.controllerSet environment variables before running:
export AAP_GATEWAY_URL="https://gateway.example.com"
export AAP_USERNAME="admin"
export AAP_PASSWORD="secretpassword"---
- name: Get AAP API Token
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Obtain API token
ansible.builtin.include_role:
name: infra.support_assist.aap_api_token
- name: Use the token for API operations
ansible.builtin.debug:
msg: "Token obtained successfully. Controller version: {{ aap_controller_full_version }}"
- name: Clean up token when done
ansible.builtin.include_role:
name: infra.support_assist.aap_api_token
tasks_from: clear_token.yml---
- name: Get AAP API Token with explicit variables
hosts: localhost
connection: local
gather_facts: false
vars:
aap_hostname: "controller.example.com"
aap_username: "admin"
aap_password: "{{ vault_aap_password }}"
aap_validate_certs: true
tasks:
- name: Obtain API token
ansible.builtin.include_role:
name: infra.support_assist.aap_api_token
- name: Perform operations with token
ansible.builtin.uri:
url: "https://{{ aap_hostname }}/api/v2/job_templates/"
method: GET
headers:
Authorization: "Bearer {{ aap_token }}"
validate_certs: "{{ aap_validate_certs }}"
register: job_templates
- name: Clean up token
ansible.builtin.include_role:
name: infra.support_assist.aap_api_token
tasks_from: clear_token.yml---
- name: Get token via AAP Gateway
hosts: localhost
connection: local
gather_facts: false
vars:
aap_hostname: "gateway.example.com"
aap_username: "admin"
aap_password: "{{ vault_aap_password }}"
tasks:
- name: Obtain API token from Gateway
ansible.builtin.include_role:
name: infra.support_assist.aap_api_token
- name: Display token info
ansible.builtin.debug:
msg: |
Token created successfully!
AAP Version: {{ aap_controller_full_version }}
Uses legacy paths: {{ aap_controller_needs_legacy_paths }}┌─────────────────────────────────────────────────────────────────┐
│ aap_api_token │
├─────────────────────────────────────────────────────────────────┤
│ 1. Pre-validation │
│ └── Verify required variables are set │
│ │
│ 2. Version Detection │
│ ├── Try: /api/controller/v2/ping/ (new path) │
│ └── Fallback: /api/v2/ping/ (legacy path) │
│ │
│ 3. Token Creation │
│ ├── If version >= 4.6.0 → ansible.platform.token │
│ └── If version < 4.6.0 → ansible.controller.token │
│ │
│ 4. Set Facts │
│ └── aap_token, controller_oauthtoken, version info │
└─────────────────────────────────────────────────────────────────┘
"'aap_hostname' must be provided"
- Ensure you set one (and only one) of these variables
- Check that your environment variables are exported correctly
"Either 'aap_token' must be provided, or both 'aap_username' and 'aap_password'"
- Provide complete credentials: both username AND password
- Or provide an existing token via
aap_token
"Could not determine AAP Controller version"
- Verify the URL is correct and reachable
- Check network connectivity and firewall rules
- Ensure SSL certificates are valid (or set
aap_validate_certs: falsefor testing)
Enable verbose output for troubleshooting:
aap_api_token_no_log: false # Show sensitive data in logs (use only for debugging!)GPL-3.0-or-later
- Author: Lenny Shirley
- Company: Red Hat
- Collection: infra.support_assist
This role is typically used in conjunction with other roles in the infra.support_assist collection:
aap_api_gather– Gather diagnostic output from AAP component APIs (Controller, Hub, Gateway, EDA)rh_case– Create and update Red Hat support cases (unified role)rh_token_refresh– Handle Red Hat API token authentication and cachingocp_must_gather– Gather OpenShift diagnosticssos_report– Generate SOS reports