Skip to content

Instantly share code, notes, and snippets.

@weshouman
Last active March 1, 2020 14:27
Show Gist options
  • Save weshouman/89ec9411ef7c3eebc3dc0fec9e67c004 to your computer and use it in GitHub Desktop.
Save weshouman/89ec9411ef7c3eebc3dc0fec9e67c004 to your computer and use it in GitHub Desktop.
Debugging Ansible tips

Debugging Options

To debug (using print statements), one has 2 options

Debug the Module

  • The module files are located inside ansible/module_utils/module_name/ ie: ansible/module_utils/k8s/
  • The parameters given to a specific task are stored in the self.params
  • self.params gets the values assigned outside the module (from the Module System)
  • Adding any print statements, will result in the task failing, so after debugging ensure no prints are still existent

Debug the Module System

  • The module systems files are located in ansible/module_utils/ (ie: basic.py ) and ansible/module_utils/system/ (ie: setup.py)
  • Modifying those files won't help at all, as they will
  1. Fail the task with the message The following modules failed to execute: setup
  2. Avoid from reaching the other non-system debug points.

Use Case

In my debugging session, I used from_yaml_all filter, instead of from_yaml_all | list which resulted in the resource_definition param being the generator object's string, thus I got the error described in this issue.
The use of list filter forces the from_yaml_all to return the output as a list, that can be justified by the fact that ansible is written in Python, and it depends on the output of yaml.load_all() which should be loaded with list(), following this answer.

Template Processing

  • Jinja whitespace templating could be further controlled based on this SO Answer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment