# If Statement 1 Or Statement 2 In Jinja2 Template

**URL:** <https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395>\
**Category:** Get Help\
**Created:** [September 1, 2025, 10:56am UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395 "2025-09-01T10:56:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dulux-Oz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulux-oz/32/9047_2.png) [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Post date:** [September 1, 2025, 10:56am UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/1 "2025-09-01T10:56:12Z")

</div>

Hi All,

(I hope this is the correct place to post this.)

I’ve got the following statements in a Template (.j2) file, but it is obviously wrong. Could someone please point out where I am wrong and what the correct syntax is (I’ve Googled, but can’t seem to find anything - I swear, Google is getting worse and worse these days).

My aim is to have “Extra Stuff” included only if the host belongs to either Group1 _or_ Group2 (or both).

```auto
Stuff
{% if inventory_hostname in groups.group1 or inventory_hostname in groups.group2 %}
Large amount of Extra Stuff
{% endif %}
More Stuff

```

Thanks in advance

Dulux -Oz

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 1, 2025, 5:11pm UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/2 "2025-09-01T17:11:09Z")

</div>

I don’t see anything “obviously wrong” with this. Why do you believe it’s wrong?

---

<div class="post-metadata">

**Author:** ![Dulux-Oz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulux-oz/32/9047_2.png) [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Post date:** [September 2, 2025, 6:34am UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/3 "2025-09-02T06:34:55Z")

</div>

Hi @flowerysong,

I believe its wrong because ansible won’t compile/run the playbook which deals with that template, and the playbook was running fine _until_ I expanded the relevant line from `{% if inventory_hostname in groups.group1 %}` to `{% if inventory_hostname in groups.group1 or inventory_hostname in groups.group2 %}`. 😄

But like you, I can’t see anything “obviously wrong” either.

Cheers  
Dulux-Oz

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [September 2, 2025, 10:08am UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/4 "2025-09-02T10:08:55Z")

</div>

If I had this issue I’d try adding some debugging, for example:

```jinja2
The `inventory_hostname` is {{ inventory_hostname }}.

The `groups.group1` variable is of type: {{ groups.group1 | ansible.builtin.type_debug }}
and it has the value:

{{ groups.group1 | ansible.builtin.to_yaml }}

```

> [@Dulux-Oz](#):
>
> the playbook was running fine _until_ I expanded the relevant line from `{% if inventory_hostname in groups.group1 %}` to `{% if inventory_hostname in groups.group1 or inventory_hostname in groups.group2 %}`. 😄

Try adding some brackets?

```jinja2
{% if (inventory_hostname in groups.group1) or (inventory_hostname in groups.group2) %}

```

Or perhaps an extra `if` and brackets?

```jinja2
{% (if inventory_hostname in groups.group1) or (if inventory_hostname in groups.group2) %}

```

Ovbs I’m guessing at this point 😉

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 2, 2025, 11:11am UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/5 "2025-09-02T11:11:34Z")

</div>

I copy-n-pasted your template, changed `group1` and `group2` to names of groups we have, and it worked just fine.

Then I added a misspelling to my `group2` analog, and it fails.

I believe your inventory doesn’t include a `group2`, at least not the way you spelled it.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [September 2, 2025, 12:45pm UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/6 "2025-09-02T12:45:13Z")

</div>

So perhaps something like this might work?

```jinja2
{% ((if groups.group1 is defined) and
     (if inventory_hostname in groups.group1)) or
   ((if groups.group2 is defined) and
    (if inventory_hostname in groups.group2))
%}

```

🤷‍♀️

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 2, 2025, 2:14pm UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/7 "2025-09-02T14:14:53Z")

</div>

