Use Ansible include_tasks with tags on the sub-tasks
Solution 1:
Q: "Is it possible to have Ansible just "know" what tasks are within the include_tasks block and pull the applicable tags?"
A: No. What is inside included task will be available after the control-flow reaches the include_task
statement and the file is included.
Q: "What's the best practice for achieving this goal?"
A: Use import_tasks
. Imports are read when the playbook starts.
Solution 2:
Kind of an old question, but for others who might struggle because they want or need to use include_tasks
instead of import_tasks
. To those who can use both, I would also highly recommend to use import_tasks
like it was answered before:
In the official Ansible manual, the developers propose to use tags: always
on include_tasks
itself while applying other tags with apply:
to the included tasks.
See this example (copied from the manual):
- name: Apply tags to tasks within included file
include_tasks:
file: install.yml
apply:
tags:
- install
tags:
- always
This way ensures that Ansible will always (except when called with --skip-tags always
) include the external tasks to be able to look at those specific tags, so if install.yml
includes a task with tags: download
this task will be run if Ansible was called with --tags download
(without adding install
).