# Stop tasks from executing if any task fails?

**URL:** <https://forum.ansible.com/t/stop-tasks-from-executing-if-any-task-fails/21608>\
**Category:** Ansible Project\
**Created:** [May 31, 2016, 10:57am UTC](https://forum.ansible.com/t/stop-tasks-from-executing-if-any-task-fails/21608 "2016-05-31T10:57:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jahedh\_Mahmud](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jahedh\_Mahmud](https://forum.ansible.com/u/Jahedh_Mahmud)\
**Post date:** [May 31, 2016, 10:57am UTC](https://forum.ansible.com/t/stop-tasks-from-executing-if-any-task-fails/21608/1 "2016-05-31T10:57:14Z")

</div>

Hello

I’m new to Ansible and have been researching a way to stop a task from making a change if any of the tasks in the playbook fail.

Is there a way to do this? my playbook has three tasks, if the install Django fails i don’t want the first two to commit. But they are reported as ‘changed’ once the playbook finishes.

tasks:

- name: update

command: apt-get update

- name: install pip

apt: pkg=python-pip

- name: install django

command: pip install django

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 31, 2016, 2:09pm UTC](https://forum.ansible.com/t/stop-tasks-from-executing-if-any-task-fails/21608/2 "2016-05-31T14:09:49Z")

</div>

> I'm new to Ansible and have been researching a way to stop a task from  
> making a change if any of the tasks in the playbook fail.

Add a when-conditional to the second task, so that it only runs if the  
first one went fine.

> Is there a way to do this? my playbook has three tasks, if the install  
> Django fails i don't want the first two to commit. But they are reported as  
> 'changed' once the playbook finishes.

You can only skip later tasks, if one task fails. Also, tasks are done  
one after the other, so there is no easy way (at least I know none) to  
only run if all tasks would succeed.

> tasks:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: update
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: apt-get update

I would rather use the apt module here.  
[https://docs.ansible.com/apt\_module.html](https://docs.ansible.com/apt_module.html)

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: install pip
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apt: pkg=python-pip

Same here.

> &nbsp;&nbsp;&nbsp;&nbsp;- name: install django
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: pip install django

You could run this task with a conditional, so it is executed only, if  
the first two tasks went without errors.

If these are your three tasks, what reasons do you have for the first  
two not to commit, if the third fails? Updating the system and  
installing pip do not seem to be causing any pain.

Johannes

---

<div class="post-metadata">

**Author:** ![Jahedh\_Mahmud](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jahedh\_Mahmud](https://forum.ansible.com/u/Jahedh_Mahmud)\
**Post date:** [May 31, 2016, 3:21pm UTC](https://forum.ansible.com/t/stop-tasks-from-executing-if-any-task-fails/21608/3 "2016-05-31T15:21:27Z")

</div>

Thanks for the feedback!

I’ve amended the updates to use the apt module.

I have a couple of servers which i’m going to do a lot of installs on (django/gunicorn/celery/redis) and git pulls. So I need a sort of ‘out’ if any of them fail. Either that or a sort of roll-back, so I was hoping there was a way to do this through ansible.

Any help appreciated.

Thanks in advance.
