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.
You need to have the following requirements:
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.
There are several parameters that are used with this module:
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
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.
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.
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.
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.