# IF/Else-Statement at vars

**URL:** <https://forum.ansible.com/t/if-else-statement-at-vars/46381>\
**Category:** Get Help\
**Tags:** playbook, ansible\
**Created:** [September 30, 2026, 7:32pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381 "2026-09-30T19:32:12Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://forum.ansible.com/u/Jonny)\
**Post date:** [September 30, 2026, 7:32pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/1 "2026-09-30T19:32:12Z")

</div>

Hi community,  
I heard somewhere that you shouldn’t use an if-else statement in jinja2 format inside a variable. Unfortunately, I can’t find anything about this in any best practices guide.

- Is this statement correct?
- Is there another good best practices guide besides [Good Practices for Ansible - GPA](https://redhat-cop.github.io/automation-good-practices/) ?

Here’s a code example:

```auto
- ansible.builtin.set_fact:
    myvar: "{{'value' if (my_group_var) else 'other_value'}}"

```

Thank you for your help

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 30, 2026, 7:39pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/2 "2026-09-30T19:39:13Z")

</div>

No matter what you heard, use what works for you, as an alternative, if you really must change, I offer `ternary`:

```auto
myvar: "{{ my_group_var|ternary('value', 'other_value') }}"

```

---

<div class="post-metadata">

**Author:** ![kks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kks/32/10352_2.png) [@kks](https://forum.ansible.com/u/kks)\
**Post date:** [September 30, 2026, 8:45pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/3 "2026-09-30T20:45:04Z")

</div>

I advocate for not using “if” statements inside jinja statements and jinjas in general. But I agree with previous comment:

> [@bcoca](#):
>
> No matter what you heard, use what works for you

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [September 30, 2026, 9:14pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/4 "2026-09-30T21:14:35Z")

</div>

> [@kks](#):
>
> I advocate for not using “if” statements inside jinja statements and jinjas in general

The argument being…?

---

<div class="post-metadata">

**Author:** ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://forum.ansible.com/u/Jonny)\
**Post date:** [October 1, 2026, 6:52am UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/5 "2026-10-01T06:52:12Z")

</div>

Can you please explain your comment?

I heard dont use if statements, its better to use ansible filters and when not create your own. But to create every time a new filter its a little bit oversized. In my opinion.

---

<div class="post-metadata">

**Author:** ![kks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kks/32/10352_2.png) [@kks](https://forum.ansible.com/u/kks)\
**Post date:** [October 1, 2026, 6:57am UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/6 "2026-10-01T06:57:46Z")

</div>

> [@bvitnik](#):
>
> The argument being…?

Jinja is a text engine, not all data is text, when something a bit more complex is being used - a lot of problems appear all of the suddent, e.g. jinja spacing issues when you want result to be dict or list. Coertion from text to other data types (implicit in old ansible-core versions, explicit in new versions) is also kind of a problem, I prefer to be sure that I work with “native” data sctructures.

Some more arguments are in this thread

> [@How to solve ansible challenge from ansible-core maintainer](https://forum.ansible.com/t/how-to-solve-ansible-challenge-from-ansible-core-maintainer/45285/1):
>
> This solution I do not like at all. It was very hard to write (I am not a jinja expert), especially get all the ‘-’ right (i had to use AI for that), otherwise result is full of unnecessary spaces. And complete solution falls apart with small changes.

([How to solve ansible challenge from ansible-core maintainer](https://forum.ansible.com/t/how-to-solve-ansible-challenge-from-ansible-core-maintainer/45285))

---

<div class="post-metadata">

**Author:** ![kks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kks/32/10352_2.png) [@kks](https://forum.ansible.com/u/kks)\
**Post date:** [October 1, 2026, 7:52am UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/7 "2026-10-01T07:52:15Z")

</div>

> [@Jonny](#):
>
> I heard dont use if statements

if statements are fine from my point of view, jinja is the problem, as this is a text engine, not data processing engine. Ansible filters are much more suitable to process data.

> [@Jonny](#):
>
> when not create your own

There are plenty of filters in ansible and they cover a lot of functionality, but sometimes you have to create your own. There is nothing inherently wrong about it.

For instance, some functionality might be missing in ansible:

[https://github.com/redhat-sap/rh\_operations/blob/galaxy/plugins/filter/combine\_default.py](https://github.com/redhat-sap/rh_operations/blob/galaxy/plugins/filter/combine_default.py)

Some functionality might be domain specific:

[https://github.com/redhat-sap/rh\_operations/blob/galaxy/plugins/filter/valid\_hostname.py](https://github.com/redhat-sap/rh_operations/blob/galaxy/plugins/filter/valid_hostname.py)

And sometimes you just create filters to understand how they work:

[https://github.com/redhat-sap/rh\_operations/blob/galaxy/plugins/filter/all.py](https://github.com/redhat-sap/rh_operations/blob/galaxy/plugins/filter/all.py)

Filters and test plugins are easy, you just have to start.

---

<div class="post-metadata">

**Author:** ![kks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kks/32/10352_2.png) [@kks](https://forum.ansible.com/u/kks)\
**Post date:** [October 1, 2026, 8:59am UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/8 "2026-10-01T08:59:33Z")

</div>

> [@Jonny](#):
>
> `myvar: "{{'value' if (my_group_var) else 'other_value'}}"`

BTW this is not jinja if, so this is “safe”  
This is jinja if

```yaml
 {%- if something -%} value {%- endif -%}

```

---

<div class="post-metadata">

**Author:** ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://forum.ansible.com/u/Jonny)\
**Post date:** [October 1, 2026, 9:28am UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/9 "2026-10-01T09:28:42Z")

</div>

Its not jinja? What is it called?

---

<div class="post-metadata">

**Author:** ![kks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kks/32/10352_2.png) [@kks](https://forum.ansible.com/u/kks)\
**Post date:** [October 1, 2026, 11:06am UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/10 "2026-10-01T11:06:00Z")

</div>

Sorry for using terminology so lightly. This is of course jinja.  
In my view one has to distinguish jinja variables.

```yaml
myvar: "{{'value' if (my_group_var) else 'other_value'}}"

```

From jinja blocks:

```yaml
 {%- if something -%} value {%- endif -%}

```

See

> **[API — Jinja Documentation (3.1.x)](https://jinja.palletsprojects.com/en/stable/api/#jinja2.Environment)**
>
> The high-level API is the API you will use in the application to load and render Jinja templates. The Low Level API on the other side is only useful if you want to dig deeper into Jinja or develop extensions.

> `block_start_string`
> 
> The string marking the beginning of a block. Defaults to `'{%'`.
> 
> `block_end_string`
> 
> The string marking the end of a block. Defaults to `'%}'`.
> 
> `variable_start_string`
> 
> The string marking the beginning of a print statement. Defaults to `'{{'`.
> 
> `variable_end_string`
> 
> The string marking the end of a print statement. Defaults to `'}}'`.

I loosly call the second one (block) just jinja, as ansible is not imaginable without the first one (variables).

---

<div class="post-metadata">

**Author:** ![Jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jonny](https://forum.ansible.com/u/Jonny)\
**Post date:** [October 1, 2026, 2:35pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/11 "2026-10-01T14:35:28Z")

</div>

> [@kks](#):
>
> loosly call the second one (block) just jinja, as ansible is not imaginable without the first one (variables).

Thanks for clarification!

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [October 1, 2026, 8:39pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/12 "2026-10-01T20:39:22Z")

</div>

@kks If you are having that much trouble with whitespace in jinja, you are definitely doing it wrong. I’m using jinja extensively in my code and never had any trouble with parsing of jinja output.

The solution is to not act too smart with jinja and reduce your code to:

```auto
{% do something and calculate <final_value> %}
{% set some_var = <final_value> %}
{{ some_var }}

```

You will have no problem with this pattern.

For simple data manipulation, this beats making custom plugin any time… at least complexity wise.

---

<div class="post-metadata">

**Author:** ![webfargo-dtoler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/webfargo-dtoler/32/11321_2.png) [@webfargo-dtoler](https://forum.ansible.com/u/webfargo-dtoler)\
**Post date:** [October 2, 2026, 8:40pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/13 "2026-10-02T20:40:23Z")

</div>

And with `ternary` you get the 3rd option for “unknown” values:  
`myvar: {{ my_group_var |ternary('value if true', 'value if false', 'value if unknown') }}`

---

<div class="post-metadata">

**Author:** ![kks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kks/32/10352_2.png) [@kks](https://forum.ansible.com/u/kks)\
**Post date:** [October 2, 2026, 9:03pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/14 "2026-10-02T21:03:44Z")

</div>

> [@webfargo-dtoler](#):
>
> 3rd option

I did not believe it unill I saw that with my own eyes. [https://github.com/ansible/ansible/blob/abf5bf59a229e0df2e15a421a5eff83a7b8e3796/lib/ansible/plugins/filter/core.py#L271](https://github.com/ansible/ansible/blob/abf5bf59a229e0df2e15a421a5eff83a7b8e3796/lib/ansible/plugins/filter/core.py#L271)

Thank you for sharing.

For whatever reason it is not documented [ansible.builtin.ternary filter – Ternary operation filter — Ansible Community Documentation](https://docs.ansible.com/projects/ansible/latest/collections/ansible/builtin/ternary_filter.html)

---

<div class="post-metadata">

**Author:** ![webfargo-dtoler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/webfargo-dtoler/32/11321_2.png) [@webfargo-dtoler](https://forum.ansible.com/u/webfargo-dtoler)\
**Post date:** [October 2, 2026, 9:19pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/15 "2026-10-02T21:19:45Z")

</div>

Odd about the filter documentation! It’s in the “Using filters” part of the Ansible community documentation, tho.

> **[Defining different values for true/false/null (ternary) - Using filters to...](https://docs.ansible.com/projects/ansible/latest/playbook_guide/playbooks_filters.html#defining-different-values-for-true-false-null-ternary)**
>
> Filters let you transform JSON data into YAML data, split a URL to extract the hostname, get the SHA1 hash of a string, add or multiply integers, and much more. You can use the Ansible-specific filters documented here to manipulate your data, or use...

Trivia: if you’re also a network nerd, ‘ternary’ is how the various CAM tables work on a LAN switch (access-lists, qos policies, others). For access-lists in particular, ‘ternary’ is how the software does a look-up “in hardware” on the ASICs for matching fields. Binary is one or the other; ternary is one or the other or else.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [October 2, 2026, 10:06pm UTC](https://forum.ansible.com/t/if-else-statement-at-vars/46381/16 "2026-10-02T22:06:01Z")

</div>

It is documented, scroll down

> **[key1=value1 - ansible.builtin.ternary filter – Ternary operation filter —...](https://docs.ansible.com/projects/ansible/latest/collections/ansible/builtin/ternary_filter.html#keyword-parameters)**
>
> Note
