Answers: <p>Fix the condition</p>
  <pre class="lang-yaml prettyprint-override"><code>{% if production_env == &quot;prod&quot; or
        production_env == &quot;stage&quot; %}
    password: &quot;{{ group_token }}&quot;
    username: usernamedoesntmatterhere
  {% elif production_env == &quot;dev&quot; %}
    password: &quot;{{ mytoken }}&quot;
    username: &quot;{{ mytokenname }}&quot;
  {% else %}
      NONE
  {% endif %}
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-11-15T08:14:46.270Z
Answers: This works! Can you explain why? My code above worked for months and suddenly didn't anymore. I don't get why Ansible templated the first block even with `dev`set as fact for `production_env`
Answers: diggnsaegg
Answers: CC BY-SA 4.0
Answers: 2022-11-15T09:12:13.890Z
Answers: This could have been working for `"prod" or "stage"` because the string "stage" evaluates to `True`. But `"dev"` could not work because the first option is always `True`.
Answers: Vladimir Botka
Answers: CC BY-SA 4.0
Answers: 2022-11-14T21:27:09.907Z
Answers: 2022-11-14T21:27:09.907Z
Answers: Vladimir Botka
Body: <p>I am using a jinja2 template for creating several files. Those differ a bit dependent on a set variable.</p>
  <p>Here's the code:</p>
  <pre><code>{% if production_env == &quot;prod&quot; or &quot;stage&quot; %}
    password: &quot;{{ group_token }}&quot;
    username: usernamedoesntmatterhere
  {% elif production_env == &quot;dev&quot; %}
    password: &quot;{{ mytoken }}&quot;
    username: &quot;{{ mytokenname }}&quot;
  {% else %}
      NONE
  {% endif %}
  </code></pre>
  <p>As you can see when <code>production_env</code> is set to <code>dev</code> it should template the second block.
  It seems that my elif condition is not working anymore.
  I used a debug task to get output which variable is set and this is correctly set to <code>dev</code>
  Ansible renders the first block even if <code>dev</code> is set correctly.</p>
  <p>That worked fine for months, not sure why it doesn't now.</p>
  <p>I already ditched the <code>elif</code> block and set it to <code>else</code>, even this is not working.</p>
  <p>Any hints?</p>
  <p>Thanks</p>
  
Tags: <ansible>
Title: Ansible jinja2 if/elif/else construct not working anymore
UserDisplayName: diggnsaegg
ThreadText: # Ansible jinja2 if/elif/else construct not working anymore
  
  (Asked by: username_0 on 2022-11-14)
  
  I am using a jinja2 template for creating several files. Those differ a bit dependent on a set variable.
  
  Here's the code:
  
  ```
  {% if production_env == "prod" or "stage" %}
    password: "{{ group_token }}"
    username: usernamedoesntmatterhere
  {% elif production_env == "dev" %}
    password: "{{ mytoken }}"
    username: "{{ mytokenname }}"
  {% else %}
      NONE
  {% endif %}
  
  ```
  
  As you can see when `production_env` is set to `dev` it should template the second block.
  It seems that my elif condition is not working anymore.
  I used a debug task to get output which variable is set and this is correctly set to `dev`
  Ansible renders the first block even if `dev` is set correctly.
  
  That worked fine for months, not sure why it doesn't now.
  
  I already ditched the `elif` block and set it to `else`, even this is not working.
  
  Any hints?
  
  Thanks
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-11-14. Score: 0 | Accepted answer
  
  Fix the condition
  
  ```yaml
  {% if production_env == "prod" or
        production_env == "stage" %}
    password: "{{ group_token }}"
    username: usernamedoesntmatterhere
  {% elif production_env == "dev" %}
    password: "{{ mytoken }}"
    username: "{{ mytokenname }}"
  {% else %}
      NONE
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: This works! Can you explain why? My code above worked for months and suddenly didn't anymore. I don't get why Ansible templated the first block even with `dev`set as fact for `production_env`  
  > **username_1**: This could have been working for `"prod" or "stage"` because the string "stage" evaluates to `True`. But `"dev"` could not work because the first option is always `True`.  
  
  ---
  
  