Tags as variable inside of playbook

5,559 views
Skip to first unread message

Branko Majic

unread,
Mar 15, 2016, 8:09:04 AM3/15/16
to ansible...@googlegroups.com
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

--
Branko Majic
Jabber: bra...@majic.rs
Please use only Free formats when sending attachments to me.

Бранко Мајић
Џабер: bra...@majic.rs
Молим вас да додатке шаљете искључиво у слободним форматима.

Mike Biancaniello

unread,
Mar 15, 2016, 12:34:25 PM3/15/16
to Ansible Project, bra...@majic.rs
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.

Jean-Yves LENHOF

unread,
Mar 15, 2016, 1:15:41 PM3/15/16
to ansible...@googlegroups.com, Branko Majic
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


Regards,

Branko Majic

unread,
Mar 15, 2016, 1:23:35 PM3/15/16
to ansible...@googlegroups.com
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
debug: msg="I am tagged, therefore I run"
when: myspecialtag in ansible_tags
tags:
- 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
include: ../handlers/main.yml
when: handlers in ansible_tags
tags:
- handlers

Best regards

Mike Biancaniello

unread,
Mar 15, 2016, 3:40:13 PM3/15/16
to Ansible Project, bra...@majic.rs
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
---
- name: stuff
  hosts
: localhost
  connection
: local
  gather_facts
: no

  vars
:
    ansible_tags
: "{{ lookup('get_tags') }}"

  tasks
:
 
- name: show me tags
    debug
: msg="tags = {{ ansible_tags }}"
    tags
:
   
- always
 
- name: Run this only and only if the tag was defined

    debug
: msg="I am tagged, therefore I run"
   
when: "'myspecialtag' in ansible_tags"
    tags
:
   
- myspecialtag



output:
ansible-playbook stuff.yml

PLAY [stuff] *******************************************************************

TASK
[show me tags] ************************************************************
ok
: [localhost] => {
   
"msg": "tags = [u'all']"
}

TASK
[Run this only and only if the tag was defined] ***************************
skipping
: [localhost]

PLAY RECAP
*********************************************************************
localhost                  
: ok=1    changed=0    unreachable=0    failed=0  



$ ansible-playbook stuff.yml --tags=myspecialtag

PLAY [stuff] *******************************************************************

TASK
[show me tags] ************************************************************
ok
: [localhost] => {
   
"msg": "tags = [u'myspecialtag']"
}

TASK
[Run this only and only if the tag was defined] ***************************
ok
: [localhost] => {
   
"msg": "I am tagged, therefore I run"
}

PLAY RECAP
*********************************************************************
localhost                  
: ok=2    changed=0    unreachable=0    failed=0  




Brian Lamb

unread,
Jun 14, 2023, 1:54:35 PM6/14/23
to Ansible Project
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)'`

Brian Lamb

unread,
Jun 14, 2023, 2:05:11 PM6/14/23
to Ansible Project
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

Brian Coca

unread,
Jun 14, 2023, 2:13:24 PM6/14/23
to ansible...@googlegroups.com
Use the never tag:

tags: never, mytag

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

--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages