Ansible debug Module: Print Variables & Messages (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
How to use the Ansible debug module (ansible.builtin.debug) to print variables, messages, and registered output. Debug playbooks with msg, var, and verbosity. Practical examples.
Ansible debug Module: Print Variables & Messages (Complete Guide)
The Ansible debug module (ansible.builtin.debug) prints variables, messages, and expressions during playbook execution. It's the primary tool for inspecting data and troubleshooting automation.
Basic Usage
Print a Message
Print a Variable
Parameters
| Parameter | Description | Example | |-----------|-------------|---------| | msg | Message to print (supports Jinja2) | "Host: {{ inventory_hostname }}" | | var | Variable name to print (no {{ }} needed) | ansible_os_family | | verbosity | Only show at this verbosity level or higher | 2 (needs -vv) |
Note: Use either msg or var, not both.
Print Registered Output
Print Complex Data Structures
Dictionaries
Lists
Conditional Debug Output
Verbosity Levels
Control when debug output appears:
Run with: ansible-playbook playbook.yml -vv
Print Facts and Magic Variables
Print Loop Items
Format Output with Filters
Debug Patterns
Assert-then-debug
Pause-style debugging
msg vs var
FAQ
What is the Ansible debug module used for?
The ansible.builtin.debug module prints messages and variable values during playbook execution. It's the primary way to inspect data, troubleshoot errors, and verify variable content in Ansible automation.
How do I print a variable in Ansible?
Use ansible.builtin.debug with var: variable_name (no curly braces) or msg: "{{ variable_name }}". The var parameter shows the raw variable; msg allows formatted output with Jinja2 expressions.
What is the difference between debug msg and var?
msg accepts a string with Jinja2 expressions ("Value: {{ my_var }}") and prints a formatted message. var takes a raw variable name (my_var) and prints both the name and value. Use msg for formatted output, var for quick inspection.
How do I debug only at higher verbosity?
Set the verbosity parameter: verbosity: 2 makes the task output only visible when running with -vv or higher. This keeps normal output clean while preserving debug info for troubleshooting.
How do I print registered command output?
Register the task result, then use debug with var: result.stdout for text output, var: result.stdout_lines for line-by-line, or var: result for the full result object including return code and stderr.
Conclusion
The Ansible debug module is essential for playbook development and troubleshooting. Use var for quick variable inspection, msg for formatted output, and verbosity to control when debug info appears. Combine with register, when, and loops for targeted debugging.
Related Articles • Ansible Troubleshooting: Debug & Fix Playbook Errors • Ansible register: Capture Task Output • Ansible assert Module: Validate Variables & Conditions
Category: troubleshooting