Updated April 4, 2023
Introduction to Ansible Debug
In Ansible, when we create and run playbooks, it’s very common that we run into an error due to some issues with our playbook. This can be a syntax error, a logical error or some mandatory parameter is missing. So, this is very important that we should always write our playbook in such have a way that it always prints enough information, be it successful or failure. In Ansible, a module named debug is the requirement when you need to debug and when you need more information in playbook execution output.
In this article we will try to learn some concepts and see Ansible debug examples. This is a default module that comes with Ansible package. This module prints statements and variable’s values when executing playbooks. This can be very helpful in cases, where you can skip some erratic tasks in the playbook and don’t want to stop it altogether. Using debug in all the erratic tasks, will provide you enough information about the data execution in those tasks and all variables used in such tasks. This will help in troubleshooting and is very useful if you use it with registered variables. We will explore such cases in the example section.
How do Ansible Debug works?
In Ansible debug module comes with some parameters and these parameters accept some options. This is given below:
- 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.
For Ansible debug, we should also note this works with Microsoft Windows supported modules as well.
Examples to Implement Ansible Debug
Now we will take some examples, but before going there, we first understand our lab used for testing purposes. Here we have an Ansible control server named ansible-controller and two remotes hosts named host- one and host-two. We will create playbooks and run Ansible commands on the ansible-controller node and manage the users on remote hosts.
Examples #1
To print a default message on the output of running a playbook via ansible-playbook, we can create a simple playbook like below:
Code:
---
name: This will print a default hosts: host-one
tasks:
name: Here we use Ansible debug debug:
Output:
ansible-playbook debug_dafault_msg.yml
Example #2
To print the value of a variable that was defined in the same playbook. we can create a simple playbook like below:
Code:
---
name: This will print the defined varible. hosts: all
vars:
fruit: apple tasks:
name: Here we use Ansible debug module to print defined Variable.
debug:
var: fruit
Output:
ansible-playbook debug_print_var.yml
Example #3
To print an Ansible fact of remote hosts, we can create a simple playbook like
Code:
---
name: This will print one of Ansible facts from remote hosts: all
tasks:
name: Here we use Ansible debug module the ipv4 of remote debug:
var: ansible_default_ipv4.address
Output:
ansible-playbook debug_ansible_fact.yml
Example #4
To print a customized message on the screen, Write a playbook like below:
Code:
---
name: This will print customized message hosts: all
tasks:
name: Here we use Ansible debug module to print some customized message debug:
msg: "This is machine is {{ ansible_hostname }} with IP {{ ansible_default_ipv4.address }}"
Output:
ansible-playbook debug_with_customized_msg.yml
Example #5
You can register the output of a task in a variable and as we know the values of these variables will be stored in JSON format. So, you can call that variable later in the same playbook. To test this or to test which value will be used when we call registered variables. We can use debug like below, creating a playbook.
Code:
---
name: This playbook will print a value from registred variable hosts: all
tasks:
name: Here we are running command module to run a command "hostname" command: free -m
register: free_mem
debug:
var: free_mem.stdout_lines
Output:
ansible-playbook debug_register_values.yml
Example #6
Controlling Verbosity is done by giving values against parameter verbosity and then mentioning the same or more number of –v while running the playbook. Like the playbook in previous. Where we didn’t define any verbosity, then by default it ran with 0 verbosity level. But if define the verbosity level like below:
Code:
---
name: This playbook will print a value from registred variable hosts: all
tasks:
name: Here we are running command module to run a command "hostname" command: free -m
register: free_mem
debug:
var: free_mem.stdout_lines verbosity: 1
Output:
ansible-playbook debug_register_values.yml -v
Conclusion
Ansible debug module is a very helpful tool for playbook developers and for administrators who work on need to update a playbook frequently on per need basis. Also, while working in a team where others also have the same stakes as you, using the debug module to add more information is always beneficial and can avoid confusion and dependencies.
Recommended Articles
This is a guide to Ansible Debug. Here we discuss an introduction to Ansible Debug, how does it work with examples. You can also go through our other related articles to learn more –