# using failed\_when: with changed\_when:

**URL:** <https://forum.ansible.com/t/using-failed-when-with-changed-when/17284>\
**Category:** Ansible Project\
**Created:** [January 15, 2015, 7:19pm UTC](https://forum.ansible.com/t/using-failed-when-with-changed-when/17284 "2015-01-15T19:19:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [January 15, 2015, 7:19pm UTC](https://forum.ansible.com/t/using-failed-when-with-changed-when/17284/1 "2015-01-15T19:19:05Z")

</div>

Hello list,

Do ‘failed\_when:’ and ‘changed\_when:’ effect one another?

(i.e. a failed task returns changed instead of failed because you’d specified both flags or vice versa?)

I guess I’m assuming that in any case ‘failed’ supersedes ‘changed’, and these flags just change the definition of those conditions and not their precedence(?).

Thank you,  
Mark

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 15, 2015, 8:24pm UTC](https://forum.ansible.com/t/using-failed-when-with-changed-when/17284/2 "2015-01-15T20:24:40Z")

</div>

Well,

I just tried and tested both:

- name: run python script to make ip static  
script: …/files/gce\_setip.py -a {{ item.public\_ip }} -n {{ ipaddrname }} -z {{ zone }}  
register: res  
failed\_when: “res.rc == 2”  
changed\_when: “res.rc == 0”  
with\_items: gce.instance\_data

and it seems to work or at least I hope so …

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [January 18, 2015, 5:07pm UTC](https://forum.ansible.com/t/using-failed-when-with-changed-when/17284/3 "2015-01-18T17:07:25Z")

</div>

Thanks; I think I was a little vague, sorry. I’m actually already using them together to evaluate the return output of a script and determine what task state to return, and I wanted to just remove the ‘failed\_when:’ line and let Ansible rely on the return code of the script being called instead of looking at the text it returns. But I wanted to leave the changed\_when looking at the output.

I was initially (probably over-thinking it and) curious if there were any corner cases where that could effect if the task still used ‘changed\_when:’ as I wanted it to, but having thought about it for a while I’m 99.9% sure that the only time my ‘changed\_when:’ will get overridden is when the task actually fails according to the return code. And in that case I obviously would want the failed status to supersede whether or not the changed\_when matched.

---

<div class="post-metadata">

**Author:** ![Mumshad\_Mannambeth](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Mumshad\_Mannambeth](https://forum.ansible.com/u/Mumshad_Mannambeth)\
**Post date:** [November 30, 2016, 9:18pm UTC](https://forum.ansible.com/t/using-failed-when-with-changed-when/17284/4 "2016-11-30T21:18:55Z")

</div>

I would like to set changed\_when = not failed\_when or vice versa. Is that possible?
