# failed\_when - multiple conditions

**URL:** <https://forum.ansible.com/t/failed-when-multiple-conditions/16773>\
**Category:** Ansible Project\
**Created:** [November 13, 2014, 1:18pm UTC](https://forum.ansible.com/t/failed-when-multiple-conditions/16773 "2014-11-13T13:18:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![profhase85](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@profhase85](https://forum.ansible.com/u/profhase85)\
**Post date:** [November 13, 2014, 1:18pm UTC](https://forum.ansible.com/t/failed-when-multiple-conditions/16773/1 "2014-11-13T13:18:20Z")

</div>

Hi,  
when trying to install keys to a java keystore I am using the following task:

`

- name: java install ssl certs  
shell:  
source /etc/profile.d/java.sh;  
cd $JAVA\_HOME/jre/lib/security;  
$JAVA\_HOME/bin/keytool -import -noprompt -keystore cacerts -alias {{ item }} -file /tmp/{{ item }}.crt -storepass storepass  
register: command\_result  
changed\_when: “‘already exists’ not in command\_result.stdout”  
failed\_when: “‘already exists’ not in command\_result.stdout”

`

If the key has already been added, keytool returns ‘1’ and writes ‘…already exists’ to stdout.  
I would like the task not to fail either:

- if the key has been added  
or  
the key already exists.

I tried the condition  
` `  
failed\_when: “‘already exists’ not in command\_result.stdout and command\_result.rc != 0”

` `

`But that one does not work (`error while evaluating conditional).

Any ideas how to achieve this task?  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 17, 2014, 9:11pm UTC](https://forum.ansible.com/t/failed-when-multiple-conditions/16773/2 "2014-11-17T21:11:20Z")

</div>

Multiple checks looks like:

failed\_when: color == ‘blue’ or number == 5

Note that if you are telling when not to fail, you will need to negate your condition.

Ansible denotes lists of conditions as “ORs” so:

failed\_when:

- color == ‘blue’
- number == 5

Is equivalent syntax.

---

<div class="post-metadata">

**Author:** ![profhase85](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@profhase85](https://forum.ansible.com/u/profhase85)\
**Post date:** [November 18, 2014, 10:28am UTC](https://forum.ansible.com/t/failed-when-multiple-conditions/16773/3 "2014-11-18T10:28:28Z")

</div>

Okay thanks, maybe I got some problem with the semantics:

`  
failed\_when:

- “‘already installed’ not in command\_result.stderr”
- “command\_result.rc == 1”

`

> > Does not fail if either of the conditions is false ( so it does not fail if result.rc == 1 and ‘already installed’ is in stderr) which in this case is the desired behaviour.  
> > If one reads this task one could think that any of the list is a fail condition (so if one condition is true, everything fails), so this is not a real ‘OR’?  
> > Maybe there should be a module with ‘passed when:’ ?

Thanks

---

<div class="post-metadata">

**Author:** ![Andy\_Baker](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Andy\_Baker](https://forum.ansible.com/u/Andy_Baker)\
**Post date:** [May 3, 2015, 12:24pm UTC](https://forum.ansible.com/t/failed-when-multiple-conditions/16773/4 "2015-05-03T12:24:23Z")

</div>

Just hit a similar thing. I feel that ‘passed\_when’ or ‘succeeded\_when’ would make for simpler playbooks in several cases.

Also - it’s not clear from the docs what syntax is allowed fo failed\_when. Is it parsed as Python? i.e. any valid Python expression allowed?

---

<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:** [May 4, 2015, 4:42pm UTC](https://forum.ansible.com/t/failed-when-multiple-conditions/16773/5 "2015-05-04T16:42:20Z")

</div>

when: and all \*\_when: are parsed by jinja, so any valid jinja2  
expression should work. If the docs are not clear on this we do take  
PRs to make them better.

---

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [November 2, 2016, 10:06pm UTC](https://forum.ansible.com/t/failed-when-multiple-conditions/16773/6 "2016-11-02T22:06:45Z")

</div>

I just hit this issue (two years after you did) and the reply to my issue was that Ansible list syntax does an implicit AND on the multiple conditions, not OR, contradicting what Michael DeHaan wrote above earlier. I’m asking the Ansible project to document this somewhere, not obvious at all.

My issue submission for reference:  
[https://github.com/ansible/ansible/issues/18311](https://github.com/ansible/ansible/issues/18311)

Using your original example, re-formatting as follows should work:

failed\_when: \>  
‘already installed’ not in command\_result.stderr or  
command\_result.rc == 1
