when clause can't evaluate the presence of tags. As a workaround, I use variables and tags together to run tasks specific to that tag/variable.
Ex: Imagine a playbook and inventory
# inventory
[dev]
192.168.1.1
# site.yml
- hosts: dev
roles:
- { role: common }
and in common/tasks/main.yml
# roles/common/tasks/main.yaml
- name: Install links
apt: name=links state=present
- include: uninstall.yml
when: uninstall_links is defined
tags:
- uninstall
# roles/common/tasks/uninstall.yml
- name: Uninstall links
apt: name=links state=absent
With this approach, you use the tag to select only the tasks in uninstall.yml, but you also need to set 'uninstall_links' variable to something to enable it. So if you run the playbook without any parameters, it will, by default, run the install task. To uninstall, you can set the tag 'uninstall' to your playbook (or cmdline) and MUST set the variable. If you don't set the tag, it will run everything (install and uninstall) in that order, which is good to test the entire process.
How to run everything (it will install and uninstall):
$ ansible-playbook -i inventory site.yml -l dev -s -k -e "uninstall_links=true"
How to run only the 'uninstall' tag on dev group
$ ansible-playbook -i inventory site.yml -l dev -s -k -e "uninstall_links=true" -t uninstall
Therefore, variables and tags could also be in site.yml/inventory files, allowing you to commit into your SCM and record your intention.