<p>The syntax failures are caused by the presence of the <code>{{...}}</code> <a href="http://jinja.pocoo.org/docs/2.10/templates/#expressions" rel="nofollow noreferrer">expression</a> blocks (normally used for filling the template output with the corresponding content) inside the <code>{%...%}</code> <a href="http://jinja.pocoo.org/docs/2.10/templates/#list-of-control-structures" rel="nofollow noreferrer">statement</a> blocks.</p>

<p>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 <code>{%...%}</code> statement blocks variables are referenced directly (and variable assignments are done in <code>{% set ...%}</code> statements), so what you're after may be along these lines:</p>

<pre><code>{% set docker_compose_mq = &lt;string-passed from Jenkins&gt; %}
{% set docker_compose_profiles = "string" %}

{% if risk_docker_compose_mq == "string" %}
  {% set risk_docker_compose_profiles = "string1" %}
{% endif %}
</code></pre>

CC BY-SA 4.0
2018-05-29T12:36:59.977Z
Didn't work out unfortunately. Maybe something deeper in my Ansible estate causing these issues.
jto
CC BY-SA 4.0
2018-05-23T00:12:27.937Z
2018-05-23T00:12:27.937Z
Dan Cornilescu
<p>The syntax is the below one:</p>

<pre><code>{% if backup_dest[0:1]  == "/" %}
</code></pre>

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

<pre><code>{% if variable == "blabla" %}
</code></pre>

<p>=> this one tests if "variable" equals to "blabla"</p>

<p>Regards</p>

CC BY-SA 4.0
2020-03-10T00:17:30.073Z
2020-03-10T00:17:30.073Z
Fred DENIS
<p>This is a snippet from my Ansible jinja template which populates an environment specific template.</p>

<pre><code>docker_compose_mq: &lt;string-passed from Jenkins&gt;
docker_compose_profiles: "string"

{% if "{{ risk_docker_compose_mq }}" == "string" %}
  {% "{{ risk_docker_compose_profiles: "string1" }}" %}
{% endif %}
</code></pre>

<p>This fails with a pretty generic error message:</p>

<pre><code>"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)"}
</code></pre>

<p>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?</p>

CC BY-SA 4.0
2018-05-21T17:41:46.493Z
Could you try this https://gist.github.com/halberom/794c06598f40ccc31560?
030
CC BY-SA 4.0
2018-05-29T13:48:06.090Z
No luck with this either - Convinced it must be something wrong with another aspect of my Ansible setup... @030
jto
<ansible><jinja><templating>
Ansible Jinja template if statement
jto
# Ansible Jinja template if statement

(Asked by: username_0 on 2018-05-21)

This is a snippet from my Ansible jinja template which populates an environment specific template.
docker_compose_mq: 
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?

**Tags:** ansible, jinja, templating

> **Comments:**
> **username_1**: Could you try this https://gist.github.com/halberom/794c06598f40ccc31560?  
> **username_0**: No luck with this either - Convinced it must be something wrong with another aspect of my Ansible setup... @username_1  

---

## 2 answers

### Answer by username_2 on 2018-05-23. Score: 3

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 =  %}
{% set docker_compose_profiles = "string" %}

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

> **Comments:**
> **username_0**: Didn't work out unfortunately. Maybe something deeper in my Ansible estate causing these issues.  

---

### Answer by username_3 on 2020-03-10. Score: 0

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"
Regards


---

