Skip to content
Menu
Mujahidtricks
  • Home
  • CONTACT US
  • ABOUT US
  • Terms and Conditions
  • Disclaimer
  • COOKIES POLICY
  • Privacy Policy
Mujahidtricks

Using Debug Module in Ansible Playbooks

Posted on April 28, 2022 by admin

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

If you haven’t set up Ansible yet, see the following guides.

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 any output values, but the status (eg modified, 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 mentioned below:

  1. message
  2. var
  3. Verbosity

1. Debug module with Msg parameter

Below is the skeleton for the piece that will be used in 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 job accepts debug module arguments via the message parameter. The only difference between the two tasks is that in the first task the parameter msg is present in the same line with the module name debug. In the second, the task module name and msg parameter are on a separate line and are 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 with the following commands.

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

Both tasks send the message to the console while the playbook is running, which can be verified by the following task output.

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 a message. The syntax for list of values ​​and dictionary mapping is 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

View the output below. You can see that the output is printed using Python’s syntactic view 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 ​​with the message parameter. If you look at the task below, I created two variables named UBUNTU_VERSION and EDITION† I print both variable values ​​with 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 }}"

Exit:

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

One 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 the 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 just want to print the variable, without strings like we did in the previous section, you can use the . usage var parameter and pass the variable name as an argument.

When using the messaging module, you must use double braces to print the variable value. But with the var parameter you can just use the . pass along variable name without braces or quotesI

    - 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 }}"

Exit:

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

3. Debug module with extended parameter

Ansible supports four levels of verbosity. Verbosity is set to zero by default. To print the messages, you need to increase the verbosity by -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 won’t see the output of the debug message in the terminal unless you use -v flag when activating the playbook.

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

In above task, I set verbosity level to two. Take a look at the output below, you can see that the output is printed as “skip” for this job.

$ 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 set the verbosity to two, I have to use the . usage -vv flag to print the messages. Likewise, depending on the verbosity level you set in the task, you should use the corresponding level of -v flag when running the script.

$ 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’ve gone through what the Ansible debug module is, how it works, and how to use the Ansible debug module with examples.

Ansible

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Ansible
  • Computer
  • Linux User Management
  • PHP MySQL
  • Reviews
  • Technology
  • Trending
  • Uncategorized
  • Windows 10 PC
  • Windows 11
  • XAMPP

Pages

  • ABOUT US
  • CONTACT US
  • COOKIES POLICY
  • Disclaimer
  • HOME
  • Privacy Policy
  • Terms and Conditions

Recent Posts

  • Top analyst says Apple will try again with the HomePod
  • Official Pixel 6a unboxing video shows off the new under-display fingerprint scanner for the phone
  • Windows 11’s New Feature Can Help You Save Time in Command Prompt
  • Snapchatters can now promote their eBay listings directly on Snapchat
  • Bug affecting eSIM on iPhone disables iMessage and FaceTime
©2022 Mujahidtricks | Powered by WordPress & Superb Themes