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
On Wednesday, June 14, 2023 at 10:54:35 AM UTC-7 Brian Lamb wrote:
> 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)'`
>
> On Tuesday, March 15, 2016 at 12:40:13 PM UTC-7 Mike Biancaniello wrote:
>
>> 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 view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/6b435b4b-68b9-4e46-851a-0033a430faa9n%40googlegroups.com.