# idea: invoke tags for roles in role definition

**URL:** <https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740>\
**Category:** Ansible Project\
**Created:** [April 30, 2013, 8:36am UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740 "2013-04-30T08:36:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mattias\_Gees](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Mattias\_Gees](https://forum.ansible.com/u/Mattias_Gees)\
**Post date:** [April 30, 2013, 8:36am UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/1 "2013-04-30T08:36:50Z")

</div>

I think it would be a good idea to make it possible to invoke tags when you define a role in you playbook.  
For example we have a yum-repos roles with multiple repository definitions in it. All these repository’s are tagged (epel-repo, company-repo, …). Now we need to add --tags needed-repos when we execute our playbook. I am in favor to add an extra variable like ‘with\_tags’ to the roles definition in the main playbook.

Here is an example:  
roles:

- { role: foo, var: 8888, with\_tags: [‘epel\_repo’, ‘company\_repo’] }

With this change I think it is possible to reduce the amount of roles.

Let me now what you think.

Regards,  
Mattias

---

<div class="post-metadata">

**Author:** ![Vincent\_Van\_der\_Kuss](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@Vincent\_Van\_der\_Kuss](https://forum.ansible.com/u/Vincent_Van_der_Kuss)\
**Post date:** [April 30, 2013, 8:41am UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/2 "2013-04-30T08:41:30Z")

</div>

Hi,

I also like my colleague’s idea because in some cases you don’t know (or want to know) which tags you need for a certain role. so +1 for this.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [April 30, 2013, 12:23pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/3 "2013-04-30T12:23:34Z")

</div>

" Now we need to add --tags needed-repos when we execute our playbook."

Actually, that’s not quite the case, you need to specify “–tags needed-repos” only when you specify --tags. If you don’t specify --tags, all tags will run, including untagged items.

The solution is to untag the steps that you always need to run every single time.

---

<div class="post-metadata">

**Author:** ![Vincent\_Van\_der\_Kuss](https://avatars.discourse-cdn.com/v4/letter/v/7c8e57/32.png) [@Vincent\_Van\_der\_Kuss](https://forum.ansible.com/u/Vincent_Van_der_Kuss)\
**Post date:** [April 30, 2013, 12:31pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/4 "2013-04-30T12:31:11Z")

</div>

Michael,

I think this is what Mattias means :

In a role you could tag several tasks. In your playbook you could say : I want to run this role but only those tasks who have the following tags. That way you don’t need to specify them on the command line.

It would be like a “collection” of tasks where you can choose from using tags. In this case yum repos.

Vincent

---

<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:** [April 30, 2013, 6:49pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/5 "2013-04-30T18:49:18Z")

</div>

Roles do have the capability to tag several tasks:

- { role: foo, tags: [‘a’, ‘b’] }

Which automatically tags all tasks loaded with a, b, c

Thus if you want to run the role you just say

–tags a

or --tags b

Thus you could establish a convention of just tagging the role with it’s role name, if you wanted that.

You _DO_ need to specify them on the command line to indicate you want to run certain tags, I don’t think you want to get away from that at all.

You do get the tags list when you specify invalid tags so it’s easy to browse them if need be.

---

<div class="post-metadata">

**Author:** ![Mattias\_Gees](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Mattias\_Gees](https://forum.ansible.com/u/Mattias_Gees)\
**Post date:** [May 2, 2013, 9:35am UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/6 "2013-05-02T09:35:34Z")

</div>

Hello Micheal,

The idea we have is to make it also possible to run tags of roles without defining them on the command line, but to make it possible to define which tags need to be run in the playbook definition.  
In our case we have a yum-repos role with multiple repository definitions. But not all playbooks need all those yum repositories (sometimes we only need our company repo and in other cases only the epel repo and so on). Instead of needing to remember what repositories every playbook needs and defining it on the command line. It would be useful to define the needed tags in the playbook.

regards  
Mattias

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 2, 2013, 9:39am UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/7 "2013-05-02T09:39:58Z")

</div>

​Can't you solve this with conditional or parametrized includes?​

---

<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:** [May 2, 2013, 12:04pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/8 "2013-05-02T12:04:43Z")

</div>

“Instead of needing to remember what repositories every playbook needs”

Sounds like those playbooks should just template the repos they need into yum.repos.d to me.

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [May 3, 2013, 2:03pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/9 "2013-05-03T14:03:40Z")

</div>

Excerpts from Mattias Gees's message of 2013-04-30 04:36:50 -0400:

> I think it would be a good idea to make it possible to invoke tags when you  
> define a role in you playbook.  
> For example we have a yum-repos roles with multiple repository definitions  
> in it. All these repository's are tagged (epel-repo, company-repo, ...).  
> Now we need to add --tags needed-repos when we execute our playbook. I am  
> in favor to add an extra variable like 'with\_tags' to the roles definition  
> in the main playbook.
> 
> Here is an example:  
> roles:  
> - { role: foo, var: 8888, with\_tags: ['epel\_repo', 'company\_repo'] }

Just to note, because I like this idea too, this will work as-is now:

---

<div class="post-metadata">

**Author:** ![awiddersheim](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@awiddersheim](https://forum.ansible.com/u/awiddersheim)\
**Post date:** [October 19, 2013, 1:07am UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/10 "2013-10-19T01:07:09Z")

</div>

Sorry to dig up old posts but I was just trying to do this today and found that it wasn’t really possible unless I’m missing something which lead me to this thread after some googling. Is this feature not going to happen? I’d obviously like to see it implemented as well.

---

<div class="post-metadata">

**Author:** ![awiddersheim](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@awiddersheim](https://forum.ansible.com/u/awiddersheim)\
**Post date:** [October 19, 2013, 10:02pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/11 "2013-10-19T22:02:39Z")

</div>

I am trying to do the same thing which brought me to this post. Is this not possible yet? Any plans for it to be implemented? If I were to conjure up a patch would it get accepted?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 20, 2013, 1:14am UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/12 "2013-10-20T01:14:17Z")

</div>

The previous summary from serve:

" I want to run this role but only those tasks who have the following tags."

This is what tags already do.

I’m confused as to what you want, or think is not there.

---

<div class="post-metadata">

**Author:** ![awiddersheim](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@awiddersheim](https://forum.ansible.com/u/awiddersheim)\
**Post date:** [October 20, 2013, 1:55pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/13 "2013-10-20T13:55:13Z")

</div>

I want to be able to specify what tags I want to be run for a role in the playbook instead of on the command line. So to use what I’m doing now as an example, I’m trying to deploy an application that has a server and a client. They share a lot of the same tasks for installation. Servers will get all of the server files including client files while the clients only get the client related files. I didn’t want to have to split things up into roles for this because it seemed like unnecessary sprawl. What I ended up doing in the interim which I got the idea from the previous post is just use conditional logic and variables.

Here is what I put in my playbook:

- {role: application, client\_only: true}

Then in my role for tasks that are only required for servers I added:

when: client\_only is not defined

Instead of doing this I’d like to just use tags and be able to invoke tags from within the playbook. I understand that you can do it on the command line but some of the other users who use ansible or even myself may forget to add the “client” tag or whatever when they run something. This would help prevent that.

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [October 20, 2013, 3:30pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/14 "2013-10-20T15:30:31Z")

</div>

What about to do a client role and server role and have. Client role will have only what client need and server role will have client role as a dependency and add server specific stuff?

I know you wrote, that you do not separate roles, but I have feeling you are missing role dependencies?

---

<div class="post-metadata">

**Author:** ![awiddersheim](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@awiddersheim](https://forum.ansible.com/u/awiddersheim)\
**Post date:** [October 20, 2013, 4:43pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/15 "2013-10-20T16:43:19Z")

</div>

I’m sure doing separate client/server roles would work but again, I don’t really want to have this role sprawl. Means more files, more directories… just more stuff. To me it is simpler to just keep everything in a single role and just use tags to install what is appropriate.

---

<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:** [October 20, 2013, 4:43pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/16 "2013-10-20T16:43:30Z")

</div>

“I want to be able to specify what tags I want to be run for a role in the playbook instead of on the command line”

Ok, so this is about selectively including _part_ of a role.

Yeah, we don’t want do that.

What should happen here is you should break the role into smaller parts and leverage role dependencies.

---

<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:** [October 20, 2013, 4:44pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/17 "2013-10-20T16:44:05Z")

</div>

“I know you wrote, that you do not separate roles, but I have feeling you are missing role dependencies?”

[http://ansibleworks.com/docs/playbooks\_roles.html#role-dependencies](http://ansibleworks.com/docs/playbooks_roles.html#role-dependencies)

---

<div class="post-metadata">

**Author:** ![awiddersheim](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@awiddersheim](https://forum.ansible.com/u/awiddersheim)\
**Post date:** [October 20, 2013, 5:02pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/18 "2013-10-20T17:02:11Z")

</div>

Is there anyway when executing a role for it to know that it was invoked as dependency for another role and can it know what the initial role was named?

---

<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:** [October 22, 2013, 1:09am UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/19 "2013-10-22T01:09:40Z")

</div>

Nope.

---

<div class="post-metadata">

**Author:** ![Chaoran\_Xie](https://avatars.discourse-cdn.com/v4/letter/c/b38774/32.png) [@Chaoran\_Xie](https://forum.ansible.com/u/Chaoran_Xie)\
**Post date:** [January 31, 2014, 6:03pm UTC](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740/20 "2014-01-31T18:03:57Z")

</div>

Hi Vincent,

Did you guys ever find a way to “run this role but only those tasks who have the following tags. That way you don’t need to specify them on the command line”. It seems that at one point Michael DeHaan said it below that this is what tags already do but i wasnt able to get it to work. And the suggestion for breaking things into multiple roles and uses dependencies does not really apply to what i want to do.

[Next page](https://forum.ansible.com/t/idea-invoke-tags-for-roles-in-role-definition/12740.md?page=2)
