# Avoid running pre/post tasks without roles/tasks

**URL:** <https://forum.ansible.com/t/avoid-running-pre-post-tasks-without-roles-tasks/14986>\
**Category:** Ansible Project\
**Created:** [April 10, 2014, 7:47am UTC](https://forum.ansible.com/t/avoid-running-pre-post-tasks-without-roles-tasks/14986 "2014-04-10T07:47:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [April 10, 2014, 7:47am UTC](https://forum.ansible.com/t/avoid-running-pre-post-tasks-without-roles-tasks/14986/1 "2014-04-10T07:47:24Z")

</div>

Hi all.

I have a master deployment playbook that more or less includes all of my servers, which I use with tags to deploy only the apps/configs I want at the time. Hosts are grouped into hostgroups by roles and each hostgroup has it’s own pre/post tasks for handling load-balancers, etc. It looks something like this:

- hosts: tag\_role\_hostgroup1  
serial: 1  
pre\_tasks:

- name: Remove from lb  
…  
tags: pre\_tasks  
roles:

- { role: app1, tags: app1 }

- { role: app2, tags: app2 }  
post\_tasks:

- name: Register to lb  
…  
tags: post\_tasks

- hosts: tag\_role\_hostgroup2  
serial: 1

pre\_tasks:

- name: Some preps  
…  
tags: pre\_tasks  
roles:
- { role: app2, tags: app2 }
- { role: app3, tags: app3 }  
post\_tasks:
- name: Cleanup  
…  
tags: post\_tasks

So far so good. It works pretty well as I can think about applications when deploying, and each hostgroup will take care of itself regarding load-balancers and all. Problem is when deploying for example app1 or app3. In this case one play will have no roles selected, but since I’m including “pre\_tasks,post\_tasks” as tags its pre/post tasks will still run, bringing machines down and up again with no work done.

Do you have an idea of how to tackle this? I prefer to not create a playbook per app as it’s a lot less powerful and flexible. I was thinking of making ansible run pre/post tasks only if some task in roles/tasks is going to run, but that’s kind of a last resort.

Thanks for Ansible, btw!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 15, 2014, 8:55pm UTC](https://forum.ansible.com/t/avoid-running-pre-post-tasks-without-roles-tasks/14986/2 "2014-04-15T20:55:34Z")

</div>

You could possibly consider passing ansible-playbook a --limit to only run on the servers you want to run on.

Others may have better suggestions.
