Created
January 13, 2025 15:26
-
-
Save fischerdr/39c31fd0b16186039305f514ae312d98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import json | |
| import sys | |
| import logging | |
| import click | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(levelname)s - %(message)s' | |
| ) | |
| def get_inventory_extravars(tower_url, token, inventory_name): | |
| """ | |
| Connect to Ansible Tower and retrieve extra_vars of a specific inventory. | |
| :param tower_url: URL of the Ansible Tower instance | |
| :param token: Ansible Tower authentication token | |
| :param inventory_name: Name of the inventory to look up | |
| :return: Extra variables as a dictionary | |
| """ | |
| headers = { | |
| "Authorization": f"Bearer {token}", | |
| "Content-Type": "application/json" | |
| } | |
| try: | |
| # Fetch the list of inventories | |
| response = requests.get(f"{tower_url}/api/v2/inventories/", headers=headers) | |
| response.raise_for_status() | |
| inventories = response.json() | |
| # Find the inventory by name | |
| inventory = next((inv for inv in inventories["results"] if inv["name"] == inventory_name), None) | |
| if not inventory: | |
| logging.error(f"Inventory '{inventory_name}' not found.") | |
| sys.exit(1) | |
| # Fetch details of the inventory | |
| inventory_id = inventory["id"] | |
| response = requests.get(f"{tower_url}/api/v2/inventories/{inventory_id}/", headers=headers) | |
| response.raise_for_status() | |
| inventory_details = response.json() | |
| # Extract and return extra_vars | |
| extra_vars = inventory_details.get("variables", "") | |
| if extra_vars: | |
| return json.loads(extra_vars) | |
| else: | |
| logging.warning("No extra_vars found for the inventory.") | |
| return {} | |
| except requests.exceptions.RequestException as e: | |
| logging.error(f"Failed to connect to Ansible Tower: {e}") | |
| sys.exit(1) | |
| except json.JSONDecodeError: | |
| logging.error("Failed to parse extra_vars as JSON.") | |
| sys.exit(1) | |
| @click.command() | |
| @click.option('--tower-url', required=True, help='URL of the Ansible Tower instance') | |
| @click.option('--token', required=True, help='Ansible Tower authentication token') | |
| @click.option('--inventory-name', required=True, help='Name of the inventory to look up') | |
| def main(tower_url, token, inventory_name): | |
| """ | |
| CLI to retrieve extra_vars of a specific inventory from Ansible Tower. | |
| """ | |
| extra_vars = get_inventory_extravars(tower_url, token, inventory_name) | |
| logging.info(f"Extra variables for inventory '{inventory_name}':") | |
| print(json.dumps(extra_vars, indent=4)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment