Ansible 2.8.1

In my playbook tasks/ dir:


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
    name: "{{ dev_image }}"
    source: pull
    - 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'
    - container


  • 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.

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.


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
    file: install.yml
        - install
    - 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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.