備忘
課題: include_tasksやinclude_roleなどの対象をloopなどで変更する際に、それら毎にtagを割り振るには?
例えば以下のtasksをもつplaybookにおいて、--tags task02などで実行するinclude_tasksを制御したいとする。
- name: Include task files
ansible.builtin.include_tasks:
file: tasks/{{ item }}.yml
loop:
- task00
- task01
- task02
- task03
- task04
ソリューション
- 呼び出し先のtasksにapplyを用いてtagを付与
かつ - include_tasksなどloop本体はtags: alwaysとして常に実行させる
- name: Include task files
ansible.builtin.include_tasks:
file: tasks/{{ item }}.yml
apply:
tags: "{{ [item] }}" # 1) applyにより、呼び出し先のtasksにtagを設定
tags:
- always # 2) include_tasksそのものはtagによらずに実行させる
loop:
- task00
- task01
- task02
- task03
- task04
1) includeにおけるtagsの継承 (applyによる指定)
applyによるtagの継承が必要。
この設定が無いと読み込んだtasks内のtaskが実行されない。
なお、呼ばれた先からさらにinclude_tasksなどを行った場合、その先にも適用される。
参考
Tag inheritance for includes: blocks and the apply keyword
2) include_tasksなどそのものは tags: always にてtagによらずに実行
この設定が無いとtag指定playbook実行時、include_tasksなどそのものが実行されない。
なお、動的にtagを付けたい今回の様なケースで無く、静的な対応でよいならば、applyと同じtagsを指定すればよいでしょう。
実行例
ちなみに呼び出すtasksは以下の様にdebugを行うのみ。
tags指定は行っていない。
tasks/task00.yml
- debug:
msg: task00
--tags task00,task03
$ ansible-playbook -i localhost, -c local --tags task00,task03 tags_test.yml
PLAY [all] *****************************************************************************************************************************
TASK [Include task files] **************************************************************************************************************
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task00.yml for localhost => (item=task00)
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task01.yml for localhost => (item=task01)
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task02.yml for localhost => (item=task02)
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task03.yml for localhost => (item=task03)
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task04.yml for localhost => (item=task04)
TASK [debug] ***************************************************************************************************************************
ok: [localhost] => {
"msg": "task00"
}
TASK [debug] ***************************************************************************************************************************
ok: [localhost] => {
"msg": "task03"
}
PLAY RECAP *****************************************************************************************************************************
localhost : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
--skip-tags task00,task03
$ ansible-playbook -i localhost, -c local --skip-tags task00,task03 tags_test.yml
PLAY [all] *****************************************************************************************************************************
TASK [Include task files] **************************************************************************************************************
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task00.yml for localhost => (item=task00)
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task01.yml for localhost => (item=task01)
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task02.yml for localhost => (item=task02)
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task03.yml for localhost => (item=task03)
included: /home/hiroyukionodera/repo/tags_dynamic/tasks/task04.yml for localhost => (item=task04)
TASK [debug] ***************************************************************************************************************************
ok: [localhost] => {
"msg": "task01"
}
TASK [debug] ***************************************************************************************************************************
ok: [localhost] => {
"msg": "task02"
}
TASK [debug] ***************************************************************************************************************************
ok: [localhost] => {
"msg": "task04"
}
PLAY RECAP *****************************************************************************************************************************
localhost : ok=8 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0