When conditional not working when i use 'OR' for multiple confitions

22 views
Skip to first unread message

Vikram S

unread,
Mar 19, 2021, 6:53:59 AM3/19/21
to Ansible Project

I have wrriten below script to check if switch is in state mentioned in WHEN condition and to end playbook if so. But when condition doesn't work when i use 'OR' conditional to check for multiple conditions.

I tried different ways of writing the OR syntax as given below but didn't work. What am i doing wrong?

SYNTAX 1:

name: GATHER STACK INFO

ios_command: 
 commands: "show switch" 

 register: print_showswitch 

 - name: END THIS PLAYBOOK IF ALL MEMBER SWITCHES ARE NOT IN READY STATE
meta: end_host 
 when: - "'Waiting' in print_showswitch.stdout[0]" or 
             - "'Progressing' in print_showswitch.stdout[0]" or 
             - "'Initializing' in print_showswitch.stdout[0]"


SYNTAX 2:

when:

- ("'Waiting' in print_showswitch.stdout[0]") or ("'Progressing' in print_showswitch.stdout[0]")


SYNTAX 3:

when:

- ("'Waiting' in print_showswitch.stdout[0]" or "'Progressing' in print_showswitch.stdout[0]")

Thanks,
Vikram

Antony Stone

unread,
Mar 19, 2021, 6:57:41 AM3/19/21
to ansible...@googlegroups.com
On Friday 19 March 2021 at 11:53:59, Vikram S wrote:

> - name: END THIS PLAYBOOK IF ALL MEMBER SWITCHES ARE NOT IN READY STATE

I find this ambiguous.

Does it mean that you want to end the playbook if every switch is in some
state state other than ready (so, any switch being ready means the playbook
goes ahead), or does it mean that you want to end the playbook if not all
switches are ready (some are, but not all)?


Antony.

--
"How I managed so long without this book baffles the mind."

- Richard Stoakley, Group Program Manager, Microsoft Corporation,
referring to "The Art of Project Management", O'Reilly press

Please reply to the list;
please *don't* CC me.

Brian Coca

unread,
Mar 19, 2021, 10:34:53 AM3/19/21
to Ansible Project
The - is an implicit AND so you are doing X OR AND Y OR AND Z.

remove the - and make it all one string:

when: - "'Waiting' in print_showswitch.stdout[0]" or
'Progressing' in print_showswitch.stdout[0]" or
'Initializing' in print_showswitch.stdout[0]"



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

Vikram S

unread,
Mar 19, 2021, 12:56:38 PM3/19/21
to Ansible Project
Hi Anthony,

Yes, i want to end the playbook if every member switch in a stack is in some state state other than ready. I am writing script for IOS upgrade and every switch needs to be in READY state to proceed otherwise it's going to cause problem.

Regards,
Vikram

Antony Stone

unread,
Mar 19, 2021, 1:38:12 PM3/19/21
to ansible...@googlegroups.com
On Friday 19 March 2021 at 17:56:38, Vikram S wrote:

> Hi Antony,
>
> Yes, i want to end the playbook if every member switch in a stack is in
> some state state other than ready. I am writing script for IOS upgrade and
> every switch needs to be in READY state to proceed otherwise it's going to
> cause problem.

Now I am even more confused.

First you say: "I want to end the playbook if every member switch in a stack
is in some state state other than ready."

So, every switch must be in a state other than ready, for the playbook to end.

One switch in ready state means this is not true, so the playbook continues.

Then you say instead: "every switch needs to be in READY state to proceed
otherwise it's going to cause problem"

So, now every switch must be in ready state for the playbook to proceed.

Any switch which is not in ready state causes the playbook to fail.


The two statements do not match, and it's important that you are clear about
what logic you are trying to implement in order to get the syntax right.

I *think* you probably intend the second state - all switches must be ready,
otherwise you stop, but they're not my switches :)


Antony.

--
This email was created using 100% recycled electrons.

Vikram S

unread,
Mar 19, 2021, 3:24:13 PM3/19/21
to Ansible Project
Hi Anthony,

Sorry for the confusion. I mean that 'any' switch and not 'every' switch.

As i said earlier, every switch needs to be in READY state to proceed otherwise it's going to cause problem. So i have written the script to check even if 1 switch in the stack is in a state other than READY, then playbook has to end.

Regards,
Vikram

Deepthi uta

unread,
Mar 20, 2021, 1:48:03 AM3/20/21
to ansible...@googlegroups.com
Hi  
 cloud you help me how to write ansible playbooks for tags, which were used in rds database in aws cloud. 

Thanks & regards,


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/395a7793-3e36-4a59-80fe-4ee2c5886bf2n%40googlegroups.com.

Vikram S

unread,
Mar 22, 2021, 1:38:11 AM3/22/21
to Ansible Project
Hi,

I can write a script using tags but don't have knowledge about AWS. I have written scripts only for IOS and ASA.

Regards,
Vikram

Reply all
Reply to author
Forward
0 new messages