Ansible Hacks

In some cases you may want to temporarily hack Ansible by patching its source code.

Patch ansible diff to ignore re-ordering of lines or changes in case of strings

Ansible diff and check mode -D -C is indispensible to show what changes would be applied, without actually doing anything. In most situations the diff is relatively small and can be understood. But in some cases, the refactoring may be quite large, making the diff unreadable. This may for instance happen to a file, where the ordering of the lines, or upper/lowercase of the strings, does not really matter. Ideally you want to spot those changes that are not only moving a line or changing its case.

Edit ansible's plugins/callback/__init__.py:

vim $(ansible --version | grep 'python module location' | cut -d = -f 2)/plugins/callback/__init__.py

and patch the _get_diff function to apply .lower() and sorted() before computing the diff.

@@ -474,14 +474,14 @@
                     after_header = u"after: %s" % diff['after_header']
                 else:
                     after_header = u'after'
-                before_lines = diff['before'].splitlines(True)
-                after_lines = diff['after'].splitlines(True)
+                before_lines = diff['before'].lower().splitlines(True)
+                after_lines = diff['after'].lower().splitlines(True)
                 if before_lines and not before_lines[-1].endswith(u'\n'):
                     before_lines[-1] += u'\n\\ No newline at end of file\n'
                 if after_lines and not after_lines[-1].endswith('\n'):
                     after_lines[-1] += u'\n\\ No newline at end of file\n'
-                differ = difflib.unified_diff(before_lines,
-                                              after_lines,
+                differ = difflib.unified_diff(sorted(before_lines),
+                                              sorted(after_lines),
                                               fromfile=before_header,
                                               tofile=after_header,
                                               fromfiledate=u'',

Run ansible with -D -C to inspect the new diff, then revert the patch before pushing the changes.

Patch ansible to remove (mitogen) strategy plugin deprecation

Ansible 12+ deprecates third-party strategy plugins like mitogen. Ansible itself only supports disabling all deprecation warnings with deprecation_warnings = False, which may hide other valuable messages. While waiting for mitogen to adapt, the annoying deprecation warning can be removed by patching the ansible source code:

sed -i '/is_core_plugin = /s/.*/        is_core_plugin = 1/' $(ansible --version | grep 'python module location' | cut -d = -f 2)/plugins/loader.py

Patch ansible to fix missing protomatter manifest

An old upstream Bug triggers the following warning message:

[WARNING]: Collection at '[...]/_internal/ansible_collections/ansible/_protomatter' does not have a MANIFEST.json file [...]

The same issue documents the following work-around

cat > $(ansible --version | grep 'python module location' | cut -d = -f 2)/_internal/ansible_collections/ansible/_protomatter/MANIFEST.json << "EOF"
{
  "collection_info": {
    "namespace": "ansible",
    "name": "_protomatter",
    "version": "2.20.3"
  }
}
EOF