Ansible Debug

Ansible Debug
Techiio-author
Written by Nilima PaulJanuary 25, 2022
10 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Debug.

Introduction to Ansible Debug

In Ansible, when we make and run playbooks, it's exceptionally normal that we run into a mistake because of certain issues with our playbook. This can be a sentence structure blunder, a sensible mistake, or some obligatory boundary that is absent. Thus, this is vital that we ought consistently to compose our playbook in such have a way that it generally prints sufficient data, be it fruitful or disappointment. In Ansible, a module named investigate is the necessity when you want to troubleshoot and when you want more data in playbook execution yield.

In this article, we will attempt to gain proficiency with certain ideas and see Ansible investigate models. This is a default module that accompanies the Ansible bundle. This module prints articulations and variable qualities when executing playbooks. This can be exceptionally useful in cases, where you can skirt a few flighty errands in the playbook and don't have any desire to stop it through and through. Utilizing troubleshoot in every one of the whimsical errands will give you enough data about the information execution in those undertakings and all factors utilized in such assignments. This will help in investigating and is extremely valuable assuming you use it with enrolled factors. We will investigate such cases in the model area.

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 boundary acknowledges strings as sources of info. This is utilized to print a redid message. If no message is given, a conventional message like "Hi World!" is

var: - This acknowledges strings as information and this is the variable that has been set either by Ansible realities or by the playbook. Additionally, the qualities composed here will have implied twofold interjection, as this choice runs in the jinja2 setting. Along these lines, you don't have to utilize the jinja2 delimiter except if you need to print a twofold introduction too. You can utilize a twofold introduction when you print a variable in a

verbosity: - This has default as 0. This boundary is utilized to control when troubleshooting is in a run. For instance, if esteem 3 is given, the investigation will possibly 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

Presently we will take a few models, yet before going there, we initially comprehend our lab utilized for testing. Here we have an Ansible control server named ansible-regulator and two controllers have named have one and host-two. We will make playbooks run Ansible orders on the ansible-regulator hub and deal with the clients 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 host: host-one
tasks:
name: Here we use Ansible debug:

Output:

ansible-playbook debug_dafault_msg.yml
blogpost

Example #2

To print the value of a variable that was defined in the same playbook. we can create a simple playbook like below: the

Code:

---
name: This will print the defined variable. hosts: all
vars:
fruit: apple tasks:
name: Here we use Ansible debug module to print defined Variables.
debug:
var: fruit

Output:

ansible-playbook debug_print_var.yml
blogpost

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
blogpost

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
blogpost

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 a 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
blogpost

Example #6

Controlling Verbosity is finished by giving qualities against boundary verbosity and afterward referencing something similar or more number of - v while running the playbook. Like the playbook in past. Where we didn't characterize any verbosity, then, at that point, as a matter of course it ran with 0 verbosity level. Be that as it may, if characterize the verbosity level like beneath:

Code:

---
name: This playbook will print a value from registred variable hosts: all
tasks:
name: Here we are running a 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
blogpost

Conclusion

Ansible investigates module is an exceptionally accommodating instrument for playbook engineers and for heads who work on need to refresh a playbook now and again on per need premise. Additionally, while working in a group where others likewise have similar stakes as you, utilizing the investigate module to add more data is generally valuable and can stay away from disarray and conditions.

Ansible
Ansible Debug
Linux
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software91
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance