Using Ansible How Do You Search The Contents of a File on Linux for “{{” or “}}”?

Problem scenario
You want to see if an Ansible playbook is injecting (or leaving) two braces, "{{" or "}}", in a specific file on Linux.  These symbols can be injected accidentally with little notification.  Variable substitution could fail during the course of a playbook run.  This is a subtle problem that may have no alert.  You want some debugging to check for these symbols.  How do you know a file on a managed node does not have a "{{" or "}}"?

Solution
Root cause

What makes the double braces difficult to place into a shell module in Ansible is that Ansible views them (as a pair) as a reserved symbol; variable expansion in Ansible is done via these very symbols in quotes.  

Procedure
You need to re-write the playbook to use the "shell" module. To search for lines that have both types of double braces, "{{" and "}}", with no quotes, put these two lines in your playbook.
shell:  /usr/bin/cat /path/to/file.txt | grep "\{\{" | grep "\}\}"
register: foundplaceholder

The backslash "\" (without quotes) acts as an escape character.  The variable foundplaceholder will have a value if a single line had both types of double braces, "{{" and "}}" (with no quotes).

Leave a comment

Your email address will not be published. Required fields are marked *