# Is there a way to act on a failed task

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-act-on-a-failed-task/13468>\
**Category:** Ansible Project\
**Created:** [September 13, 2013, 3:33pm UTC](https://forum.ansible.com/t/is-there-a-way-to-act-on-a-failed-task/13468 "2013-09-13T15:33:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steen\_Larsen](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Steen\_Larsen](https://forum.ansible.com/u/Steen_Larsen)\
**Post date:** [September 13, 2013, 3:33pm UTC](https://forum.ansible.com/t/is-there-a-way-to-act-on-a-failed-task/13468/1 "2013-09-13T15:33:10Z")

</div>

Hi,

I’m new to Ansible, and trying to find a way to write a file when a task fails. I’m cool with the play stopping so it’s not ignore\_fail I’m looking for but something like

when: fail command echo “sometext” \> /tmp/error

Is something like this possible, and if so, how ?

/Steen

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [September 13, 2013, 11:11pm UTC](https://forum.ansible.com/t/is-there-a-way-to-act-on-a-failed-task/13468/2 "2013-09-13T23:11:05Z")

</div>

Off the top of my head (and untested (and before coffee) ) ...

- name: command that fails  
&nbsp;&nbsp;shell: exit 1  
&nbsp;&nbsp;register: last\_command  
&nbsp;&nbsp;ignore\_errors: true

- name catch failed command  
&nbsp;&nbsp;shell: echo "Oops!"  
&nbsp;&nbsp;when: last\_command.rc != 0

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 14, 2013, 1:06am UTC](https://forum.ansible.com/t/is-there-a-way-to-act-on-a-failed-task/13468/3 "2013-09-14T01:06:47Z")

</div>

exactly!

---

<div class="post-metadata">

**Author:** ![Steen\_Larsen](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Steen\_Larsen](https://forum.ansible.com/u/Steen_Larsen)\
**Post date:** [September 16, 2013, 8:48am UTC](https://forum.ansible.com/t/is-there-a-way-to-act-on-a-failed-task/13468/4 "2013-09-16T08:48:47Z")

</div>

Thanks for the reply. If I do it like that however I would need to stop the play explicitly since i ignore the errors. Is there a way to do that ?

/Steen

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 16, 2013, 8:59am UTC](https://forum.ansible.com/t/is-there-a-way-to-act-on-a-failed-task/13468/5 "2013-09-16T08:59:03Z")

</div>

​Add a subsequent fail: task when: last\_command.rc != 0​

---

<div class="post-metadata">

**Author:** ![Steen\_Larsen](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Steen\_Larsen](https://forum.ansible.com/u/Steen_Larsen)\
**Post date:** [September 16, 2013, 10:24am UTC](https://forum.ansible.com/t/is-there-a-way-to-act-on-a-failed-task/13468/6 "2013-09-16T10:24:57Z")

</div>

Ah, thanks. Wasn’t aware of the fail task. That will do nicely.

/Steen
