Table of Contents
Sometimes you need all ansible variables for a debugging purpose or just to choose some from it.
This is explanation how to make a nice dump of the variables to a file residing on the remote host. If you want to print multiple/all variables in a more convenient way by seeing them directly on the console, you might be interested in this: Ansible Debug Print All Variables
Here is an easy way to do this:
1. Create testing playbook
First we are going to create some playbook only for the dump purpose.
vim dump.yml
- hosts: all gather_facts: yes tasks: - name: Dump all variables action: template src=dumpall.j2 dest=/tmp/ansible_dump.all
If you like, you can also incorporate the task in your current existing playbook, by adding it to main.yml or some other preferred file.
2. Create the template file “dumpall.j2”
Now we need to define our dumpall.j2 template file, which will hold the structure for our variable dump.
In the current directory (the same you created the playbook) create the template file:
vim dumpall.j2
Module Variables ("vars"): -------------------------------- {{ vars | to_nice_json }} Environment Variables ("environment"): -------------------------------- {{ environment | to_nice_json }} GROUP NAMES Variables ("group_names"): -------------------------------- {{ group_names | to_nice_json }} GROUPS Variables ("groups"): -------------------------------- {{ groups | to_nice_json }} HOST Variables ("hostvars"): -------------------------------- {{ hostvars | to_nice_json }}
If you are using the dumper inside your existing playbook (not the testing one), you will need to put the template file in your template directory. Usually this directory is ($playbook_dir/templates/)
3. Execute the playbook to dump the variable information
Here is how to execute the playbook for one specific host:
ansible-playbook -i some-domain.com:22, dump.yml
This is going to execute the playbook on a host ‘some-domain.com’, connecting to ssh port 22. You can modify your ssh port if it is different.
Also don’t forget to put the comma (,) after the host name.
If you like to run the playbook for group of host, you can refer your hosts file with the -i option:
ansible-playbook -i path_to/inventory.file dump.yml
4. Checking the results
After you have run successfully the debugging playbook, you can login to your host/hosts and checkout the content of /tmp/ansible_dump.all
Here is some example output:
Module Variables ("vars"): -------------------------------- { "ansible_check_mode": false, "ansible_diff_mode": false, "ansible_facts": {}, "ansible_forks": 15, "ansible_inventory_sources": [ "some.example.com:XXXX," ], "ansible_play_batch": [ "some.example.com" ], "ansible_play_hosts": [ "some.example.com" ], "ansible_play_hosts_all": [ "some.example.com" ], "ansible_playbook_python": "/usr/bin/python2", "ansible_port": XXXX, "ansible_run_tags": [ "all" ], "ansible_skip_tags": [], "ansible_verbosity": 0, "ansible_version": { "full": "2.7.10", "major": 2, "minor": 7, "revision": 10, "string": "2.7.10" }, "environment": [], "group_names": [ "ungrouped" ], "groups": { "all": [ "some.example.com" ], "ungrouped": [ "some.example.com" ] }, "hostvars": { "some.example.com": { "ansible_check_mode": false, "ansible_diff_mode": false, "ansible_facts": {}, "ansible_forks": 15, "ansible_inventory_sources": [ "some.example.com:8022," ], "ansible_playbook_python": "/usr/bin/python2", "ansible_port": XXXX, "ansible_run_tags": [ "all" ], "ansible_skip_tags": [], "ansible_verbosity": 0, "ansible_version": { "full": "2.7.10", "major": 2, "minor": 7, "revision": 10, "string": "2.7.10" }, "group_names": [ "ungrouped" ], "groups": { "all": [ "some.example.com" ], "ungrouped": [ "some.example.com" ] }, "inventory_dir": "None", "inventory_file": "some.example.com:8022,", "inventory_hostname": "some.example.com", "inventory_hostname_short": "server", "omit": "__omit_place_holder__05394e06854e9b5483293f830f3dec2441c3707f", "playbook_dir": "/home/example/Desktop/temp/ansible" } }, "inventory_dir": "None", "inventory_file": "some.example.com:8022,", "inventory_hostname": "some.example.com", "inventory_hostname_short": "server", "omit": "__omit_place_holder__05394e06854e9b5483293f830f3dec2441c3707f", "play_hosts": [ "some.example.com" ], "playbook_dir": "/home/example/Desktop/temp/ansible", "role_names": [] } Environment Variables ("environment"): -------------------------------- [] GROUP NAMES Variables ("group_names"): -------------------------------- [ "ungrouped" ] GROUPS Variables ("groups"): -------------------------------- { "all": [ "some.example.com" ], "ungrouped": [ "some.example.com" ] } HOST Variables ("hostvars"): -------------------------------- { "some.example.com": { "ansible_check_mode": false, "ansible_diff_mode": false, "ansible_facts": {}, "ansible_forks": 15, "ansible_inventory_sources": [ "some.example.com:8022," ], "ansible_playbook_python": "/usr/bin/python2", "ansible_port": 8022, "ansible_run_tags": [ "all" ], "ansible_skip_tags": [], "ansible_verbosity": 0, "ansible_version": { "full": "2.7.10", "major": 2, "minor": 7, "revision": 10, "string": "2.7.10" }, "group_names": [ "ungrouped" ], "groups": { "all": [ "some.example.com" ], "ungrouped": [ "some.example.com" ] }, "inventory_dir": "None", "inventory_file": "some.example.com:8022,", "inventory_hostname": "some.example.com", "inventory_hostname_short": "server", "omit": "__omit_place_holder__05394e06854e9b5483293f830f3dec2441c3707f", "playbook_dir": "/home/example/Desktop/temp/ansible" } }
5. Some additional resources
I’ve written some additional information on Ansible, you could find useful.
https://github.com/f500/ansible-dumpall
FYI: this github project shows you how to list 90% of variables across all hosts. I find it more globally useful than single host commands. The README includes instructions for building a simple inventory report. It’s even more valuable to run this at the end of a playbook to see all the Facts. To also debug Task behaviour use register:
The result is missing a few items: – included YAML file variables – extra-vars – a number of the Ansible internal vars described here: Ansible Behavioural Params