Ansible Dump All Variables In A File

Published on Author gryzli1 Comment

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

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

 

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: 

 

5. Some additional resources

I’ve written some additional information on Ansible, you could find useful. 

My Ansible CheatSheet

Print All Ansible Variables During Playbook Execution

Optimizing Ansible For Best Performance

One Response to Ansible Dump All Variables In A File

  1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.