Ansible

The Ansible Debug Module

We are going to talk about what the Ansible debug module is in this discussion. What permitted arguments are utilized in the debug module? Furthermore, how exactly do you employ the Ansible debug module with each argument in the playbook?

Many usable independent components called modules are available through Ansible and Ansible will run these modules to do some specific task. Debug is one of these modules in Ansible.  The Ansible debug package can identify any issues while executing the playbook. The issues could be a grammatical mistake, a conceptual mistake, or a missed argument in a playbook. The Ansible debug package shows information as the playbook is being executed in the Ansible terminal.

Whenever you execute the playbook by default, you would not receive output information. Alternatively, you will receive the state of the playbook, such as altered, ignored, etc. It can also be employed to debug parameters and expressions without affecting how the playbook is being executed. So, either it will be successful or a failure of the playbook like when we want to ignore any inconsistent activity in the playbook. But if we do not want to stop it completely, then the debug module will be extremely advantageous in this situation.

We will learn the processing of commands in all inconsistent activities and all parameters employed in them by using the debug in each of them. The use of this together with register parameters will improve troubleshooting and be greatly advantageous when we list the tasks in the playbook and debug them in Ansible.

Parameters of Debug Module in Ansible

The following are the parameters of the debug module which will be used in the Ansible playbook and each parameter contains a different value in it.

Msg Parameter: The “msg” parameter stores the value of the string in itself. If we want to display any message which we want to display in the ansible terminal, we will utilize this parameter. When we did not have any message in the playbook, a generic greeting like “Hello World!” will be displayed.

Var Parameter: The var” parameter has been set either through Ansible facts or the playbook that accepts string value as the input value. The var parameter executes in a jinja2 environment that is why the value stored in var will also include implicit double interpolation. So, if you want to display information that is stored in var that we will use the double interpolation, then we do not need to employ the Jinja2 terminator. Whenever we will showcase a parameter in an expression, we will utilize the double interpolation.

Verbosity Parameter:  The verbosity parameter has a default value which is 0. This parameter determines whether the debugger is running or not. Like, if the parameter contains a value of 2, then the debugger will only be performed when the playbook is being executed with -v or above.

Example 01: Utilizing the “msg” Parameter of Debug Module in Ansible

Here is the very first and simple example where we are going to use one of the parameters of debug module which is the “msg” parameter. The “msg” means message parameter and it is used to showcase any string or character statement in the output of Ansible. To implement this, we create the playbook in Ansible so that we can write the statement to execute.

[root@master ansible]# nano ansible_debug.yml

After creating the paybook, the ansible_debug.yml playbook will be launched into the new terminal of the Ansible tool. Now, let us start writing the hosts and the tasks which we want to implement in the terminal. First, we will write the name of the playbook task and then we will write the IP address or name of the remote host on which machine we want to configure things. Next, we will include the task option of ansible so that we can write the tasks in it. In the tasks, we will write the name and message which we want to debug in the remote host machine. As we want to showcase the message on the localhost machine, so we will write the message in it.

Shape Description automatically generated with medium confidence

Now, we have fulfilled all the commands related to the “msg” parameter in the playbook. Next, we will terminate the playbook. And as we want to check whether the playbook is running or not, we will write the below command in the terminal.

[root@master ansible]# ansible-playbook ansible_debug.yml

Now, after executing the above command, we will get the output like this as shown below:

Example 02: Utilizing the “var” Parameter of Debug Module in Ansible

Next, we want to implement the second example and another parameter of the debug module which is the “var” parameter. So, now, we do not have to create another playbook. We simply just opened the already created playbook ansible_debug.yml to alter the commands according to the “var” parameter requirements. In this example, we have added the gather_fct option as a “false” so that the debugger cannot get the extra data related to the local host. Then, we have included the “vars” parameter. In this parameter, we have first written the variable which we want to display “Hello” and the name of the technology we are using which is “Ansible”. Then, we listed the tasks in the playbook.

A picture containing text Description automatically generated

After closing the playbook, we will execute the playbook by writing the following command:

[root@master ansible]# ansible-playbook ansible_debug.yml

Here is the desired output of the playbook:

Example 03: Utilizing the “shell” & “register” Parameters in Ansible Debug Module

This is the 3rd and last example that we are implementing in Ansible. In this example, we are going to include the shell and the register parameter of Ansible. We have used the shell command “uptime” option so that we can also display the timestamp, duration, number of the remote host at the moment, and average system loading timing over the previous 1, 5, and 15 minutes. And then we passed the “system_uptime” option which is used to get the total time of configuring the commands on the remote host device.

Text Description automatically generated

After altering the data in the playbook, we will make the inventory file as we have included all the hosts in the playbook. So, we will provide the information of each host in it.

[rot@master ansible]# nano host.yml

Here is all the data related to the host so that we can execute the playbook:

All:

hosts:

Linux_Host:

ansible_host: 192.168.3.229

ansible_user: ansible

ansible_password: *******

ansible_connection: ssh

ansible_port: 22

To run the playbook here is the command:

[root@master ansible]# ansible-playbook ansible_debug.yml –i host.yml

Conclusion

We studied Ansible’s debug module in this guide. We have used Ansible’s module function debugging capabilities in the playbook. The debug module’s parameters have also been covered. To assist users, we comprehend each parameter with a clear explanation, and we have created several examples based on these parameters.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content