Mastering Development System & Network

Use Ansible include_tasks with tags on the sub-tasks

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: ""
    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.

Leave a Reply

Your email address will not be published. Required fields are marked *