How to make use of provided tags with --skip-tags or --tags inside a (role)-task ?

---- Question

Is it possible to access the list of provided tags ( --tags, --skip-tags ) from a variable inside a task?

Is it maybe possible to access the invoked command itself from variable?

Is it possible to use patterns (http://docs.ansible.com/intro_patterns.html) with tags?

Something like --tags=config,&reload would be very helpful.

------ Example

Given i have:

  • task A with tags: [ config ]
  • task B with tags: [ config, reload ]
  • task C with tags: [ service, reload ]

---- Problem

I’d like to skip task B if --tags does not contain ‘config’ AND ‘reload’.

Thanks in advance.

No, this is not available, it doesn’t really make sense for a task to know what tags are available because it can’t do anything with that information.

Boolean operations other than “OR” on --tags is also not presently available.

Hello Michal,

thanks for your time & answer. I was asking because i wanted to extend the use of host(-group) patterns (http://docs.ansible.com/intro_patterns.html)… to tags.

It would allow for generic roles to provide tasks that don’t get executed by default … but can be triggered by providing a certain set of tags.

Imagine it was possible to do the following …

ansible --tags php-fpm,nginx,&service,&stop

which would match all tasks with exacly

  • tags: [php-fpm, service, stop]
  • tags: [nginx, service, stop]

… to stop the php-fpm and nginx service.

I can imagine a lot of use-cases where this would come in handy.

The idea came up while i was developing a “dashboard” for my development machine that would allow me to quickly change it’s state by selecting tags (using autocompletion).

How cool would it be to just … click together some expressive commands like “composer, install” , “mysql, install” … and have the machine in the desired state.

It shouldn’t be too hard to expose the provided tags as a variable + create a jinja function that can be used inside ‘when’ and evaluates the provided tags against the ‘tags’ variable ?

What are your thoughts about this? Could you share some implementation tips or am I on the completely wrong track here?