×


Ansible "Debug" Module - Explained with Examples with Ubuntu



Debugging process is a very critical part of developing a piece of software code. Ansible playbooks are not an exception for this.

For printing variables or messages on the terminal output during execution, Ansible provides a module called "debug". It is a very beneficial utility for developing a playbook. For example, it can be used with the “when:” directive and during the execution without interrupting the playbook.

Here at LinuxAPT, we shall look into the "debug module" in Ansible with Ubuntu. 


Prerequisite for this Tutorial

You need to have the following requirements:

  • You should have an installed Ansible on the controller node (Ubuntu 20.04 in our case).
  • You should have a basic knowledge about what the purpose of Ansible is and how to write a playbook (and of course you need to know what a playbook is).


If you are just a beginner in the Ansible world, we suggest that you first learn about Vagrant and how to set up a basic local testing environment using Vagrant. Once you set up a basic lab of having one controller node and two target nodes, you are all set to perform the examples in this guide.


Parameters Used with Debug Module

There are several parameters that are used with this module:

  • msg: It is a string type parameter that prints a custom message.
  • var: It specifies the variable to be debugged: "msg" and "var" are both mutually exclusive.
  • verbosity: This sets the number only after which the debug operation runs. For example, if it is set to 2, the debug runs only when "-vv" is specified.


Example Uses of Debug Module

1. Print a Simple Statement

In its most basic usage, the debug module can be used for printing a statement in the output. Create a playbook "debug-demo.yml" with the following content:

---
- name: Ansible debug module basic example
hosts: web
tasks:
- name: Basic debug module message
debug:
msg: "LinuxApt Ansible"

To run this playbook and any subsequent playbook, use the following format:

$ ansible-playbook example.yml -i /path/to/inventory/file


2. Print a Variable

Beside printing the basic statements on the terminal, we can also use the "debug" module to print the values of the variables. In the following example, we set a variable in the "my_vars.yml" file and output its value using the "msg" parameter of the debug module:

---
- hosts: web
vars_files:
- demo_vars.yml
tasks:
# Display "Variable 'demo_var1' is set to 'demo_val1'".
- debug: msg="Variable 'demo_var1' is set to {{ demo_var1 }}"

The previous playbook prints the value of the variable "demo_var1" along with the message specified in the "msg" parameter.

Similarly, the variables declared inside a playbook can also be used for printing on the terminal during a playbook execution:

---
- hosts: web
vars:
my_var2: my_val2
tasks:
# This task displays "Variable 'my_var2' is set to 'my_val2'".
- debug: msg="Variable 'my_var2' is set to {{ my_var2 }}"

In the previous playbook, the variable "my_var2" is printed during execution.


3. Using Debug Module with the Register Variables

In the similar approach as in the previous examples, we can also use the debug module with the Register variables. Now, let us create another playbook with the following content:

---
- hosts: all
gather_facts: no
become: false
tasks:
- name: Check the user name
ansible.builtin.shell: /usr/bin/whoami
register: login
- name: Display the user name using the output from previous task
debug: msg="Logged in as user {{ login.stdout }}"

The previous playbook has two tasks. The first task gives the name of the user from which the shell command was run and stores this value in a register type variable "login". The second task uses this variable in the printing the message using the “msg” parameter of the debug module.


4. Using the Debug Module Along with the "When" Conditional

In this case, we can set the condition for the debug module to run only when certain conditions are satisfied. Create a new playbook with the following contents:

---
- hosts: all
gather_facts: yes
become: true
tasks:
- name: Checking the release information of the servers
ansible.builtin.command: /usr/bin/lsb_release -a
register: info
- name: Print the release information
debug: msg="The System information is as follows {{ info.stdout }}"
when: ansible_facts['distribution']=="Debian"

In the previous playbook, we used the "lsb_release –a" command to print the release information for a system. The "when" statement sets a condition to print this information using the "debug" module only when the OS distribution is Debian.


[Need help in fixing Ansible issues ? We can help you. ]


Conclusion

This article covers how to use the "debug" module with some examples. In fact, the Ansible "debug" module is very useful for actively debugging operations as we seen in the given examples. It is equally helpful for getting a verbose output from a playbook.


How do Ansible Debug works ?

  • msg: – This parameter accepts strings as inputs. This is used to print a customized message. If no message is given, then a generic message like “Hello World!” is
  • var: – This accepts strings as input and this is the variable that has been set either by Ansible facts or by the playbook. Also, the values written here will be having implicit double interpolation, as this option runs in the jinja2 context. So, you don’t need to use jinja2 delimiter unless you want to print double interpolation as well. You can use double interpolation when you print a variable in a
  • verbosity: – This has default as 0. This parameter is used to control when debug is in a run. For example if value 3 is given then debug will only run if -v or above is given while running the playbook.

More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com