EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Ansible Debug

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Ansible Tutorial » Ansible Debug

Ansible Debug

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,013 ratings)
Course Price

View Course

Related Courses
Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

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

Ansible Debug1

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

Ansible Debug2

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

Ansible Debug3

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

Ansible Debug4

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

JSON format

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

Verbosity

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 –

  1. Ansible Versions
  2. Ansible Architecture
  3. Ansible Vault
  4. Ansible Filters

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Ansible Tutorial
  • Ansible Basics
    • What is Ansible
    • Is Ansible free
    • How to Install Ansible
    • Ansible Versions
    • Ansible Architecture
    • Ansible Commands
    • Ansible ad-hoc Commands
    • Ansible Playbooks
    • Ansible Roles
    • Ansible Tags
    • Ansible Ping
    • Ansible Apt
    • Ansible Facts
    • Ansible Tower
    • Ansible Galaxy
    • Ansible Handlers
    • Ansible Loop
    • Ansible Block
    • Ansible Conditional
    • Ansible YAML
    • Ansible Debug
    • Ansible Synchronize
    • Ansible Template
    • Ansible group_vars
    • Ansible Lookup
    • Ansible File Module
    • Ansible Yum Module
    • Ansible Shell Module
    • Ansible lineinfile
    • Ansible Service Module
    • Ansible User Module
    • Ansible Windows Modules
    • ansible-doc
    • Ansible Filters
    • Ansible Add User to Group
    • Ansible Register
    • Ansible Set Fact
    • Ansible Hosts File
    • Ansible add_host
    • Ansible Collections
    • Ansible with_items
    • Ansible Replace Line in File
    • Ansible inventory_hostname
    • Ansible Dynamic Inventory
    • Ansible local_action
    • Ansible Firewalld
    • Ansible Unarchive
    • Ansible Sudo
    • Ansible Create Directory
    • Ansible Reboot
    • Ansible wait_for
    • Ansible run_once
    • Ansible Authorized_key
    • Ansible Interview Questions

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Linux Training Course Learn More