Can't get include_tasks be selected by a tag

This is probably just me, but I can’t get this to work…

My playbook:

---
- name: Test
  hosts: localhost
  gather_facts: false

  roles:
    - { role: myrole, tags: [ "one" ] }

  tasks:
    - debug: msg="Playbook task"

and ‘roles/myrole/tasks/main.yml’ reads :

---
- include_tasks: one.yml
  tags: one

- include_tasks: two.yml
  tags: two

and the two role taskfiles, ‘one.yml’ :
- debug: msg="Role myrole - task ONE"

and ‘two.yml’
- debug: msg="Role myrole - task TWO"

Both taskfiles are executed while running the playbook. Even if ‘-t one’ is specified.

‘include_tasks’ docs say :

tags    full

Tags are interpreted by this action but are not automatically inherited by the include tasks, see `apply` Allows for the ‘tags’ keyword to control the selection of this action for execution

which looks what I want.. I want that only ‘roles/myrole/tasks/one.yml’ gets executed.
Can somebody please educate me on this ?

Thanks !

This adds tag “one” to all of the tasks of the role. So your roles/myrole/tasks/main.yml effectively becomes:

---
- include_tasks: one.yml
  tags:
  - one
  - one

- include_tasks: two.yml
  tags:
  - two
  - one

I’d say you just need to remove tags: [ "one" ] from the roles: key.

That’s clearly explained ! thanks !

Docs say :
When you add a tag to the role option, Ansible applies the tag to ALL tasks within the role.
which I interpreted as 'the tag is specified during role execution, so only tasks that has this task defined will get selected for execution. So I’m wrong..

Guess I have to switch to :

- include_role:
    name: myrole
    tasks_from: one

thanks for your reply Bojan !

tags, like most keywords, are inherited except on ‘include’ , roles is an import so both include_tasks already inherited the tags: [ "one" ].

includes do not push their own keywords into inheritance, why they have the apply keyword, so an include_role would function as you expect, but import_role and roles do not.

Also note that the included tasks won’t inherit the tag from the include_tasks, but will inherit tags from roles in this case.