# Need assistance in using 'AND' and 'OR' condition together

**URL:** <https://forum.ansible.com/t/need-assistance-in-using-and-and-or-condition-together/35273>\
**Category:** Ansible Project\
**Created:** [June 2, 2021, 7:31pm UTC](https://forum.ansible.com/t/need-assistance-in-using-and-and-or-condition-together/35273 "2021-06-02T19:31:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikram\_S](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vikram\_S](https://forum.ansible.com/u/Vikram_S)\
**Post date:** [June 2, 2021, 7:31pm UTC](https://forum.ansible.com/t/need-assistance-in-using-and-and-or-condition-together/35273/1 "2021-06-02T19:31:21Z")

</div>

I have written below script that will end the playbook if IOS version running on router satisfies below conditions:

1. Version is greater than or is 16.12.02 AND

2. Version is NOT certain versions like 16.12.05 OR 16.09.06 OR 17.03.02’, etc as mentioned below.

But script does not work. When i run this for a device with version 16.12.05, script fails though it shouldn’t be as per the condition.

But when i test the ‘AND’ and ‘OR’ condition separately, they work. It’s just that when i combine them, it doesn’t seem to. What should i change?

tasks:

- name: GATHER DEVICE FACTS

ios\_facts:

- name: END THIS PLAYBOOK IF DEVICE IS ALREADY RUNNING NEW IMAGE OR HIGHER VERSION THAN THAT

fail:

msg: DEVICE IS ALREADY RUNNING NEW IMAGE OR HIGHER VERSION THAN THAT. HENCE UPGRADE IS NOT NECESSARY

when:

- ansible\_net\_version is version(‘16.12.06’, ‘\>=’)

- "ansible\_net\_version is version(‘16.12.05’, ‘!=’) or

ansible\_net\_version is version(‘16.09.06’, ‘!=’) or

ansible\_net\_version is version(‘17.03.02’, ‘!=’) or

ansible\_net\_version is version(‘17.03.03’, ‘!=’) or

ansible\_net\_version is version(‘17.05.01’, ‘!=’) or

ansible\_net\_version is version(‘17.07.01’, ‘!=’)"

I modified OR syntax and tried below syntax but didn’t work:

- name: END THIS PLAYBOOK IF DEVICE IS ALREADY RUNNING NEW IMAGE OR HIGHER VERSION THAN THAT

fail:

msg: DEVICE IS ALREADY RUNNING NEW IMAGE OR HIGHER VERSION THAN THAT. HENCE UPGRADE IS NOT NECESSARY

when:

- ansible\_net\_version is version(‘16.12.04’, ‘\>=’)

- (ansible\_net\_version is version(‘16.12.05’, ‘!=’) or ansible\_net\_version is version(‘16.09.06’, ‘!=’) or ansible\_net\_version is version(‘17.03.02’, ‘!=’) or ansible\_net\_version is version(‘17.03.03’, ‘!=’))

Thanks,

Vikram

---

<div class="post-metadata">

**Author:** ![Antony\_Stone](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Antony\_Stone](https://forum.ansible.com/u/Antony_Stone)\
**Post date:** [June 2, 2021, 7:47pm UTC](https://forum.ansible.com/t/need-assistance-in-using-and-and-or-condition-together/35273/2 "2021-06-02T19:47:41Z")

</div>

> I have written below script that will end the playbook if IOS version  
> running on router satisfies below conditions:
> 
> &nbsp;&nbsp;&nbsp;1. Version is greater than or is 16.12.02 AND  
> &nbsp;&nbsp;&nbsp;2. Version is NOT certain versions like 16.12.05 OR 16.09.06 OR  
> &nbsp;&nbsp;&nbsp;17.03.02', etc as mentioned below.
> 
> But script does not work. When i run this for a device with version  
> 16.12.05, script fails though it shouldn't be as per the condition.

I think you have = and != mixed up.

> But when i test the 'AND' and 'OR' condition separately, they work. It's  
> just that when i combine them, it doesn't seem to. What should i change?

Let's work through your conditions when version = 16.12.05...

> tasks:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: GATHER DEVICE FACTS  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ios\_facts:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: END THIS PLAYBOOK IF DEVICE IS ALREADY RUNNING NEW IMAGE OR  
> HIGHER VERSION THAN THAT  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail:

So, you want it to fail when something below becomes true

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: DEVICE IS ALREADY RUNNING NEW IMAGE OR HIGHER VERSION THAN  
> THAT. HENCE UPGRADE IS NOT NECESSARY  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ansible\_net\_version is version('16.12.06', '\>=')

Not true - version = 16.12.05

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "ansible\_net\_version is version('16.12.05', '!=') or

Not true - version = 16.12.05, so "version != 16.12.05" is false

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_net\_version is version('16.09.06', '!=') or

True - version = 16.12.05, therefore != 16.09.06

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_net\_version is version('17.03.02', '!=') or  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_net\_version is version('17.03.03', '!=') or  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_net\_version is version('17.05.01', '!=') or  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_net\_version is version('17.07.01', '!=')"

One of the conditions is true, therefore script fails.

I \*think\* you want to replace "!=" with "=" so that failure occurs only if the  
version does match one of the strings you have listed.

I might be wrong, and you want to keep "!=" but change "or" to "and" so that  
failure occurs only if version is not equal to any of the listed strings.

Either way, I think what you have written will always end up being "true".

Antony.

---

<div class="post-metadata">

**Author:** ![Vikram\_S](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vikram\_S](https://forum.ansible.com/u/Vikram_S)\
**Post date:** [June 3, 2021, 6:43pm UTC](https://forum.ansible.com/t/need-assistance-in-using-and-and-or-condition-together/35273/3 "2021-06-03T18:43:22Z")

</div>

Hi Antony,

On second thought, you are right in that condition will always end up being “true”. I guess my requirement can never be written as condition in ansible.

Regards,  
Vikram

---

<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:** [June 3, 2021, 6:57pm UTC](https://forum.ansible.com/t/need-assistance-in-using-and-and-or-condition-together/35273/4 "2021-06-03T18:57:07Z")

</div>

- ansible\_net\_version is version('16.12.06', '\>=') and  
ansible\_net\_version not in ('17.03.02', 17.03.03', ... )
