# Tags as variable inside of playbook

**URL:** <https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871>\
**Category:** Ansible Project\
**Created:** [March 15, 2016, 12:08pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871 "2016-03-15T12:08:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![azaghal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/azaghal/32/4743_2.png) [@azaghal](https://forum.ansible.com/u/azaghal)\
**Post date:** [March 15, 2016, 12:08pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871/1 "2016-03-15T12:08:54Z")

</div>

Hello folks,

Is it possible to use tags as a variable inside of playbook?

Specifically, I would like to have a condition to execute certain task  
only if a tag has been defined.

I am aware this can be done with an --extra-vars option + checking for  
variable instead, but I wanted to see if I could abuse the tags system  
a bit to make the syntax less verbose/clearer.

Best regards

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [March 15, 2016, 4:34pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871/2 "2016-03-15T16:34:25Z")

</div>

This is probably very terrible, but…

`  
pre\_tasks:

- name: set tag as fact  
set\_fact:  
myvar = mytag  
tags: mytag  
`

I don’t think there is an ‘ansible\_tags’ var passed to the playbook.

I haven’t looked, but It is possible that an action\_plugin might have access to that (from **main** import cli), so you can create a module that sets ‘ansible\_tags’ as an ansible fact.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [March 15, 2016, 5:15pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871/3 "2016-03-15T17:15:32Z")

</div>

If I understand correctly what you want...

ansible-playbook myplaybook.yml --tags=myspecialtag is what you're looking for...

A tag can be use in more than one task...

The doc :  
[http://docs.ansible.com/ansible/playbooks\_tags.html](http://docs.ansible.com/ansible/playbooks_tags.html)

Regards,

---

<div class="post-metadata">

**Author:** ![azaghal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/azaghal/32/4743_2.png) [@azaghal](https://forum.ansible.com/u/azaghal)\
**Post date:** [March 15, 2016, 5:23pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871/4 "2016-03-15T17:23:28Z")

</div>

Thanks for the input, but that is something that would solve my  
requirement only partially.

What I wanted to do was more along the lines of (as someone has  
already pointed out, tags do not seem to be exposed in a run):

- name: Run this only and only if the tag was defined  
&nbsp;&nbsp;debug: msg="I am tagged, therefore I run"  
&nbsp;&nbsp;when: myspecialtag in ansible\_tags  
&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;- myspecialtag

For now I think I would need to stick to the --extra-vars in combination  
with --tags.

Perhaps a better description of what I want to do is to be able to  
rerun handlers only as needed in order to have a way to clean-up  
certain broken runs ("meta"-code provided below):

- name: Run handlers only  
&nbsp;&nbsp;include: ../handlers/main.yml  
&nbsp;&nbsp;when: handlers in ansible\_tags  
&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;- handlers

Best regards

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [March 15, 2016, 7:40pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871/5 "2016-03-15T19:40:13Z")

</div>

It sounds like you want to instruct a task to execute ONLY if a tag is present (skip if tag not set). I’ve seen that request before, but no idea if it may or may not be implemented.

I wonder if a vars\_plugin or lookup\_plugin might work…

would this work?

# file ./lookup\_plugins/get\_tags.py

`  
from **future** import (absolute\_import, division, print\_function)

from ansible.plugins.lookup import LookupBase  
from **main** import cli

class LookupModule(LookupBase):

def run(self, terms=‘’, \*\*kwargs):  
tags = cli.get\_opt(‘tags’).split(‘,’)  
return [tags]

`

# file ./stuff.yml

`

---

<div class="post-metadata">

**Author:** ![Brian\_Lamb](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@Brian\_Lamb](https://forum.ansible.com/u/Brian_Lamb)\
**Post date:** [June 14, 2023, 5:54pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871/6 "2023-06-14T17:54:35Z")

</div>

This looked promising, except that cli is not available on my ansible v 2.14.2 using python 3.11.2  
`Error was a <class ''ImportError''>, original message: cannot import name ''cli'' from '' __main__'' (/usr/bin/ansible-playbook)'`

---

<div class="post-metadata">

**Author:** ![Brian\_Lamb](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@Brian\_Lamb](https://forum.ansible.com/u/Brian_Lamb)\
**Post date:** [June 14, 2023, 6:05pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871/7 "2023-06-14T18:05:11Z")

</div>

A Magic var has been added, you can use ansible\_run\_tags now.  
[https://github.com/ansible/ansible/blob/stable-2.5/changelogs/CHANGELOG-v2.5.rst#minor-changes-4](https://github.com/ansible/ansible/blob/stable-2.5/changelogs/CHANGELOG-v2.5.rst#minor-changes-4)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 14, 2023, 6:11pm UTC](https://forum.ansible.com/t/tags-as-variable-inside-of-playbook/20871/8 "2023-06-14T18:11:06Z")

</div>

Use the never tag:

tags: never, mytag

This ensures the task is never selected UNLESS you specifically target  
a defined tag.
