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

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

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.

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

- ansible_net_version is version('16.12.06', '>=') and
ansible_net_version not in ('17.03.02', 17.03.03', ... )