Interview Prep/Ansible/Jinja2, Conditionals and Loops

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 {{ }}.

All Ansible questions Roadmap: Ansible

More on Jinja2, Conditionals and Loops