Ansible Debug Module: Print Variables & Messages During Playbook Execution
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
How to print variables and debug messages during Ansible playbook execution with the debug module. Use msg, var, and verbosity parameters.

How to print a text or a variable during the execution with Ansible?
ansible.builtin.debug prints messages and variable values to stdout during playbook execution — the primary way to inspect registered task output, Ansible facts, and Jinja2 expressions without halting the play. Set the verbosity parameter to suppress debug output in normal runs and show it only when -v or higher is passed.
See also: Ansible debug Module: Print Variables & Debug Messages (Complete Guide)
Ansible debug module — ansible.builtin.debug
Today we’re talking about Ansible module debug. It’s part of theansible.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_
See also: Ansible Multi-Line Strings: Literal (|) & Folded (>) Block Scalars Guide
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?
---
- name: debug module Playbook
hosts: all
vars:
fruit: "apple"
tasks:
- name: debug message
ansible.builtin.debug:
msg: "our fruit is {{ fruit }}"
verbosity: 2
Debug Module Usage Patterns
Print a simple message
- name: Show deployment info
ansible.builtin.debug:
msg: "Deploying version {{ app_version }} to {{ inventory_hostname }}"Print a variable
- name: Show all ansible facts
ansible.builtin.debug:
var: ansible_facts
- name: Show specific fact
ansible.builtin.debug:
var: ansible_distributionPrint registered variable from command output
- 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_linesConditional debug (only show on verbose)
- 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: 2Print multiple variables
- name: Show deployment summary
ansible.builtin.debug:
msg:
- "Host: {{ inventory_hostname }}"
- "OS: {{ ansible_distribution }} {{ ansible_distribution_version }}"
- "IP: {{ ansible_default_ipv4.address }}"
- "RAM: {{ ansible_memtotal_mb }}MB"
- "CPUs: {{ ansible_processor_vcpus }}"Debug loop results
- name: Check service status
ansible.builtin.service_facts:
- name: Show status of key services
ansible.builtin.debug:
msg: "{{ item }}: {{ ansible_facts.services[item].state | default('not found') }}"
loop:
- nginx.service
- postgresql.service
- redis.serviceSee also: Ansible blockinfile Module: Insert & Manage Multi-Line Text Blocks (Guide)
msg vs var
| Parameter | Accepts Jinja2 | Shows |
|---|---|---|
msg | Yes ({{ }}) | Formatted message string |
var | No (bare name) | Full variable content with type info |
# These are equivalent:
- debug:
msg: "{{ my_list }}"
- debug:
var: my_list
# But var shows more detail (type, structure)Debugging Strategies
Strategy 1: Add debug tasks temporarily
- name: DEBUG - show variable before use
ansible.builtin.debug:
var: database_config
tags: [debug] # Run only with --tags debugStrategy 2: Use ansible -m debug ad-hoc
# Check a variable without running full playbook
ansible server1 -m debug -a "var=hostvars[inventory_hostname]" -i inventoryStrategy 3: Use callback plugins
# Get detailed task timing
ANSIBLE_CALLBACKS_ENABLED=timer,profile_tasks ansible-playbook playbook.ymlFAQ
How do I pretty-print JSON in debug?
- name: Pretty print JSON
ansible.builtin.debug:
msg: "{{ my_dict | to_nice_json }}"Can I write debug output to a file?
Use copy module instead:
- name: Save debug to file
ansible.builtin.copy:
content: "{{ my_var | to_nice_yaml }}"
dest: /tmp/debug_output.yml
delegate_to: localhostHow do I suppress debug output in production?
Use the verbosity parameter or tags:
- debug:
msg: "{{ sensitive_data }}"
verbosity: 3 # Only shows with -vvvConclusion
Now you know how to print text/variable during execution with Ansible.Related Articles
Category: troubleshooting
Watch the video: Ansible Debug Module: Print Variables & Messages During Playbook Execution — Video Tutorial