> [@Dulux-Oz](#):
>
> ansible won’t compile/run the playbook

This is not a useful description of the problem you’re encountering. If you want people to be able to help you, you need to provide some level of useful information (in this case, the text of the error output by Ansible when you try to run the playbook.)

> [@chris](#):
>
> So perhaps something like this might work?
> 
> ```auto
> {% ((if groups.group1 is defined) and
> (if inventory_hostname in groups.group1)) or
> ((if groups.group2 is defined) and
> (if inventory_hostname in groups.group2))
> %}
> 
> ```

It’s easier to handle that case by inverting the condition and checking against the list of groups that the current host is in.

```auto
{% if 'group1' in group_names or 'group2' in group_names %}

```

---

<div class="post-metadata">

**Author:** ![Dulux-Oz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulux-oz/32/9047_2.png) [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Post date:** [September 4, 2025, 9:48am UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/8 "2025-09-04T09:48:15Z")

</div>

Thanks for everyone for chipping in answers and suggestions.

The solution that I got working was to use the line: `{% if (inventory_hostname in groups.group1) or (inventory_hostname in groups.group2) %}`, which was originally missing the `(` `)`.

Thanks everyone  
Dulux-Oz

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 4, 2025, 12:03pm UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/9 "2025-09-04T12:03:09Z")

</div>

Glad you got something working, but the parentheses make no difference in that expression. You must have also fixed the underlying issue, which I’m guessing was a typo in a group name.

Either way, the better solution — the best suggestion offered in the thread — is the one from @flowerysong:

> [@flowerysong](#):
>
> It’s easier to handle that case by inverting the condition and checking against the list of groups that the current host is in.
> 
> ```auto
> {% if 'group1' in group_names or 'group2' in group_names %}
> 
> ```

More generally, if you have a bunch (more than two) groups to check, or if the list of relevant groups is already in a variable, it may be better to write the expression thus:

```auto
{% if ['group1',
       'group2',
       'group3',
       'group4',
       'group5''] | intersect(group_names) | length > 1 %}

```

or

```auto
{% if my_group_list | intersect(group_names) | length > 1 %}

```

---

<div class="post-metadata">

**Author:** ![Dulux-Oz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulux-oz/32/9047_2.png) [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Post date:** [September 5, 2025, 6:51am UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/10 "2025-09-05T06:51:59Z")

</div>

> …but the parentheses make no difference in that expression. You must have also fixed the underlying issue, which I’m guessing was a typo in a group name.

😄 That may be your belief (based on your experience, etc) but FTR, the only change made (anywhere in the Ansible ecosystem) was the addition of two `(` characters and two `)` characters in the locations specified - nothing, and I mean _nothing_ - else was changed anywhere (& I say that as someone who has been troubleshooting computers, software, and networks for over 40 years, so I _know_ how to isolate issues and test for them systematically - feel free to check out my Resume/Credentials if you have trouble accepting my statement(s)).

As a newcomer to Ansible I’m still getting tripped up on both semantics and also the best way to “skin the cat” - which is why I ask questions instead of wasting time floundering around. Having absorbed all of the above suggestions, I now agree with you and @flowerysong that his/her suggestion is the “best” way to do things and have implement such both in my initial issue and others, so thank you to both of you (and all the others) for all the help - it really is appreciated.

Until next time

Cheers  
Dulux-oz

---

<div class="post-metadata">

**Author:** ![anon81884396](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon81884396](https://forum.ansible.com/u/anon81884396)\
**Post date:** [September 5, 2025, 12:04pm UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/11 "2025-09-05T12:04:15Z")

</div>

The parenthesis are syntactic grouping. Usually they’re employed for doing complex conditions like, for example, `(2>3 or 3<4) and (mars is in retrograde or 2==1)`. The only difference of `{% if (inventory_hostname in groups.group1) or (inventory_hostname in groups.group2) %}` vs `{% if inventory_hostname in groups.group1 or inventory_hostname in groups.group2 %}` is readability; they both evaluate to the same thing. It is incorrect to boil this down to “a belief”. It would be like saying adding a comment or a newline to the end of a file made it work.

---

<div class="post-metadata">

**Author:** ![vampyreapocalypse](https://avatars.discourse-cdn.com/v4/letter/v/8e7dd6/32.png) [@vampyreapocalypse](https://forum.ansible.com/u/vampyreapocalypse)\
**Post date:** [September 5, 2025, 12:22pm UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/12 "2025-09-05T12:22:32Z")

</div>

It’s not so much a belief as a programming fact. The parentheses make no difference in that context. I’d love to see the git diff on that file to see what else was changed.

As a ‘newcomer’ to ansible you are also a newcomer to jinja templating, so you don’t have all the answers. Glad you decided to take some users’ advice and got the problem fixed. But “doing this for 40 years” isn’t the flex you think it is. No one in IT cares what you did in 1994, they care what you did in 2024…

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 5, 2025, 8:41pm UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/13 "2025-09-05T20:41:57Z")

</div>

> [@Dulux-Oz](#):
>
> the only change made (anywhere in the Ansible ecosystem) was the addition of two `(` characters and two `)` characters in the locations specified

🙂 It’s unfortunate you never posted the error you were getting. We’ve heard such claims before — sometimes from our own mouths.

I literally copy-n-pasted your expression from your first post

> [@Dulux-Oz](#):
>
> `{% if inventory_hostname in groups.group1 or inventory_hostname in groups.group2 %}`

Then defined an inventory with `group1` and `group2`, put my current machine into `group1` and ran it. It worked beautifully. Moved my machine from `group1` to `group2` and that worked as expected. Then I stuck a typo into the group names and it died with red text saying:

> fatal: [[tango.trailsong.org](http://tango.trailsong.org)]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable.. ‘dict object’ has no attribute ‘grooup1’\n\nThe error appears to be in ‘/home/utoddl/ansible/Dulux-Oz\_00.yml’: line 7, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: The test\n ^ here\n”}

Since you never posted the error you got, we’ve no way of knowing what the problem was, but it wasn’t the lack of parentheses.

I also have been making mistakes in this field professionally for 42 years. [Here’s a picture](https://forum.ansible.com/t/introduce-yourself-2023-edition/15/89) of my key-punch cards to prove it! Only now I can make mistakes on a scale we couldn’t imagine back then. But even then the refrain “I didn’t change anything else” was a clear sign that, well, you know… 🙂

Cheers!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 5, 2025, 8:42pm UTC](https://forum.ansible.com/t/if-statement-1-or-statement-2-in-jinja2-template/44395/14 "2025-10-05T20:42:20Z")

</div>

Closed on October 5, 2025, 8:42pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
