0

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)

  • 1
    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. – U880D Feb 24 '23 at 08:15

0 Answers0