Evaluating 'or' in when: conditional

40 views
Skip to first unread message

Barry Morrison

unread,
Jan 26, 2015, 10:04:36 PM1/26/15
to ansible...@googlegroups.com
I have a task with a
when: "'web' in group_names"

and it work as I expect it to, only running against servers in a web group. 

But now I want the conditional to match on web or db. 

I expected something like:

when: "'web' or 'db' in group_names"

would work, but it does not. Neither does

when: "'web' in group_names" or "'db' in group_names"

I've also tried most variations of the above. 

How can I get a task to only run if 'web' OR 'db' exist in the group_name? 

Thanks!

Barry Morrison

unread,
Jan 26, 2015, 11:16:11 PM1/26/15
to ansible...@googlegroups.com
I got some help via #ansible and was able to figure out what was happening.  Some insight:

- debug: msg="a web or db {{group_names}}"
 
when: '"web" in group_names or "db" in group_names'


TASK: [_nrpe | debug msg="a web or db {{group_names}}"] ****************************
skipping
: [web1.prod.domain.net]
skipping
: [web2.prod.domain.net]

TASK: [_nrpe | debug var=group_names] *****************************************
ok
: [web1.prod.domain.net] => {
 
"group_names": [
 
"prod-web"
 
]
}
ok
: [web2.prod.domain.net] => {
 
"group_names": [
 
"prod-web"
 
]
}


So it actually works when I have group_names as prod-web and I match on when: "'web' in group_names' but it doesn't work when I have [prod|build]-web so I leveraged groups of groups and created a [web:children] with prod-web and dev-web as a part of the group, then the match on 'web' works. 
Reply all
Reply to author
Forward
0 new messages