pre_tasks:
- name: set tag as fact
set_fact:
myvar = mytag
tags: mytagfrom __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 ]
---
- 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
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
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