AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

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 ArticlesAnsible Troubleshooting: Debug & Fix Playbook ErrorsAnsible register: Capture Task OutputAnsible assert Module: Validate Variables & Conditions

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home