This is a snippet from my Ansible jinja template which populates an environment specific template.

docker_compose_mq: <string-passed from Jenkins>
docker_compose_profiles: "string"

{% if "{{ risk_docker_compose_mq }}" == "string" %}
  {% "{{ risk_docker_compose_profiles: "string1" }}" %}
{% endif %}

This fails with a pretty generic error message:

"Syntax Error while loading YAML.\n\n\nThe error appears to have been in 'True': line 26, column 2, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\n(could not open file to display line)"}

I'm almost certain it's to do with escaping the quotes here but can't for the life of me work out what I'm doing wrong, any ideas?

  • Could you try this gist.github.com/halberom/794c06598f40ccc31560?
    – 030
    Commented May 21, 2018 at 17:41
  • No luck with this either - Convinced it must be something wrong with another aspect of my Ansible setup... @030
    – jto
    Commented May 29, 2018 at 13:48

2 Answers 2


The syntax failures are caused by the presence of the {{...}} expression blocks (normally used for filling the template output with the corresponding content) inside the {%...%} statement blocks.

I only used standalone jinja2 templates, so I'm not 100% certain if this applies to Ansible jinja templates as well, but I suspect so. In the Jinja2 {%...%} statement blocks variables are referenced directly (and variable assignments are done in {% set ...%} statements), so what you're after may be along these lines:

{% set docker_compose_mq = <string-passed from Jenkins> %}
{% set docker_compose_profiles = "string" %}

{% if risk_docker_compose_mq == "string" %}
  {% set risk_docker_compose_profiles = "string1" %}
{% endif %}
  • Didn't work out unfortunately. Maybe something deeper in my Ansible estate causing these issues.
    – jto
    Commented May 29, 2018 at 12:36

The syntax is the below one:

{% if backup_dest[0:1]  == "/" %}

=> this one tests if "backup_dest" first character is "/"

{% if variable == "blabla" %}

=> this one tests if "variable" equals to "blabla"


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.