At work the Ansible repo that I work with is not as organized as I’d like. I’d like to make sure that the one that I am starting to commit to in my Lab is not going to end up in the same state. In order to help keep it tidy I figured it would be a good idea to separate out some of the tasks that I have in the roles I am working on. Then I could call import_tasks on them in the main.yml with some conditionals. This would also allow me to resuse groups of tasks from one role in another. I started out with something like this:

tasks/tasks_b.yml
---
- name: tasks_b - 1
  tags: [never, tags_b]
  debug:
    msg: This is a test from the tasks_b tasks
tasks/tasks_a.yml
---
- name: tasks_a - 1
  tags: [never, tags_a]
  debug:
    msg: This is a test from the tasks_a tasks
tasks/main.yml
---
- include_tasks: "{{ role_path }}/tasks/tasks_a.yml"
  tags: [never, tags_a]

- include_tasks: "{{ role_path }}/tasks/tasks_b.yml"
  tags: [never, tags_b]

I was attempting to use the never reserved tag to make sure that a task was only executed when I specifically included the tag. I guess I misunderstood the documentation because the way I found that it works is that when you specify the never tag on a task it will only execute when you include any tag. So with the setup about and running:

ansible-playbook -i hosts playbooks/testing.yml -l worker1 -t tags_a

I would get the following output:

PLAY [all] *********************************************************************************************************************************************************************************************************

TASK [testing : include_tasks] *************************************************************************************************************************************************************************************
included: /home/ramon/code/Lab/ansible/playbooks/roles/testing/tasks/tasks_a.yml for worker1

TASK [testing : tasks_a - 1] ***************************************************************************************************************************************************************************************
ok: [worker1] => {
    "msg": "This is a test from the tasks_a tasks"
}

TASK [testing : include_tasks] *************************************************************************************************************************************************************************************
included: /home/ramon/code/Lab/ansible/playbooks/roles/testing/tasks/tasks_b.yml for worker1

TASK [testing : tasks_b - 1] ***************************************************************************************************************************************************************************************
ok: [worker1] => {
    "msg": "This is a test from the tasks_b tasks"
}

PLAY RECAP *********************************************************************************************************************************************************************************************************
worker1                    : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

So even though I only included tags_a in the command I got the exection of both tasks with the never tag.

I couldn’t really find a workaround that seems correct so I ended up adding my own conditional to the import_tasks tasks to check the tags:

---
- import_tasks: "{{ role_path }}/tasks/tasks_a.yml"
  when: '"tags_a" in ansible_run_tags'

- import_tasks: "{{ role_path }}/tasks/tasks_b.yml"
  when: '"tags_b" in ansible_run_tags'

Now when I run the playbook with just the tags_a tag I only get those tasks and likewise for tags_b. Additionally when I don’t include any tags all both are skipped:

$ ansible-playbook playbooks/testing.yml -i hosts -l worker1 -t tags_a

PLAY [all] ******************************************************************************************************************************************************************************************************

TASK [testing : tasks_a - 1] ************************************************************************************************************************************************************************************
ok: [worker1] => {
    "msg": "This is a test from the tasks_a tasks"
}

TASK [testing : tasks_b - 1] ************************************************************************************************************************************************************************************
skipping: [worker1]

PLAY RECAP ******************************************************************************************************************************************************************************************************
worker1                    : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

$ ansible-playbook playbooks/testing.yml -i hosts -l worker1 -t tags_b

PLAY [all] ******************************************************************************************************************************************************************************************************

TASK [testing : tasks_b - 1] ************************************************************************************************************************************************************************************
ok: [worker1] => {
    "msg": "This is a test from the tasks_b tasks"
}

PLAY RECAP ******************************************************************************************************************************************************************************************************
worker1                    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

$ ansible-playbook playbooks/testing.yml -i hosts -l worker1

PLAY [all] ******************************************************************************************************************************************************************************************************
TASK [testing : tasks_a - 1] ************************************************************************************************************************************************************************************
skipping: [worker1]


TASK [testing : tasks_b - 1] ************************************************************************************************************************************************************************************
skipping: [worker1]

PLAY RECAP ******************************************************************************************************************************************************************************************************
worker1                    : ok=0    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

I have commited this testing role to my Lab if you are interesting it adding something like that for you own test or just want to copy this: - Role - Playbook