# Multiple and/or conditions in when wont work

**URL:** <https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855>\
**Category:** Ansible Project\
**Created:** [June 29, 2017, 9:00pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855 "2017-06-29T21:00:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [June 29, 2017, 9:00pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/1 "2017-06-29T21:00:23Z")

</div>

I am having issues with using multiple conditions in when to validate whether or not to run a task.

Basically I am using extrended variables within the ansible command to state what sort of updates I want to run like this:  
ansible-playbook site.yml -i inventory --ask-vault -u (username)-e “security=true restart=true” -k -K

- name: Update all packages  
yum:  
name: “_"  
state: latest  
exclude: "kernel_”  
when: security is not defined or kernel is not defined or specified\_packages is not defined and ansible\_os\_family == “RedHat”

However I can not figure out a combination to get this conditional to run properly. Everytime I run it the playbook continues to run the task when I dont want it to. I already experimented with adding another variable to specify if I want to skip this task only but I would prefer it to automatically get skipped when I specify to run another type of update.

I’ve tried the following:

(security is not defined or kernel is not defined or specified\_packages is not defined) and (ansible\_os\_family == “RedHat”)

(security is not defined or kernel is not defined or specified\_packages is not defined) and ansible\_os\_family == “RedHat”

or even:

when: ansible\_os\_family == “RedHat”  
when: security is not defined or kernel is not defined or specified\_packages is not defined

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 30, 2017, 6:17am UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/2 "2017-06-30T06:17:04Z")

</div>

> I am having issues with using multiple conditions in when to validate  
> whether or not to run a task.
> 
> Basically I am using extrended variables within the ansible command to  
> state what sort of updates I want to run like this:  
> ansible-playbook site.yml -i inventory --ask-vault -u (username)-e  
> "security=true restart=true" -k -K
> 
> - name: Update all packages  
> &nbsp;&nbsp;&nbsp;yum:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "\*"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: latest  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exclude: "kernel\*"  
> &nbsp;&nbsp;&nbsp;when: security is not defined or kernel is not defined or  
> specified\_packages is not defined and ansible\_os\_family == "RedHat"
> 
> However I can not figure out a combination to get this conditional to run  
> properly. Everytime I run it the playbook continues to run the task when I  
> dont want it to.

When do you want the task to run?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 30, 2017, 11:54am UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/3 "2017-06-30T11:54:02Z")

</div>

Multiple AND/OR do work, are you sure your conditions are being met?

---

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [June 30, 2017, 5:05pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/4 "2017-06-30T17:05:58Z")

</div>

i want to run the task when ansible\_os\_family == “RedHat” (this works) AND if none of the following variables are set (security,kernel,specified\_packages) through extended variables (-e)

---

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [June 30, 2017, 5:10pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/5 "2017-06-30T17:10:18Z")

</div>

Im posting my stack overflow question as it is much more clear:

[Ask Question](https://stackoverflow.com/questions/ask)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 30, 2017, 6:47pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/6 "2017-06-30T18:47:35Z")

</div>

Your last three posted have different explanation of what you are trying to achieve, but according to this post I think you are looking for this

when: not (security is defined or kernel is defined or specified\_packages is defined) and ansible\_os\_family == "RedHat"

This only run on RedHat when none of the variables is set.

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [June 30, 2017, 7:38pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/7 "2017-06-30T19:38:12Z")

</div>

You could use the intersect filter from jinja2.

I am thinking about sth. like the following:

when: ansible\_os\_family == “RedHat” and not vars.keys() | intersect([security,kernel,specified\_packages])

---

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [June 30, 2017, 8:31pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/8 "2017-06-30T20:31:04Z")

</div>

My bad I was working with many different solutions and combinations so I got confused. I tried tons of complicated ways … can’t believe such a simple one worked lol. I probably over complicated it but what you suggested works great!

when: not (security is defined or kernel is defined or  
specified\_packages is defined) and ansible\_os\_family == “RedHat”

Thanks!

---

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [June 30, 2017, 8:39pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/9 "2017-06-30T20:39:45Z")

</div>

This doesn’t work as if I put the second value (in this case kernel) it tries to check it throws an error saying security is undefined. I found that jinja2 templates dont seem to work in the when clause. I tried many jinja2 templates that should work in regular situations but they failed (e.g when: security|d(‘’) = ’ ’ …etc or even when: security | default(‘false’) = false). Kai’s response works the best although I found a workaround to use false/true flags.

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [July 2, 2017, 5:45pm UTC](https://forum.ansible.com/t/multiple-and-or-conditions-in-when-wont-work/24855/10 "2017-07-02T17:45:36Z")

</div>

The jinja2 template, does work in the when clause.  
In fact, you dont even need the curly brakets.

My example gave you an error because i missed the quotes around the items. The items arent variables, these are strings. So you cant get an “is not defined” with that code!

The following code skips the task:

- hosts: all

vars:  
var1: True  
security: Hello

tasks:

- name: This is a test task  
debug:  
msg: Yo  
when: var1 and not vars.keys() | intersect([“security”,“kernel”,“specified\_packages”])

This code runs it:

- hosts: all

vars:  
var1: True

tasks:

- name: This is a test task  
debug:  
msg: Yo  
when: var1 and not vars.keys() | intersect([“security”,“kernel”,“specified\_packages”])
