# Skip tag 'always' in a role if no tasks in that role are executed

**URL:** <https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179>\
**Category:** Ansible Developer\
**Created:** [April 15, 2016, 9:13pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179 "2016-04-15T21:13:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Strahinja\_Kustudic](https://forum.ansible.com/u/Strahinja_Kustudic)\
**Post date:** [April 15, 2016, 9:13pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/1 "2016-04-15T21:13:22Z")

</div>

For example if I have a playbook like this:

`  
// site.yml

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 18, 2016, 11:36pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/2 "2016-04-18T23:36:39Z")

</div>

–skip-tags always, but if you have any tasks tagged ‘always’ that overlap with the ‘postgres’ tag, they won’t run either.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Strahinja\_Kustudic](https://forum.ansible.com/u/Strahinja_Kustudic)\
**Post date:** [April 18, 2016, 11:52pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/3 "2016-04-18T23:52:34Z")

</div>

Which means there is no nice workaround to this problem. Do you think it would make sense to introduce a new special tag for this situation, or do you have a better suggestion?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 18, 2016, 11:56pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/4 "2016-04-18T23:56:28Z")

</div>

No workaround, but not sure that this merits a new directive as separating the plays can easily get around this limitation (and then including them in general playbook to use the other way).

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Strahinja\_Kustudic](https://forum.ansible.com/u/Strahinja_Kustudic)\
**Post date:** [April 19, 2016, 12:02am UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/5 "2016-04-19T00:02:50Z")

</div>

Splitting the whole site into smaller playbooks doesn’t help in this case, since I want to run a role on a whole site. I don’t want to think on which host/group the role is applied, I just want to apply it on all hosts which have it.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Strahinja\_Kustudic](https://forum.ansible.com/u/Strahinja_Kustudic)\
**Post date:** [April 19, 2016, 12:06am UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/6 "2016-04-19T00:06:16Z")

</div>

Also I don’t see making playbooks just based on roles, it’s possible to do it like that, but if you have a lot of hosts, it’s highly unlikely. One of the reasons you have roles is to easily use them in different plays.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [April 19, 2016, 2:32pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/7 "2016-04-19T14:32:56Z")

</div>

> Would it be possible to add something similar to roles, e.g. a special  
> tag always\_role or always\_with\_role, or whatever sounds better, so  
> that a task with that tag is run if any of the tasks in a role are  
> executed?

It seems like you could work around this by adding a tag like that  
yourself, and including that tag when you run Ansible. Change 'always' in  
your role to 'common\_vars' or whatever, and then run

&nbsp;&nbsp;ansible-playbook site.yml -t postgres,common\_vars

if you want the vars from the common role, and just drop the 'common\_vars'  
tag if you don't.

You have to remember to include the common\_vars tag every time you run  
with any other tag; if you want to include it most of the time, maybe  
create a shell function like

&nbsp;&nbsp;ansible-playbook () { /usr/bin/ansible-playbook $\* -t common\_vars ; }

and run /usr/bin/ansible-playbook when you don't want '-t common\_vars'  
automatically tacked on. (Or call the shell function something else, but  
if you don't want to have to remember to type something new, or whatever.)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Strahinja\_Kustudic](https://forum.ansible.com/u/Strahinja_Kustudic)\
**Post date:** [April 19, 2016, 5:44pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/8 "2016-04-19T17:44:14Z")

</div>

Hi Josh,

Thanks for the suggestion, but you missed the point which is probably because I didn’t explain it correctly. I don’t want to execute tasks from a ‘common’ role when I’m executing the ‘postgres’ role, I only want to execute the ‘postgres’ role.

My problem is that I would like to somehow tag some tasks in a role so that they are executed if any of the tasks in that role are also executed because of some tag in that role. E.g. I have a include\_vars task in a role and I have tasks in that role which are tagged with ‘postgres\_config’, I would like that the include\_vars task is run with that ‘postgres\_config’ tag and every possible tag in the postgres role, but I don’t want to update tags of that that include\_vars task every time I add a new tag in a role.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 19, 2016, 5:47pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/9 "2016-04-19T17:47:40Z")

</div>

if you don’t want tasks to run always … don’t use the always tag …

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@Strahinja\_Kustudic](https://forum.ansible.com/u/Strahinja_Kustudic)\
**Post date:** [April 19, 2016, 6:01pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/10 "2016-04-19T18:01:20Z")

</div>

I’ll do that anyway, just asking how to make a task run always if any task in a role is executing, but now that I think about it, we don’t have that many tags in roles anyway, so I’ll just list them all.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [April 19, 2016, 6:24pm UTC](https://forum.ansible.com/t/skip-tag-always-in-a-role-if-no-tasks-in-that-role-are-executed/21179/11 "2016-04-19T18:24:52Z")

</div>

> Thanks for the suggestion, but you missed the point which is probably  
> because I didn't explain it correctly. I don't want to execute tasks  
> from a 'common' role when I'm executing the 'postgres' role, I only  
> want to execute the 'postgres' role.

Right, I imagined you having two high-level use cases:

(1) You want to run just the 'postgres' role, and none of the tasks in the  
&nbsp;&nbsp;&nbsp;&nbsp;'common' role.

(2) You want to run things that include some of the tasks in the 'common'  
&nbsp;&nbsp;&nbsp;&nbsp;role -- and when you do, you want to run the include\_vars task as well.

So my suggestion was that if you tag the include\_vars task with a tag like  
'common\_vars', you can then get that task when you want it, and not when  
you don't -- you just have to add that tag when you do want it, as opposed  
to getting it always except when you don't want it. So e.g. if you wanted  
to run tasks in the 'common' role with some other tag, like 'common\_foo',  
that's when you'd do

&nbsp;&nbsp;ansible-playbook site.yml -t common\_foo,common\_vars

This is slightly more annoying than having the include\_vars task have an  
'always' tag, because if you forget, and leave off the 'common\_vars' tag,  
then it won't get run; that's the problem that the shell function idea was  
meant to work around.

Anyway, it sounds your other workaround should work as long as you don't  
mind having to put all of the other tags from all of the other tasks in  
the common role (all the common\_foo stuff) on the include\_vars task. But I  
think my suggestion would work if there were too many tags for that to be  
maintainable, so I figured I'd mention it (among other things, in case  
someone else comes across this thread in the future).

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
