Ansible 2.8.1
In my playbook tasks/
dir:
main.yml
dev.yml
In main.yml
I have a block like this:
- include_tasks: dev.yml
when: ec2_tag_env == 'dev'
It works fine
However, if I try to call a specific task within dev.yml using a tag. It won't qualify the task during the run
For example, this tagged task within dev.yml
:
- name: Pull the latest image
docker_image:
name: "{{ dev_image }}"
source: pull
tags:
- container
When I run the playbook with -t container
it will NOT qualify since the include_tasks
step doesn't have that tag.
Adding the tag to include_tasks
will of course fix the problem, but I would then need to keep track of tags as they get added to sub-tasks and add them here as well:
- include_tasks: dev.yml
when: ec2_tag_env == 'dev'
tags:
- container
Questions
Is it possible to have Ansible just "know" what tasks are within the
include_tasks
block and pull the applicable tags?What's the best practice for achieving this goal?
What I would prefer to not have to do:
- Put everything
main.yml
. I have so many tasks in this playbook I really want to keep them organized in files. - Tag all my
include_tasks
blocks with all its sub-tags manually. Sounds like a nightmare to manage.
Kind of an old question, but for others who might struggle because they want or need to use
include_tasks
instead ofimport_tasks
. To those who can use both, I would also highly recommend to useimport_tasks
like it was answered before:In the official Ansible manual, the developers propose to use
tags: always
oninclude_tasks
itself while applying other tags withapply:
to the included tasks.See this example (copied from the manual):
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 ifinstall.yml
includes a task withtags: download
this task will be run if Ansible was called with--tags download
(without addinginstall
).Q: "Is it possible to have Ansible just 'know' what tasks are within the include_tasks block and pull the applicable tags?"
A: What is inside included task will be available after the control flow reaches the
include_task
statement and the file is included. See the examples below under the line. For example, such tags won't be included in the list of available tags. See option--list-tags
of ansible-playbook. In this respect, the answer is no. But, such tags may be available (after the control flow reaches theinclude_task
statement) if also specified on the level of the include_task. Also, the special tag always makes such tags available.Q: "What's the best practice for achieving this goal?"
A: There are two options:
Either use
import_tasks
. The imports are read when the playbook starts.Or, use the special tag always. Ansible will always include the tasks from the file.
To clarify the differences. Given the file below
works as expected
When the tags are used
-t t1,t2
, the task tagged t1 is executed, but the task tagged t2 is not because the file is not includedNote that, when started, the application knows nothing about the tags in the included file
When started, the application still knows nothing about the tags in the included file but the tasks will be always included
When the tags
-t t1,t2
are used now both tasks tagged t1 and t2 are executedNote that also here, when started, the application knows nothing about both the applied tags and the tags in the included file
The only difference caused by the applied tag is that all included tasks can be triggered by this tag, e.g.
But, the applied tags won't help you in triggering the included tasks separately. It is also good to understand that the applied tags make no sense without the same tags, or special tag always, on the include_task level.