Stop tasks from executing if any task fails?

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.

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:

     - name: update

       command: apt-get update

I would rather use the apt module here.
https://docs.ansible.com/apt_module.html

     - name: install pip

       apt: pkg=python-pip

Same here.

    - name: install django

       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

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.