# Are there any try-catch/rollback mode or error handler?

**URL:** <https://forum.ansible.com/t/are-there-any-try-catch-rollback-mode-or-error-handler/13300>\
**Category:** Ansible Project\
**Created:** [August 14, 2013, 9:10am UTC](https://forum.ansible.com/t/are-there-any-try-catch-rollback-mode-or-error-handler/13300 "2013-08-14T09:10:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cristobal\_Rosa](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Cristobal\_Rosa](https://forum.ansible.com/u/Cristobal_Rosa)\
**Post date:** [August 14, 2013, 9:10am UTC](https://forum.ansible.com/t/are-there-any-try-catch-rollback-mode-or-error-handler/13300/1 "2013-08-14T09:10:22Z")

</div>

Hi guys!,

I would like to know if there are any ‘try - catch / rollback’ mode, I mean, I’m running a playbook with some tasks, when a given task crashes, I would like to run an external error recovery task ¿Can I do that?

Playbook example:

- hosts: someofthem  
user: myuser  
tasks:  
-name: First task  
action: whatever  
onerror: clean\_and\_exit #this could be task (or a handler)  
-name: Second task  
action: whatever  
onerror: clean\_and\_exit

…  
-name: clean\_and\_exit  
action: do something…

Thanks in advance

---

<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:** [August 14, 2013, 11:16am UTC](https://forum.ansible.com/t/are-there-any-try-catch-rollback-mode-or-error-handler/13300/2 "2013-08-14T11:16:15Z")

</div>

You can add “ignore\_errors” on a task and then use the “when” statement

- action: shell foo  
register: foo\_result  
ignore\_errors: True

- action: shell bar  
when: foo\_result | failed

(The " | failed " is basically a wrapper around saying “foo\_result.failed” but is also a bit more smart in some cases like when you get a list back)

---

<div class="post-metadata">

**Author:** ![Cristobal\_Rosa](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Cristobal\_Rosa](https://forum.ansible.com/u/Cristobal_Rosa)\
**Post date:** [August 14, 2013, 11:18am UTC](https://forum.ansible.com/t/are-there-any-try-catch-rollback-mode-or-error-handler/13300/3 "2013-08-14T11:18:03Z")

</div>

Great! This works perfect for me.  
Thanks you very much!
