# pre/post for roles and tasks

**URL:** <https://forum.ansible.com/t/pre-post-for-roles-and-tasks/12983>\
**Category:** Ansible Project\
**Created:** [June 19, 2013, 1:10am UTC](https://forum.ansible.com/t/pre-post-for-roles-and-tasks/12983 "2013-06-19T01:10:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sash\_Nagarkar](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@Sash\_Nagarkar](https://forum.ansible.com/u/Sash_Nagarkar)\
**Post date:** [June 19, 2013, 1:10am UTC](https://forum.ansible.com/t/pre-post-for-roles-and-tasks/12983/1 "2013-06-19T01:10:26Z")

</div>

Hello,

I see that pre\_tasks and post\_tasks exist for roles, where you can run individual tasks before or after completing all of the actions in a role. I also see that there is notify, which is for executing a handler if a task caused a change to the target system.

The former covers the case where the entire playbook is applied at once to a target, and the latter covers the case where I can restart services after making changes. However, I need to stop some services before executing a task, no matter how or why the task gets executed. After the task is complete, I need to start the previously stopped services again. How could I do this?

i.e.

…  
stop A  
stop B  
update something  
start B  
start A  
…

where “update something” is an individual task in one of my roles.

Thanks,  
Sash

---

<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:** [June 19, 2013, 2:36pm UTC](https://forum.ansible.com/t/pre-post-for-roles-and-tasks/12983/2 "2013-06-19T14:36:59Z")

</div>

- service: name=foo state=stopped
- service: name=foo state=started
