Score:0

Tagging behavior in a playbook with a role which contains an "include_role" task

bf flag

Having this structure:

Playbook(play2.yml)

    - hosts: localhost
      roles:
              - name: genrole
                tags: 
                   - genrole_tag # tagging is needed, since other roles may be present just below current one

Major Role[roles/genrole/tasks/main.yml]

        - debug:
            msg: 'This is task 1'
          tags: task1
        - import_role:
                 name: role1
        - debug: 
            msg: "After import, before include"
          tags: always
        - include_role:
            name: role1           
        - debug: 
            msg: "After basic include, before include with apply"
          tags: always
        - include_role:
            name: role1
            apply:
              tags: role1
          tags: role1

Role1's main.yml (loaded via include_role|import_role)[roles/role1/tasks/main.yml]:

- debug:
    msg: 'This is task 12'
  tags: task12
- debug:
    msg: 'This is task 13'
  tags: task13
- include:  include_task.yml

Role1's include_task.yml[roles/role1/tasks/include_task.yml]:

    - debug:
        msg: 'This is task 15'
      tags: task15
    - debug:
        msg: 'This is task 16'
      tags: task16

Execution

If I'd run ansible using inner tags, things are behaving as expected(e.g.: only 12, 15 printed out):

$ ansible-playbook -i inventory debug2/play2.yml --tags task12,task15 | grep msg
    "msg": "This is task 12"
    "msg": "This is task 15"
    "msg": "After import, before include"
    "msg": "After basic include, before include with apply"

But, if I'd use the most general tag(genrole_tag), it acts like a wildcard tag, enabling all inner tasks:

$ ansible-playbook -i inventory debug2/play2.yml --tags task12,task15,genrole_tag | grep msg
    "msg": "This is task 1"
    "msg": "This is task 12"
    "msg": "This is task 13"
    "msg": "This is task 15"
    "msg": "This is task 16"
    "msg": "After import, before include"
    "msg": "This is task 12"
    "msg": "This is task 13"
    "msg": "This is task 15"
    "msg": "This is task 16"
    "msg": "After basic include, before include with apply"
    "msg": "This is task 12"
    "msg": "This is task 13"
    "msg": "This is task 15"
    "msg": "This is task 16"

Question:

How could I execute only the tasks tagged with specified tags in CLI(e.g. task12, task15), which are part of the roles tagged with specified tags(e.g. genrole_tag)

U880D avatar
ca flag
As I understand your example and structure and the related documentation [Adding tags to includes](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_tags.html#adding-tags-to-includes) it is working as expected. I myself would change the structure to achieve the desired goal.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.