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
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/97d529b1-b9be-4f0a-b320-25ee3ed8ad2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.