idea: invoke tags for roles in role definition

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

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.

" 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.

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

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.

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

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

“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.

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:

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.

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?

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.

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.

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?

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.

“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.

“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

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?

Nope.

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.