Ansible Pilot

Print text or variable during execution - Ansible module debug

Some real-life examples from print “Hello world!”, print a text, print a variable, print a combination of text and variable and set the verbosity level.

August 3, 2021
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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 demo 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

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 demo

Let’s jump in live Ansible debug module demo.

In the following example we are going beyond the print of “Hello world!” text, printing a text, a variable, text and variable, and demonstrate to you how to use the verbosity level. Are you ready?

---
- name: debug module demo
  hosts: all
  vars:
    fruit: "apple"
  tasks:
    - name: debug message
      ansible.builtin.debug:
        msg: "our fruit is {{ fruit }}"
        verbosity: 2

code with ❤️ in GitHub

Recap

Now you know how to print text/variable during execution with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 200+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Trustpilot
Follow me

Subscribe not to miss any new releases

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress