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

24 views
Skip to first unread message

Vikram S

unread,
Jun 2, 2021, 3:31:22 PM6/2/21
to Ansible Project

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

Antony Stone

unread,
Jun 2, 2021, 3:48:08 PM6/2/21
to ansible...@googlegroups.com
On Wednesday 02 June 2021 at 21:31:21, Vikram S wrote:

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

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:
>
> - name: GATHER DEVICE FACTS
> ios_facts:
> - name: END THIS PLAYBOOK IF DEVICE IS ALREADY RUNNING NEW IMAGE OR
> HIGHER VERSION THAN THAT
> fail:

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

> 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', '>=')

Not true - version = 16.12.05

> - "ansible_net_version is version('16.12.05', '!=') or

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

> ansible_net_version is version('16.09.06', '!=') or

True - version = 16.12.05, therefore != 16.09.06

> 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', '!=')"

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.

--
There are two possible outcomes:

If the result confirms the hypothesis, then you've made a measurement.
If the result is contrary to the hypothesis, then you've made a discovery.

- Enrico Fermi

Please reply to the list;
please *don't* CC me.
Message has been deleted

Vikram S

unread,
Jun 3, 2021, 2:43:23 PM6/3/21
to Ansible Project
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

Brian Coca

unread,
Jun 3, 2021, 2:57:51 PM6/3/21
to Ansible Project
- ansible_net_version is version('16.12.06', '>=') and
ansible_net_version not in ('17.03.02', 17.03.03', ... )



--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages