Ansible import_role tags and sub tasks

Viewed 91

I am trying to understand how tags are passed in ansible and I am using this test playbook

local.yml

- hosts: localhost
  connection: local
  tasks:
   - name: test role
     import_role: 
       name: test
     tags: test

roles/test/tasks/main.yml

- name: test role task
  include_tasks: 
    file: test_task.yml

roles/test/tasks/test_task.yml

- debug: msg="test_task"

From my understanding, the import_role will cause the test tag to be applied to all the tasks of that role. And that the include_tasks will stop tags from being passed onto the tasks that are being included.

But this is what I get as output

PLAY [localhost] ******************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [localhost]

TASK [test : test role task] ******************************************************************************************************************
included: /home/fleetscut/temp/roles/test/tasks/test_task.yml for localhost

TASK [test : debug] ***************************************************************************************************************************
ok: [localhost] => {
    "msg": "test_task"
}

PLAY RECAP ************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

The include is run as expected because it inherits the test tag from the parent. However what I don't understand is why the tasks from inside the include are run. They should not inherit the test tag and therefore should be skipped.

I have tried to include a tag on the sub task incase there was some default action on untagged tasks, but still the same outcome.

Is my understanding of tag inheritance incorrect? Or does a roles tags get passed down to sub tasks regardless of if they were included or imported?

0 Answers
Related