# Need help with tags

**URL:** <https://forum.ansible.com/t/need-help-with-tags/34678>\
**Category:** Ansible Project\
**Created:** [February 24, 2021, 4:15pm UTC](https://forum.ansible.com/t/need-help-with-tags/34678 "2021-02-24T16:15:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mouton\_Noir](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mouton\_Noir](https://forum.ansible.com/u/Mouton_Noir)\
**Post date:** [February 24, 2021, 4:15pm UTC](https://forum.ansible.com/t/need-help-with-tags/34678/1 "2021-02-24T16:15:48Z")

</div>

Hi,

I’d like to use tags to select tasks I launch.  
I found documentations about how to limit playbook, role… execution to the tagged tasks l choose from adhoc command (-t option).

But I need to do the same from another playbook… and I can’t find anything about that

ex :  
extract from the main.yml from role “myrole” :

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 24, 2021, 4:23pm UTC](https://forum.ansible.com/t/need-help-with-tags/34678/2 "2021-02-24T16:23:11Z")

</div>

There is no capability to limit tags from within a playbook. Tags can only be limited from the CLI via -t/–tags

---

<div class="post-metadata">

**Author:** ![Mouton\_Noir](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mouton\_Noir](https://forum.ansible.com/u/Mouton_Noir)\
**Post date:** [February 24, 2021, 4:39pm UTC](https://forum.ansible.com/t/need-help-with-tags/34678/3 "2021-02-24T16:39:12Z")

</div>

Thanks Matt for your quick answer… Even if it doesn’t arrange me… 😉  
It’s weird this is not possible. I’m surprised. I guess I’m not the first one to consider doing this. Is there a reason why ?  
Did you (or someone else) hear about a “trick” to get a similar behaviour using something else ?

Regards

---

<div class="post-metadata">

**Author:** ![Mouton\_Noir](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mouton\_Noir](https://forum.ansible.com/u/Mouton_Noir)\
**Post date:** [February 24, 2021, 5:05pm UTC](https://forum.ansible.com/t/need-help-with-tags/34678/4 "2021-02-24T17:05:27Z")

</div>

I finally found a “trick” that made it for me :  
Every task in the main.yml from “myrole” are conditioned with a different value for a variable myowntags  
ex:  
when: myowntags == “fst-openshot”

Then, in a taskfile or playbook, I can select the tasks I want with something like :

- name: test  
include\_role:  
name: myrole  
vars:  
myowntags: “fst-openshot”

As this role is only dedicated to pick up some tasks when needed (not applied to any group as itself), it makes it for me.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 24, 2021, 5:29pm UTC](https://forum.ansible.com/t/need-help-with-tags/34678/5 "2021-02-24T17:29:25Z")

</div>

> Every task in the main.yml from "myrole" are conditioned with a different  
> value for a variable myowntags  
> ex:  
> when: myowntags == "fst-openshot"
> 
> Then, in a taskfile or playbook, I can select the tasks I want with  
> something like :  
> - name: test  
> &nbsp;&nbsp;include\_role:  
> &nbsp;&nbsp;&nbsp;&nbsp;name: myrole  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myowntags: "fst-openshot"
> 
> As this role is only dedicated to pick up some tasks when needed (not  
> applied to any group as itself), it makes it for me.

Next option would be to fragment main.yml and put the tasks into  
the separate files, e.g

&nbsp;&nbsp;\> cat main.yml  
&nbsp;&nbsp;- import\_tasks: tasks01.yml  
&nbsp;&nbsp;- import\_tasks: tasks02.yml  
&nbsp;&nbsp;&nbsp;&nbsp;...

This would keep the original functionality untouched. Now, create  
files with the logical blocks you'd like to use, e.g.

&nbsp;&nbsp;shell: cat fst-openshot.yml  
&nbsp;&nbsp;- import\_tasks: tasks07.yml  
&nbsp;&nbsp;- import\_tasks: tasks25.yml  
&nbsp;&nbsp;- import\_tasks: tasks33.yml

Then, it would be possible to include such blocks selectively, e.g.

&nbsp;&nbsp;- name: test  
&nbsp;&nbsp;&nbsp;&nbsp;include\_role:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: myrole  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks\_from: "{{ myowntags }}.yml"

---

<div class="post-metadata">

**Author:** ![Mouton\_Noir](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mouton\_Noir](https://forum.ansible.com/u/Mouton_Noir)\
**Post date:** [February 25, 2021, 11:12am UTC](https://forum.ansible.com/t/need-help-with-tags/34678/6 "2021-02-25T11:12:05Z")

</div>

Thanks Vladimir  
I didn’t heard about tasks\_from.  
I’ll test it too

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 25, 2021, 4:30pm UTC](https://forum.ansible.com/t/need-help-with-tags/34678/7 "2021-02-25T16:30:11Z")

</div>

We've seen this requested several times, but it is normally an  
indicator of people making plays/roles way too big.

So we recommend actually splitting the playbook/roles into the more  
manageable units instead of implementing this kind of functionality or  
using a workaround via conditionals.
