Ansible Print Variable: debug Module for Messages & Troubleshooting — Video Tutorial
How to use Ansible's debug module to print variables, messages, and troubleshoot playbooks. Includes msg, var, verbosity levels, and practical debugging examples.
Watch Video
Watch "Ansible Print Variable: debug Module for Messages & Troubleshooting" on YouTube
What You'll Learn
- How to print a text or a variable during the execution with Ansible?
- Ansible Print text/variable during execution
- Parameters
- Ansible debug module Playbook
- 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
Full Tutorial Content
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?
```yaml
---
- name: debug module Playbook
hosts: all
vars:
fruit: "apple"
tasks:
- name: debug message
ansible.builtin.debug:
msg: "our fruit is {{ fruit }}"
verbosity: 2
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/print%20text%20variable%20during%20execution)
Debug Module Usage Patterns
Print a simple message
```yaml
- name: Show deployment info
ansible.builtin.debug:
msg: "Deploying version {{ app_version }} to {{ inventory_hostname }}"
```
Print a variable
```yaml
- name: Show all ansible facts
ansible.builtin.debug:
var: ansible_facts
- name: Show specific fact
ansible.builtin.debug:
var: ansible_distribution
```
Print registered variable from command output
```yaml
- name: Check disk space
ansible.builtin.command: df -h /
register: disk_result
changed_when: false
- name: Show disk space
ansible.builtin.debug:
var: disk_result.stdout_lines
```
Conditional debug (only show on verbose)
```yaml
- name: Detailed debug info (only with -v)
ansible.builtin.debug:
msg: "Connection details: {{ hostvars[inventory_hostname] }}"
verbosity: 1 # Only shows with ansible-playbook -v
- name: Very detailed debug (only with -vv)
ansible.builtin.debug:
msg: "Full variable dump: {{ vars }}"
verbosity: 2
```
Print multiple variable
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 4 min
- Category: troubleshooting
Read the full written article: Ansible Print Variable: debug Module for Messages & Troubleshooting
Topics Covered
Related Video Tutorials
- Ansible Multiline Strings: YAML Literal & Folded Block Scalars — How to write multiline strings in Ansible YAML. Use literal (|), folded (>), chomp indicators, and Jinja2 multiline for long text and commands.
- Ansible ansible.builtin vs ansible.legacy: Collection Namespaces Explained — Understand ansible.builtin vs ansible.legacy namespaces. Learn when to use FQCN, how module resolution works, and migrate to fully qualified collection names.
- Ansible blockinfile Module: Insert & Manage Text Blocks in Files — How to manage text blocks in files with Ansible blockinfile module. Insert, update, remove marked blocks in config files with idempotent operations.
- Ansible lineinfile Module: Edit Single Lines in Config Files — How to edit single lines in files with Ansible lineinfile module. Add, modify, remove lines using regex, manage config files idempotently with examples.
- Ansible Write to File: Variable Content with copy & template Modules — How to write variables to files in Ansible. Compare copy content vs template module, write JSON/YAML, and generate dynamic config files with examples.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.