Jinja2: Encountered Unknown Tag

Overview

I was running an Ansible playbook and one of the templates failed with the following error:

AnsibleError: template error while templating string: Encountered unknown tag 'd'

Jinja2 uses a brace + percent and a percent + brace to identify Jinja2 commands that are used when preprocessing a template.

This error simply is telling you that there’s a brace + percent that’s in the template that isn’t a Jinja2 command or “tag”. Search the file for a brace + percent and you should be able to locate the offending line.

Typically this might be related to a printf() type command where the output is something like this:1

message += "_e{%d,%d}:%s|%s" % (len(title), len(text), title, text)

Note specifically the brace + percent + d in the line. This is causing the problem.

Solution

Wrap the line (or lines) in a raw tag like so:

{% raw %}
			if title and text:
				message += "_e{%d,%d}:%s|%s" % (len(title), len(text), title, text)
{% endraw %}

And the application of the template succeeds.

This entry was posted in ansible, Computers and tagged , , , . Bookmark the permalink.

Leave a Reply

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