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:
- 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"}