Search code examples
ansibleprovisioning

Role tags ignored when applied on include directive alongside with_items


So I am experiencing a strange issue with ansible not being able to execute some tagged include tasks in roles.

After some digging, it appears that the ability to run the specified task broke as soon as include is associated with with_items.

For example, my simple role contains:

role/tasks/main.yml
---
- include: test.yml
  tags:
    - my_role_test


role/tasks/test.yml
---
- debug:
    msg: "It works"

When I run my playbook with --tags=my_role_test, I can see the expected output:

[20:18:52] test : debug | server | SUCCESS | 593ms
{
  - msg: It works
}

However, if I change my main.yml file and add with_itemsto the include task:

role/tasks/main.yml
---
- include: test.yml
  with_items:
    - A
    - B
  tags:
    - my_role_test

I got this output:

[20:15:41] test : include
[20:15:41]  ➥ system | included: /test/tasks/test.yml for server
[20:15:41]  ➥ system | included: /test/tasks/test.yml for server
[20:15:41]  ➥ system | -- Play recap --

and the tasks are not being executed.

Am I doing something wrong ? Is this an ansible issue ? Do you know a workaround ?

My ansible version is 2.5.2.

Thanks,


Solution

  • So, basically, ansible changed the way to manage tags inheritance since Ansible 2.5. The only solution is to put the tags directly on each task of the included task file or use static import_*. Note, that for dynamic include, the use of a block section may help:

    role/tasks/main.yml
    ---
    - include: test.yml
      with_items:
        - A
        - B
    
    
    role/tasks/test.yml
    ---
    block:
      - debug:
          msg: "It works"
      - debug:
          msg: "It works again"
    tags:
      - my_role_test