Run tasks only when matches 2 tags

I tried something like this in a task, but it didn’t work

tags: “{{ ‘production’ && ‘web’ }}”

I have a task that uploads some web assets to S3. This is needed for a “web” tag, but only for “production”. In staging, it’s unnecessary, so I have tags like “web,production”, and “web,staging” and I’d like for them to behave differently.

I was trying to avoid making specific tags like “web-production” and “web-staging”

Thanks!

Yeah you can’t just make syntax up and expect it to mean something.

Sounds like you should just use a new play in the PB and simply use host groups to select.

– Michael

I actually ran into this requirement today. For example:

roles:

  • { role: network_config, tags: netconf }

Inside that role, I might have a bunch of tasks defined that I want to run only for a particular tag so ideally I’d like to be able to specify:

ansible-playbook … -t netconf:ifcfg -t netconf:routes

… to run all tasks tagged with ‘ifcfg’ or ‘routes’, or I can choose to run all tasks in the network_config role:

ansible-playbook … -t netconf

Can’t do that with the current state of Ansible, right?

AFAIK no. As a workaround, in 'foo' role, I define tag 'foo' everywhere,
then 'foo:install', 'foo:config', 'foo:bar', 'install', 'config', 'bar'
... where appropriate, so I can execute a '-t config' to configure
roles only, or '-t foo:config' to configure foo role only, etc...

This is tedious but I don't think there is another way.

Set operations other than the current 'union' could be great (run all
tasks with tag X and Y, run all tasks except those with tags X) :
install only some roles, run only configuration steps for all roles
(skipping not apt/yum long operations), ...

Another neat feature would be to tag all tasks for a role (e.g. set the
tag 'foo' for all plays in role foo). I don't think this is actually
possible (?), something like (tasks/main.yml) :

- { include: install.yml, set_tags: [ 'foo', 'install' ] }
- { include: config.yml, set_tags: [ 'foo', 'config' ] }

M

I’d welcome pull requests to add boolean support for tags (though when adding it, it should be done such that --list-hosts and --list-tasks is also aware of it, so … utility functions and that sort of thing)

This syntax would look just like the host spec:

–hosts “foo:&bar”

as of course

–hosts “foo:bar”

would mean foo or bar.