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

CC BY-SA 4.0
2022-11-15T08:14:46.270Z
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`
diggnsaegg
CC BY-SA 4.0
2022-11-15T09:12:13.890Z
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`.
Vladimir Botka
CC BY-SA 4.0
2022-11-14T21:27:09.907Z
2022-11-14T21:27:09.907Z
Vladimir Botka
<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>

<ansible>
Ansible jinja2 if/elif/else construct not working anymore
diggnsaegg
# 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
{% 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`.  

---

