# Run tasks only when matches 2 tags

**URL:** <https://forum.ansible.com/t/run-tasks-only-when-matches-2-tags/14077>\
**Category:** Ansible Project\
**Created:** [December 5, 2013, 8:24pm UTC](https://forum.ansible.com/t/run-tasks-only-when-matches-2-tags/14077 "2013-12-05T20:24:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandon\_Hilkert](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@Brandon\_Hilkert](https://forum.ansible.com/u/Brandon_Hilkert)\
**Post date:** [December 5, 2013, 8:24pm UTC](https://forum.ansible.com/t/run-tasks-only-when-matches-2-tags/14077/1 "2013-12-05T20:24:47Z")

</div>

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!

---

<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:** [December 5, 2013, 8:41pm UTC](https://forum.ansible.com/t/run-tasks-only-when-matches-2-tags/14077/2 "2013-12-05T20:41:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [December 6, 2013, 4:09am UTC](https://forum.ansible.com/t/run-tasks-only-when-matches-2-tags/14077/3 "2013-12-06T04:09:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Michel\_Blanc1](https://avatars.discourse-cdn.com/v4/letter/m/fbc32d/32.png) [@Michel\_Blanc1](https://forum.ansible.com/u/Michel_Blanc1)\
**Post date:** [December 6, 2013, 6:12am UTC](https://forum.ansible.com/t/run-tasks-only-when-matches-2-tags/14077/4 "2013-12-06T06:12:38Z")

</div>

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

---

<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:** [December 6, 2013, 1:21pm UTC](https://forum.ansible.com/t/run-tasks-only-when-matches-2-tags/14077/5 "2013-12-06T13:21:46Z")

</div>

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.
