Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2018-05-29T12:36:59.977Z
Answers: Didn't work out unfortunately. Maybe something deeper in my Ansible estate causing these issues.
Answers: jto
Answers: CC BY-SA 4.0
Answers: 2018-05-23T00:12:27.937Z
Answers: 2018-05-23T00:12:27.937Z
Answers: Dan Cornilescu
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-10T00:17:30.073Z
Answers: 2020-03-10T00:17:30.073Z
Answers: Fred DENIS
Body: <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>
  
Comments: CC BY-SA 4.0
Comments: 2018-05-21T17:41:46.493Z
Comments: Could you try this https://gist.github.com/halberom/794c06598f40ccc31560?
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2018-05-29T13:48:06.090Z
Comments: No luck with this either - Convinced it must be something wrong with another aspect of my Ansible setup... @030
Comments: jto
Tags: <ansible><jinja><templating>
Title: Ansible Jinja template if statement
UserDisplayName: jto
ThreadText: # 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: <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?
  
  **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](http://jinja.pocoo.org/docs/2.10/templates/#expressions) blocks (normally used for filling the template output with the corresponding content) inside the `{%...%}` [statement](http://jinja.pocoo.org/docs/2.10/templates/#list-of-control-structures) 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 %}
  
  ```
  
  > **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
  
  
  ---
  
  