Debugging executor code

Executor code in Ansible is the connector code that binds together inventory data, playbooks, plays, tasks, and connection methods. While each of those other code bits can be individually debugged, how they interact can be examined within executor code.

The executor classes are defined in various files within executor/ and the PlaybookExecutor class. This class handles executing all of the plays and tasks within a given playbook. The class creation function, __init__(), creates a series of placeholder attributes, as well as setting some default values, while the run() function is where most of the fun happens.

Debugging can often take you from one file to another, jumping around the code base. For example, in the __init__() function of the PlaybookExecutor class, there is code to cache whether or not the default SSH executable supports ControlPersist. ControlPersist is the feature of SSH that keeps sockets to remote hosts open for a period of time for fast reuse. Let's put a breakpoint here and follow the code:

Now, we can run our objmethod.yml playbook again to get into a debugging state:

We'll need to step into the function to follow the execution. Stepping into the function will take us to a different file, as follows:

From here, we can use list to see the code in our new file:

Walking a few more lines down, we come to a block of code that will execute an ssh command and check the output to determine whether ControlPersist is supported:

Let's walk through the next couple of lines and then print out what the value of err is. This will show us the result of the ssh execution and the whole string that Ansible will be searching within:

As we can see, the search string is not within the err variable, so the value of has_cp remains as the default of True.

A quick note on forks and debugging: When Ansible uses multiprocessing for multiple forks, debugging becomes difficult. A debugger may be attached to one fork and not another, which will make it very difficult to debug the code. Unless specifically debugging the multiprocessing code, it's a best practice to stick to a single fork.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.79.59