How do you stop Ansible from evaluating `{{ }}` in a string, such as for Go or Helm templates?
AdvancedConceptJinja2, Conditionals and Loops
Answer
Wrap the text in {% raw %}...{% endraw %} inside templates, or mark a YAML value as unsafe with !unsafe so it is never templated.
- ansible.builtin.copy:
dest: /etc/prometheus/alert.yml
content: |
{% raw %}
summary: "{{ $labels.instance }} is down"
{% endraw %}
- ansible.builtin.debug:
msg: !unsafe "{{ not_rendered }}"
!unsafe is also how you protect values from untrusted sources (like command output or inventory strings from an API) from being templated, a real injection risk if that data contains {{ }}.
More on Jinja2, Conditionals and Loops
What role does Jinja2 play in Ansible?BeginnerWhat are the three Jinja2 delimiters?BeginnerShow a template with a loop and a conditional, and how it is deployed.BeginnerWhich filters do you use every day?BeginnerHow do you transform lists and dicts with filters (`map`, `select`, `selectattr`, `unique`, `flatten`)?IntermediateHow does `when` work and which operators can you use?Beginner