Reputation: 155
Confused as to whether role tagging from the playbook is working properly, and, if so, the philosophy behind it.
Playbook
- hosts: Test-c7-1
roles:
- role: test.tag
tags: tag2
Role/task
---
- debug:
msg: "task - tag1 set"
tags: tag1
- debug:
msg: "task - tag2 set"
tags: tag2
- debug:
msg: "task - always set"
tags: always
- debug:
msg: "task - NO TAG"
When a matching tag is passed in, the role is run, and everything in the task list is executed regardless of tagging.
Matching tag:
ansible-playbook playbook/tagtester.yml --tags "tag2"
PLAY [Test-c7-1] ********************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************************
ok: [Test-c7-1]
TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - tag1 set"
}
TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - tag2 set"
}
TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - always set"
}
TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - NO TAG"
}
PLAY RECAP **************************************************************************************************************************************************************************
Test-c7-1 : ok=5 changed=0 unreachable=0 failed=0
When an non-matching tag is passed, the role is STILL run, BUT the (non-matching) tag is passed down to the tasks, and only tasks with that tag (or tagged 'always') are executed.
Non-matching tag:
ansible-playbook playbook/tagtester.yml --tags "tag1"
PLAY [Test-c7-1] ********************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************************
ok: [Test-c7-1]
TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - tag1 set"
}
TASK [test.tag : debug] *************************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "task - always set"
}
PLAY RECAP **************************************************************************************************************************************************************************
Test-c7-1 : ok=3 changed=0 unreachable=0 failed=0
Is this expected behavior?
And, is there a way to tag a role (as well as a task - include_role) in a playbook to only filter whether that role itself is executed or not based on that tag passed in at execution?
Upvotes: 9
Views: 22542
Reputation: 68559
Is this expected behavior?
Yes, it is.
And, is there a way to tag a role (as well as a task - include_role) in a playbook to only filter whether that role itself is executed or not based on that tag passed in at execution?
Since Ansible version 2.4 there are three ways of using roles:
With import_role
and roles
declaration, Ansible concatenates the tags specified in the declaration and executes the tasks.
include_role
works like a stand-alone task, i.e. it itself observes the tags.
Notes
- The same rule applies to when
conditions.
- Before Ansible 2.4 include_role
worked like import_role
now.
- Reference Differences Between Static and Dynamic
Upvotes: 7