Interview Prep/Ansible/YAML, Syntax and Jinja2 Errors

`template error while templating string: unexpected char '$' at 18` when deploying Prometheus alert rules

IntermediateTroubleshootingYAML, Syntax and Jinja2 Errors
Answer

Likely causes → The file contains its own {{ }} syntax (Prometheus {{ $labels.instance }}, Go templates, Helm charts, GitHub Actions ${{ }}), and Ansible tries to render it as Jinja.

Fix → Choose one of these:

{% raw %}summary: "{{ $labels.instance }} is down"{% endraw %}
  • Use copy instead of template if the file has no Ansible variables at all.
  • Change the delimiters for this one file by putting a header on the first line: #jinja2: variable_start_string:'[[', variable_end_string:']]'.
  • Mark a variable value as !unsafe so it is never templated.

Prevent → Keep third-party template files under files/, not templates/.

All Ansible questions Roadmap: Ansible

More on YAML, Syntax and Jinja2 Errors