There is an example playbook with the main task:
---
- name: Include tasks
include_tasks: include_task.yml
# args:
# apply:
# tags: task
tags: task
- name: Import tasks
import_tasks: import_task.yml
tags: task
- name: Main task
debug:
msg: The main task
tags: main
include_task.yml:
---
- name: Subtask3
debug:
msg: Subtask3 include
tags: task1
- name: Subtask4
debug:
msg: Subtask4 include
tags: task2
import_task.yml:
---
- name: Subtask1
debug:
msg: Subtask1 import
tags: task1
- name: Subtask2
debug:
msg: Subtask2 import
tags: task2
A few of examples expected usages:
❯ ansible-playbook playbooks/playground.yml
localhost | SUCCESS => {
"changed": false,
"include": "include_task.yml",
"include_args": {}
}
localhost | SUCCESS => {
"msg": "Subtask3 include"
}
localhost | SUCCESS => {
"msg": "Subtask4 include"
}
localhost | SUCCESS => {
"msg": "Subtask1 import"
}
localhost | SUCCESS => {
"msg": "Subtask2 import"
}
localhost | SUCCESS => {
"msg": "The main task"
}
❯ ansible-playbook playbooks/playground.yml -t main
localhost | SUCCESS => {
"msg": "The main task"
}
And a few examples of wrong usages:
❯ ansible-playbook playbooks/playground.yml -t task
localhost | SUCCESS => {
"changed": false,
"include": "include_task.yml",
"include_args": {}
}
localhost | SUCCESS => {
"msg": "Subtask1 import"
}
localhost | SUCCESS => {
"msg": "Subtask2 import"
}
❯ ansible-playbook playbooks/playground.yml -t task,task1
localhost | SUCCESS => {
"changed": false,
"include": "include_task.yml",
"include_args": {}
}
localhost | SUCCESS => {
"msg": "Subtask3 include"
}
localhost | SUCCESS => {
"msg": "Subtask1 import"
}
localhost | SUCCESS => {
"msg": "Subtask2 import"
❯ ansible-playbook playbooks/playground.yml -t task,task2
localhost | SUCCESS => {
"changed": false,
"include": "include_task.yml",
"include_args": {}
}
localhost | SUCCESS => {
"msg": "Subtask4 include"
}
localhost | SUCCESS => {
"msg": "Subtask1 import"
}
localhost | SUCCESS => {
"msg": "Subtask2 import"
If I uncomment the fragment with:
args:
apply:
tags: task
it returns "valid" output only for: ❯ ansible-playbook playbooks/playground.yml -t task
I would like to achieve: when I use task tag, both tasks from include_task.yml or/and import_task.yml are executed. When I use task,task1 tags only the first subtask will be executed. And the similar situation for task,task2 tags.
Generally I use tags only in main.yml file. But sometimes I need a larger granulation, so I want to use additional tags in included/imported tasks. Is this possible?
I'm not sure if this is the correct, proper use of nested tags for import_tasks:
❯ ansible-playbook playbooks/playground.yml -t task2
localhost | SUCCESS => {
"msg": "Subtask2 import"
}
❯ ansible-playbook playbooks/playground.yml -t task1
localhost | SUCCESS => {
"msg": "Subtask1 import"
}
It seems to me that for a larger playbook, it would be better to use: ansible-playbook playbooks/playground.yml -t task,task1 instead of ansible-playbook playbooks/playground.yml -t task1. Because tag task1 can be used in several places.