Ansible Print Variable: debug Module for Messages & Troubleshooting
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
How to use Ansible's debug module to print variables, messages, and troubleshoot playbooks. Includes msg, var, verbosity levels, and practical debugging examples.
How to print a text or a variable during the execution with Ansible? I’m going to show you step-by-step in a live Playbook with some example code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot
Ansible Print text/variable during execution Today we’re talking about Ansible module debug. It’s part of the ansible.builtin collection so it’s part of the collection of modules “builtin” with ansible and shipped with it. It’s a stable module and works with a variety of operating systems. The purpose is to print statements during execution. This means not only text but also all the possible Ansible variables and facts
Parameters • msg _string_ • var _string_ • verbosity _integer_
This module has three parameters. If you launch without any parameter the default “Hello world!” (with exclamation mark) is printed. If we prefer to customize the message we need to specify the “msg” parameter. In the same way, the “var” parameter allows us to print a variable. We could combine text and variables in the “msg” field. Please note that you need to use always the double brackets when we want the variable value. The "verbosity" is for advanced users if you would like to hide our debug code in normal execution but keep it in the playbook if we need it in debug mode. The value could vary from 0 normal execution to 3.
Ansible debug module Playbook Let’s jump in live Ansible debug module Playbook.
In the following example we are going beyond the print of “Hello world!” text, printing a text, a variable, text and variable, and Playbooknstrate to you how to use the verbosity level. Are you ready?
Debug Module Usage Patterns
Print a simple message
Print a variable
Print registered variable from command output
Conditional debug (only show on verbose)
Print multiple variables
Debug loop results
msg vs var
| Parameter | Accepts Jinja2 | Shows | |-----------|----------------|-------| | msg | Yes ({{ }}) | Formatted message string | | var | No (bare name) | Full variable content with type info |
Debugging Strategies
Strategy 1: Add debug tasks temporarily
Strategy 2: Use ansible -m debug ad-hoc
Strategy 3: Use callback plugins
FAQ
How do I pretty-print JSON in debug?
Can I write debug output to a file?
Use copy module instead:
How do I suppress debug output in production?
Use the verbosity parameter or tags:
Conclusion Now you know how to print text/variable during execution with Ansible.
Category: troubleshooting
Watch the video: Ansible Print Variable: debug Module for Messages & Troubleshooting — Video Tutorial