Ubuntu

Ansible “Debug” Module

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.

What Will We Cover?

In this tutorial, we will see about the “debug module” in Ansible. We will also learn about the various use cases. Let’s get started now.

Prerequisites

To perform the examples shown in this tutorial, we need to have the following requirements:

1. You should have an installed Ansible on the controller node (Ubuntu 20.04 in our case).

2. 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:

1. msg: It is a string type parameter that prints a custom message.

2. var: It specifies the variable to be debugged: “msg” and “var” are both mutually exclusive.

3. 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. Printing 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: "LinuxHint Ansible"

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

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

2. Printing 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.

Conclusion

In this guide, we have seen how to use the “debug” module with some examples. 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.

 

Similar Posts