Ansible Debug: Print Variable & List All Variables – Playbook

During Ansible playbook debugging it is useful to know how to display host facts or registered variables.

To print a message from Ansible playbook, as well as a value of a variable, we can use Ansible debug module.

In the examples below i am showing how to print particular Ansible variables and how to list all known facts and variables from Ansible playbook.

Debug Ansible Playbook by Printing Variables

Print a variable:

- name: "Ansible | Print a variable"
    msg: "The operating system is {{ ansible_distribution }}"

Display a variable only if it is defined:

- name: "Ansible | Print a variable if defined"
    msg: "The 'foo' variable value is {{ foo }}"
  when: foo is defined

- name: "Ansible | Print a message if a variable is undefined"
    msg: "The 'bar' variable is undefined"
  when: bar is not defined

Print multiple variable:

- name: "Ansible | Print multiple variable"
    msg: |
      The role is {{ host_role }}
      The environment is {{ host_environment }}

List all known variables and facts:

- name: "Ansible | List all known variables and facts"
    var: hostvars[inventory_hostname]
Recommended Book

Ansible is a simple, but powerful, server and configuration management tool (with a few other tricks up its sleeve).

This book will help those familiar with the command line and basic shell scripting start using Ansible to provision and manage anywhere from one to thousands of servers.