Search code examples
ansiblejinja2

Getting error when I am trying to put expression inside a jinja template


I want to put an expression such as

"Host(`{{ .Name }}.access.{{website_tld}}`)"`

inside my jinja2 template static_traefik.yml.j2 so that {{ .Name }} should stay as is but {{website_tld}} should get replaced when I am running ansible task.

However this is throwing a massive error:

"AnsibleError: template error while templating string: unexpected char '\\\\' at 1016.
consulCatalog:\n    defaultRule: f\"Host(`{{{{ \\.Name }}}}.access.{{website_tld}}`)\"

Solution

  • Checkout jinja escaping https://jinja.palletsprojects.com/en/3.0.x/templates/#escaping

    So both would work:

    "Host(`{% raw %}{{ .Name }}{% endraw %}.access.{{website_tld}}`)"
    
    "Host(`{{ "{{ .Name }}" }}.access.{{website_tld}}`)"