Table of contents
1.
Introduction
2.
Debugging Modules
2.1.
Simple Debugging
2.2.
Detailed Debugging Steps
3.
Frequently Asked Questions
3.1.
Which language should be used for writing plugins?
3.2.
Which option can be used to make the Ansible more verbose?
3.3.
What value should ANSIBLE_KEEP_REMOTE_FILES be set on the control host so that Ansible will keep the remote module files after the module finishes executing?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Ansible-Debugging Modules

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ansible is a simple IT automation engine that is used to automate cloud provisioning, configuration management, application deployment, and many other IT requirements. It has the capability for multi-tier deployment. It models the user's IT infrastructure by describing how all of the systems inter-relate, rather than just managing one system at a time. It is very simple to deploy as it uses no agents and requires no additional custom security infrastructure. 

Must Recommended Topic, Types of Agents in Artificial Intelligence.

Debugging Modules

Simple Debugging

A user can use epdb to run a debugger in a module either locally or remotely. The user needs to add import epdb; epdb.serve() in the module code on the control node at the desired breakpoint. Use the epdb.connect() to connect to the debugger. If the user wants to connect to a remote node, then the user must use a port that is allowed by any firewall between the control and the remote node. It is guaranteed that this particular technique will work with any remote debugger, but it is not guaranteed that any particular remote debugging tool will work. Another very useful debugging tool is the q library. The user can raise an exception if they want to see some specific data, as print() statements do not work inside the modules. The user can put the raise Exception(some_value) anywhere inside the module and run it. The Ansible will handle the exception and will pass the message back to the control node and display it.

Detailed Debugging Steps

The ansible modules are in zip file format consisting of module files as well as various Python module boilerplate inside of a wrapper script. If the user wants to know what is happening inside the module, then the user needs to extract the file from the wrapper. Helper methods are provided by the wrapper script to help the user do that. 

The below-mentioned steps use localhost as the target host, though a remote host can also be used. 

  • The user needs to set the ANSIBLE_KEEP_REMOTE_FILES to 1 on the control host. This will make the Ansible to keep the remote module files rather than deleting them after the module has finished executing. The -vvv option can be used to make Ansible more verbose. This displays the temporary module file name
ANSIBLE_KEEP_REMOTE_FILES=1 ansible localhost -m ping -a 'data=debugging_session' -vvv
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: badger
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1461434734.35-235318071810595 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1461434734.35-235318071810595 `" )'
<127.0.0.1> PUT /var/tmp/tmpjdbJ1w TO /home/badger/.ansible/tmp/ansible-tmp-1461434734.35-235318071810595/AnsiballZ_ping.py
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/badger/.ansible/tmp/ansible-tmp-1461434734.35-235318071810595/AnsiballZ_ping.py && sleep 0'
localhost | SUCCESS => {
    "changed": false,
    "invocation": {
        "module_args": {
            "data": "debugging_session"
        },
        "module_name": "ping"
    },
    "ping": "debugging_session"
}
  • From the previous step, the user needs to navigate to the temporary directory. The user needs to connect to the remote host before trying to navigate to the temporary directory if the previous command was run against a remote host. 
ssh remotehost # only if not debugging against localhost
$ cd/home/badger/.ansible/tmp/ansible-tmp-1461434734.35-235318071810595
  • To convert the string into Python files with which the user can work, use the wrapper's explode command:
$ python AnsiballZ_ping.py explode
Module expanded into:
/home/badger/.ansible/tmp/ansible-tmp-1461434734.35-235318071810595/debug_dir
  • When the user looks into the temporary directory, the structure will look like this:
    directory structure
  • AnsiballZ_ping.py is a Python script in which the module code is stored in a base64 encoded string. It also contains various other helper functions for executing the module.
  • ping.py is the code for the module. 
  • args file contains a JSON string. It is a dictionary that contains module arguments as well as other variables that the Ansible passes into the module for changing its behavior.
  • After editing the code or arguments in the exploded tree, the user can use the execute subcommand to run it:
$ python AnsiballZ_ping.py execute
{"invocation": {"module_args": {"data": "debugging_session"}}, "changed": false, "ping": "debugging_session"}

Frequently Asked Questions

Which language should be used for writing plugins?

The plugins should be written in Python.

Which option can be used to make the Ansible more verbose?

The -vvv option can be used to make the Ansible more verbose.

What value should ANSIBLE_KEEP_REMOTE_FILES be set on the control host so that Ansible will keep the remote module files after the module finishes executing?

The ANSIBLE_KEEP_REMOTE_FILES should be set to 1 on the control host so that Ansible will keep the remote module files after the module finishes executing.

Conclusion

In this article, we have extensively discussed Ansible-Debugging Modules.

After reading about the Ansible-Debugging Modules, are you not feeling excited to read/explore more articles on Development? Don't worry; Coding Ninjas has you covered. To learn about system design strategieshow to start learning full-stack web development and what are top web development languages.

If you wish to enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, etc., you should check out our Guided path column at Coding Ninjas Studio. We at Coding Ninjas Studio organize many contests in which you can participate. You can also prepare for the contests and test your coding skills by giving the mock test series available. In case you have just started the learning process, and your dream is to crack major tech giants like Amazon, Microsoft, etc., then you should check out the most frequently asked problems and the interview experiences of your seniors that will surely help you in landing a job in your dream company. 

Do upvote if you find the blogs helpful.

Happy Learning!

Thank you image
Live masterclass