Ansible: Execute task only when a tag is specified
Solution 1:
Ansible 2.5 comes with special tags never
and always
. Tag never
can be used exactly for this purpose. E.g:
tasks:
- debug: msg='{{ showmevar}}'
tags: [ 'never', 'debug' ]
In this example, the task will only run when the debug
(or never
) tag is explicitly requested. [Reference on ansible docs]
Solution 2:
Although this is a roundabout solution, it works.
Inside the task list register a variable when normal execution runs. Then, add a when condition that checks that variable to the tagged task.
- shell: /bin/true
register: normal_task_list
- name: Only run when tag is specified
shell: /bin/echo "Only running because of specified tag"
when: normal_task_list is not defined
tags: specified
Solution 3:
I don't have enough reputation to upvote or comment on the answer suggesting the use of command-line variables (--extra-vars
), but I have this to add to it:
The caveat to this method is that the play will error and fail if you do not define that extra variable.
You can prevent play failure in the absence of an --extra-vars
definition by defining a default value in the playbook itself:
---
- hosts: ...
# ↓↓↓
vars:
thorough: false
# ↑↑↑
tasks:
- name: apt - install nfs-common only when thorough is true
when: thorough | bool
apt:
cache_valid_time: 86400
force: yes
pkg:
- nfs-common
Overriding via --extra-vars
will still work because variables defined on the command line take precedence over all other definitions.
The result is that the play runs without error when thorough
is not changed to true
on the command line.
Solution 4:
You can use Conditionals to protect against accidentally running tasks that would otherwise be executed if you don't specify a tag. The caveat to this method is that the play will error and fail if you do not define that extra variable.
Using the extra-vars argument you can trigger your conditional to be executed.
From ansible-playbook --help:
-e EXTRA_VARS, --extra-vars=EXTRA_VARS
set additional variables as key=value or YAML/JSON
Example:
ansible-playbook test.yaml -e "thorough=true"
test.yaml:
...
- name: apt - install nfs-common only when thorough is true
apt:
cache_valid_time: 86400
force: yes
pkg:
- nfs-common
when: thorough | default(False)
...
Solution 5:
Yes. Running ansible-playbook with the --tags foo
flag will ensure that only tasks that are tagged with foo
are executed. For example, assume we have a playbook called example.yml:
tasks:
- yum: name={{ item }} state=installed
with_items:
- httpd
- memcached
tags:
- packages
- name: some other task
..
tags:
- some other tag
running:
ansible-playbook example.yml --tags "packages"
Will make sure only the yum task is executed.
So actually you don't really need to use tags in when section to conditionally execute a task. Notice that depending on the complexity of your playbooks/roles you might need to use a combination of --tags and --skip-tags to control which tasks are executed. For example, if an include tasks is tagged as 'foo' and some task inside the included playbook is tagged as 'bar' and you run
ansible-playbook --tags "foo"
The internal task (tagged only as 'bar') will be executed. To avoid the execution of all internal tasks tagged as 'bar' you will have to execute the following command
ansible-playbook --tags foo --skip-tags bar