# When Or statement

**URL:** <https://forum.ansible.com/t/when-or-statement/34239>\
**Category:** Ansible Project\
**Created:** [November 25, 2020, 3:44pm UTC](https://forum.ansible.com/t/when-or-statement/34239 "2020-11-25T15:44:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [November 25, 2020, 3:44pm UTC](https://forum.ansible.com/t/when-or-statement/34239/1 "2020-11-25T15:44:41Z")

</div>

In an ansible play, is “or” not supported with conditional statements? I have a task that is searching a variable for 2 values. The when statements work alone, but with “or” it runs but doesn’t do what I am expecting. Here is the task, is my syntax wrong or is it just not supported? -

- name: Apply policy to the interfaces  
cisco.ios.ios\_config:  
parents: interface {{ item.key }}  
lines:
- device-tracking attach-policy IPDT\_POLICY  
loop: “{{ netbrain\_interface\_attributes|dict2items }}”  
when: \>  
item.key is not search(“Port”)  
or item.key is not search(“Vlan”)

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 25, 2020, 4:07pm UTC](https://forum.ansible.com/t/when-or-statement/34239/2 "2020-11-25T16:07:37Z")

</div>

> In an ansible play, is "or" not supported with conditional statements? I have a task that is searching a variable for 2  
> values. The when statements work alone, but with "or" it runs but doesn't do what I am expecting. Here is the task, is  
> my syntax wrong or is it just not supported? -
> 
> ```
> \- name: Apply policy to the interfaces
> cisco\.ios\.ios\_config:
> parents: interface \{\{ item\.key \}\}
> lines:
> \- device\-tracking attach\-policy IPDT\_POLICY
> loop: &quot;\{\{ netbrain\_interface\_attributes|dict2items \}\}&quot;
> when: &gt;
> item\.key is not search\(&quot;Port&quot;\)
> or item\.key is not search\(&quot;Vlan&quot;\)
> 
> ```

Maybe you need ( ) around both conditions, but you can also use (untested)

&nbsp;&nbsp;&nbsp;item.key is not regex("(Port|Vlan)")

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [November 25, 2020, 5:42pm UTC](https://forum.ansible.com/t/when-or-statement/34239/3 "2020-11-25T17:42:32Z")

</div>

I apologize I did miss a line in my task when I copied. So I used the (Port|Vlan). Which again works as a single statement. Also tried putting the statements in ()

- name: Apply policy to the interfaces  
cisco.ios.ios\_config:  
parents: interface {{ item.key }}  
lines:

- device-tracking attach-policy IPDT\_POLICY  
loop: “{{ netbrain\_interface\_attributes|dict2items }}”  
when: \>  
item.key is not regex(“(Port|Vlan)”)  
or item.key not in po\_ints

- name: Apply policy to the interfaces  
cisco.ios.ios\_config:  
parents: interface {{ item.key }}  
lines:

- device-tracking attach-policy IPDT\_POLICY  
loop: “{{ netbrain\_interface\_attributes|dict2items }}”  
when: \>  
(item.key is not regex(“(Port|Vlan)”))  
or (item.key not in po\_ints)

---

<div class="post-metadata">

**Author:** ![Jason](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jason/32/304_2.png) [@Jason](https://forum.ansible.com/u/Jason)\
**Post date:** [November 30, 2020, 3:14pm UTC](https://forum.ansible.com/t/when-or-statement/34239/4 "2020-11-30T15:14:23Z")

</div>

Hello,

Just thought I would uypdate. I was not able to find the correct syntax for an or in a when statement. What I end up doing, and I am sure there is a better way to do it, was create separate lists then adding them together.

- name: Create a list without Gigabit interfaces  
set\_fact:  
exclude1: “{{ ex | reject(‘search’, ‘Giga’) | list }}”

- name: Create a list of GigabitEthernet0/0  
set\_fact:  
exclude2: “{{ ex | select(‘search’, ‘GigabitEthernet0/0’) | list }}”

- name: Gather the Port Channel Summary  
ansible.netcommon.cli\_parse:  
command: “show etherchannel summary”  
parser:  
name: ansible.netcommon.pyats  
set\_fact: eth\_sum

- name: Get the PO Channel Interfaces  
set\_fact:  
po\_ints: “{{ eth\_sum.interfaces|json\_query(query)|  
flatten }}”  
cacheable: yes  
vars:  
query: ‘\*.port\_channel.port\_channel\_member\_intfs’

- name: Create one list to be used in a future play  
set\_fact:  
exclude\_interfaces: “{{ exclude1 + exclude2 + po\_ints }}”  
cacheable: yes

---

<div class="post-metadata">

**Author:** ![James\_Lagermann](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@James\_Lagermann](https://forum.ansible.com/u/James_Lagermann)\
**Post date:** [December 1, 2020, 4:17pm UTC](https://forum.ansible.com/t/when-or-statement/34239/5 "2020-12-01T16:17:39Z")

</div>

Have you tried to put the line in double quotes? I’m not sure if it’s always required, however, some of my ‘or’ statements will not work without them so I just put all of them in double quotes.

when: “(item.key is not regex(”(Port|Vlan)“)) or (item.key not in po\_ints)”

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 1, 2020, 4:36pm UTC](https://forum.ansible.com/t/when-or-statement/34239/6 "2020-12-01T16:36:37Z")

</div>

> Have you tried to put the line in double quotes? I'm not sure if it's always required, however, some of my 'or'  
> statements will not work without them so I just put all of them in double quotes.
> 
> ```
> when: &quot;\(item\.key is not regex\(&quot;\(Port|Vlan\)&quot;\)\) or \(item\.key not in po\_ints\)&quot;
> 
> ```

That might work, but in this case you need to use single quotes for the regex:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "(item.key is not regex('(Port|Vlan)')) or (item.key not in po\_ints)"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
