Home Ansible How To Use Debug Module In Ansible Playbooks

How To Use Debug Module In Ansible Playbooks

By Karthick
Published: Last Updated on 3.3K views

In this guide, we will discuss what is Ansible debug module, what are the supported parameters in debug module and finally how to use the debug module with each parameter in Ansible playbooks with examples in Linux.

What Is Ansible Debug Module?

Ansible debug module is used to print expressions and variables when running the playbook. By default, when you run the playbook you will not get output values instead you will get the status (E.g. changed, ignored, etc..). Using the debug module, you can print any variables or expressions. Think of this as your print statement in any programming language.

Debug module supports three parameters as listed below:

  1. msg
  2. var
  3. Verbosity

1. Debug Module With Msg Parameter

Below is the skeleton for the play which will be used throughout this guide. Replace the hosts according to your environment.

- name: Working With debug module
  hosts: m2
  gather_facts: false

I have created two tasks. In both, the task debug module accepts arguments through the msg parameter. The only difference between both tasks is that in the first task msg parameter is present in the same line with the module name debug. In the second, task module name and msg parameter is in a separate line and intended.

tasks:

    # Module and argument in the same line
    - name: First debug
      debug: msg="Hello OSTechnix Users"

    # Module and argument in separate line
    - name: Second debug
      debug: 
        msg: "You are looking at debug module guide"

You can run the syntax check or run the playbook using the following commands.

$ ansible-playbook --syntax-check .yml
$ ansible-playbook .yml

Both the tasks print the message to the console while running the playbook which can be verified from the following tasks outputs.

TASK [First debug] ***********************************************************************************************************************************
ok: [rocky.anslab.com] => {
  "msg": "Hello OSTechnix Users"
}
​
TASK [Second debug] **********************************************************************************************************************************
ok: [rocky.anslab.com] => {
  "msg": "You are looking at debug module guide"
}

You can also use List and dictionary values to print as message. The syntax for list of values and dictionary mapping will be as follows.

# List Line debug messages
    - name: List debug messages
      debug:
        msg:
          - India
          - Australia
          - Japan

    # Dictionary debug messages
    - name: Dict debug messages
      debug:
        msg:
          Country: India
          State: TN

Take a look at the output below. You can see the output is printed with python syntactical representation for list and dictionary.

TASK [List debug messages] ***************************************************************************************************************************
ok: [rocky.anslab.com] => {
  "msg": [
      "India",
      "Australia",
      "Japan"
  ]
}
​
TASK [Dict debug messages] ***************************************************************************************************************************
ok: [rocky.anslab.com] => {
  "msg": {
      "Country": "India",
      "State": "TN"
  }
}

You can also print variable values using the msg parameter. If you look at the task below, I have created two variables named UBUNTU_VERSION and RELEASE. I am printing both the variable values using the msg parameter.

    - name: Printing variable 
      vars:
        UBUNTU_VERSION: 22.04
        RELEASE: APRIL 2022
      debug:
        # msg: {{ UBUNTU_VERSION }} Latest version of Ubuntu is {{ UBUNTU_VERSION }} and it is releasing on {{ RELEASE }}
        msg: "{{ UBUNTU_VERSION }}"

Output:

TASK [Printing variable] *****************************************************************************************************************************
ok: [rocky.anslab.com] => {
    "msg": "Latest version of Ubuntu is 22.04 and it is releasing on APRIL 2022"
}

An important point to note here is, when your variable name comes as the first word, then you should enclose the entire line with double quotes. It is always recommended to use double quotes when working with msg parameter.

msg: {{ UBUNTU_VERSION }} is releasing on {{ RELEASE }} # WILL FAIL
msg: "{{ UBUNTU_VERSION }} is releasing on {{ RELEASE }}" # ENCLOSED IN QUOTES

2. Debug Module With Var Parameter

If you wish to print the variable alone without any strings like we did in the last section, then you can use the var parameter and pass the variable name as argument.

When using the msg module, you should use double curly braces to print the variable value. But using the var parameter, you can just pass the variable name without any braces or quotes.

    - name: Printing variable
      vars:
        UBUNTU_VERSION: 22.04
      debug:
        var: UBUNTU_VERSION

Output:

TASK [Using Var] *************************************************************************************************************************************
ok: [rocky.anslab.com] => {
   "UBUNTU_VERSION": 22.04
}

3. Debug Module With Verbosity Parameter

Ansible supports four level of verbosity. By default verbosity is set to zero. To print the messages you have to increase the verbosity using -v flag.

$ ansible all -m shell -a "uptime" -v # LEVEL 1
$ ansible all -m shell -a "uptime" -vv # LEVEL 2
$ ansible all -m shell -a "uptime" -vvv # LEVEL 3
$ ansible all -m shell -a "uptime" -vvvv # LEVEL 4

When you run the playbook you can set the verbosity level for a task using the debug module. In this case you will not see the debug message output in the terminal unless you use -v flag when triggering the playbook.

    - name: Printing variable
      vars:
        UBUNTU_VERSION: 22.04
        RELEASE: APRIL 2022
      debug:
        var: UBUNTU_VERSION
      verbosity: 2

In above task I have set the verbosity level to two. Take a look at below output, you can see the output is printed as "skipping" for this task.

$ ansible-playbook <playbook.yml>
​
TASK [Printing variable] *****************************************************************************************************************************
skipping: [rocky.anslab.com]

You can pass -v flag to your playbook. It will tell the skipped reason.

$ ansible-playbook <playbook.yml> -v
​
TASK [Printing variable] *****************************************************************************************************************************
skipping: [rocky.anslab.com] => {"skipped_reason": "Verbosity threshold not met."}

Since I have set the verbosity to two, I have to use the -vv flag to print the messages. Similarly according to the verbosity level you set in the task, you should use the corresponding level of -v flag when running the playbook.

$ ansible-playbook <playbook.yml> -vv
​
TASK [Printing variable] *****************************************************************************************************************************
task path: /home/vagrant/ansible_lab/vagrant_lab_sync/playbooks/2_debug_module/debug_with_verbosity.yml:8
ok: [rocky.anslab.com] => {
   "msg": "Latest version of Ubuntu is 22.04 and it is releasing on APRIL 2022"
}
META: ran handlers
META: ran handlers

Conclusion

In this guide, we have gone through what is Ansible debug module, how does it work and how to use debug module in Ansible with respective examples.

You May Also Like

2 comments

Umata June 2, 2022 - 4:44 pm

Hey Man!
Can you tell me what the vars.yml file looks like, from the ansible-playbook 4_var_precedence.yml example -e @vars.yml

Reply
karthick June 4, 2022 - 2:19 am

UMATA – I have nowhere used the example you asked for in this article. Are you referring to ansible variables article ?

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More