In Ansible, obtaining the list of all available variables can be achieved through several methods, depending on the environment or context in which you wish to understand these variables. Here are some common methods to obtain the list of Ansible variables: ### 1. Using the setup module
Ansible's setup module gathers detailed information about remote hosts. When executed, it returns all currently available variables along with their detailed information, including automatically discovered variables and facts.
Example:
yaml- name: Collect all facts and variables hosts: all tasks: - name: Gather facts setup: - name: Print all gathered facts and variables debug: var: hostvars[inventory_hostname]
In this example, the setup module first gathers all facts, and the debug module is used to print all variables for the current host.
2. Using the debug module and the vars keyword
You can directly use the debug module with the vars keyword to output all variables within the current task scope.
Example:
yaml- name: Display all variables/facts known for a host hosts: all tasks: - name: List all known variables and facts debug: var: vars
This will output all variables within the scope of the current playbook.
3. Writing scripts using the Ansible API
If you need to handle or analyze these variables more deeply and automatically, you can use the Ansible API. By writing Python scripts, you gain precise control over the process. Example Python script:
pythonfrom ansible.parsing.dataloader import DataLoader from ansible.inventory.manager import InventoryManager from ansible.vars.manager import VariableManager loader = DataLoader() inventory = InventoryManager(loader=loader, sources='your_inventory_file.ini') variable_manager = VariableManager(loader=loader, inventory=inventory) host = 'your_host' host_vars = variable_manager.get_vars(host=inventory.get_host(host)) print(host_vars)
This script loads the specified inventory file and prints all variables for the specified host.
Notes
When using these methods to view variables, ensure you consider security, especially when dealing with sensitive data. Different Ansible versions may have subtle differences in certain features; remember to check the specific documentation for your version.