# How to rollback the ansible-playbook tasks ?

**URL:** <https://forum.ansible.com/t/how-to-rollback-the-ansible-playbook-tasks/30673>\
**Category:** Ansible Project\
**Created:** [June 27, 2019, 5:05pm UTC](https://forum.ansible.com/t/how-to-rollback-the-ansible-playbook-tasks/30673 "2019-06-27T17:05:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefano\_Leandro](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stefano\_Leandro](https://forum.ansible.com/u/Stefano_Leandro)\
**Post date:** [June 27, 2019, 5:05pm UTC](https://forum.ansible.com/t/how-to-rollback-the-ansible-playbook-tasks/30673/1 "2019-06-27T17:05:24Z")

</div>

Hi Folks,

I would to ask some suggestions for controlling a state rollback of ansible-plyabook tasks .

Are there some best practice to follow ?

Thank you,

Steano L.

---

<div class="post-metadata">

**Author:** ![shivharsh.nec](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@shivharsh.nec](https://forum.ansible.com/u/shivharsh.nec)\
**Post date:** [July 3, 2019, 9:30am UTC](https://forum.ansible.com/t/how-to-rollback-the-ansible-playbook-tasks/30673/2 "2019-07-03T09:30:58Z")

</div>

Hi,

Ansible does not have a mechanism to keep track of changes and automatically perform a rollback. However, there are a couple of ways you could use to fulfill your requirement:

- **Using blocks and rescue:** Ansible by default stops playbook execution on a task failure. However, It also provides a mechanism to perform error handling and execute some task in response to a failure. The block section is a logical grouping of all the desired tasks to be executed on a managed host and the rescue section contains the tasks to be executed to recover from the error encountered in block section.

Something like below:

`

- name: Attempt and graceful roll back demo  
block:
- name: i force a failure  
command: /bin/false  
rescue:
- debug:  
msg: ‘I caught an error, performing rollback’  
command: /bin/false  
`

The only disadvantage of using blocks is that rollback would only be limited to the hosts on which the task has failed in block section. You may refer to the [link](https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html) for more details.

- **Writing an explicit rollback play:** If you wish to perform a global rollback, that is on all the hosts, then you need to explicitly write a play with rollback steps and execute it based on some when condition.

Something similar to the below playbook:  
`
