Ansible Debug Print All Variables

Published on Author gryzliLeave a comment

I have written a post about how to debug playbooks by dumping all variables in file (on remote server) by using Ansible template file. Here is some faster and more convenient way to print multiple variables or all with debug purpose inside a playbook.

Here are some handy commands for quick dumping of a given variable, multiple variables, or all variables. 

Dump all variables/facts for a given host (without invoking a playbook)

1) Based on inventory file 

# Dump facts for host "some_host" which is defined inside inventory_file.txt
ansible -i inventory_file.txt some_host -m setup 

2) Without inventory file

Here is how you can dump the facts, without even having an inventory file: 

(Don’t forget the “,” (comma) in the end of the hostname/ip)

# Dump facts for ip 1.1.1.1
ansible -i1.1.1.1,  some_host -m setup 

# Dump facts for domain example.com
ansible -iexample.com,  some_host -m setup 

 

Printing multiple Ansible variables with debug purpose (inside a playbook)

First you need to define your debug task , which I called debug_info in my case:

I have used some nice technique (I found out there) for printing multi-line message inside debug statement:

- name: Print some debug information 
  vars: 
    msg: |
        Ansible Distribution: {{ ansible_distribution }}
        Ansible Dist version: {{ ansible_distribution_version }}
  debug: 
    msg: "{{ msg.split('\n') }}"       
  tags: debug_info

 

In order to get only the debug information (without executing any other tasks inside the playbook), you could limit the task executing by providing “–tags ‘debug_info’ ” to ansible-playbook command.

So after executing command:

ansible-playbook --tags "debug_info" -i hosts/mvps.host test.yml

I’m getting something like this:

Output from running ansible debug_module for printing variables
Ansible multiline variable debug

 

Printing all Ansible variables with debug purpose (inside a playbook)

Now if we want to print all internal variables, we could use the following yaml:

---
- name: dump all
  hosts: all
 
  tasks:
    - name: Print some debug information 
      vars: 
        msg: |
          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 }} 
    
      debug: 
        msg: "{{ msg.split('\n') }}"       
      tags: debug_info

 

Another good way is to use something like that:

- name: Display all variables/facts known for a host
  debug:
    var: hostvars[inventory_hostname]
  tags: debug_info

 

Executing this task is going to dump all your variables.

Cheers !

 

If you are interested in Ansible performance tunning as well, take a look here: 

Tuning Ansible For Maximum Performance

Leave a Reply

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