How to Use Debug Module in Ansible Playbook

Ansible provides hundreds of modules which are reusable standalone scripts that get executed by Ansible on your behalf. The Ansible debug module is a handy module that prints statements during playbook execution. Additionally, it can be used to debug expressions and variables without interfering with the playbook execution.

In this guide, we are going to demonstrate how to use debug module in ansible playbook with examples.

Print a Simple Statement Using Debug Module

The most basic use of the debug module in Ansible is to prints simple statements to stdout (Standard Output). Consider a playbook that contains the following content.

---
- name: Ansible debug module in action
  hosts: all
  tasks:
          - name: Print a simple statement
            debug:
              msg: "Hello World! Welcome to Linuxtechi"

Debug-Simple-Print-Statement

When executed, the playbook prints the statement “Hello World! Welcome to Linuxtechi” to the terminal.

$ sudo ansible-playbook /etc/ansible/print-simple-stamement.yml

Simple-Debug-Print-Ansible-Playbook-Execution

Print Variables Using Debug module

Apart from just printing simple statements to the terminal, the debug module also captures and prints the values of variables using the msg parameter.

Consider the playbook below. We have specified two variables: greetings and site using the vars keyword.

---
- name: Ansible debug module in action
  hosts: all
  vars:
          greetings: Hello World!
          site: Linuxtechi
  tasks:
          - name: Print the value of a variable
            debug:
              msg: "{{ greetings }}, Welcome to {{ site }}."

Debug-Variables-Ansible-Playbook

During playbook runtime, the debug module retrieves the values stored in the variables and prints them to stdout using the msg parameter.

$ sudo ansible-playbook /etc/ansible/print-variable.yml

Execute-Debug-Variables-Ansible-Playbook

Use Debug Module with Shell & Register Modules

The debug command can also be used alongside other Ansible modules such as shell and register modules. Consider the playbook file shown.

The Playbook checks how long the remote node has been running using the uptime -p shell command. The output of the command is captured by the register module and saved in the system_uptime variable.

The register module then prints out the value of the variable which contains the command output to stdout.

---
- name: Ansible debug module in action
  hosts: all
  tasks:
          - name: Print system uptime
            shell: uptime -p
            register: system_uptime
          - name: Print uptime of managed node
            debug:
              msg: "{{ system_uptime }}"

Debug-Shell-Register-Ansible-Playbook

When the playbook is executed, you get a bunch of information printed to stdout. At the bottom, be sure to find the output on the system uptime as defined in the Playbook.

$ sudo ansible-playbook /etc/ansible/print-system-uptime.yml

Execute-Debug-Shell-Register-Playbook

Conclusion

In general, the Ansible debug module is a handy module that prints statements to the output. These can be simple strings or command output which is stored in variables. In most cases, it is used with shell and register modules to streamline capturing and printing command output.

Also Read : How to Use Ansible Vault to Secure Sensitive Data

1 thought on “How to Use Debug Module in Ansible Playbook”

Leave a Comment

Your email address will not be published. Required fields are marked *