課題: loop構成のchanged_whenなどでは、register変数をどの様に使用できるか?
loop構成のタスクに対するregister変数は、各ループの実行結果を含む。
その場合、changed_whenなどではどの様にregister変数を利用すれば良いのか確認した。
結論: cheanged_whenなどでは、register変数は非ループ構成と同様に使用できる
register変数は、ループのそれぞれの回ではその回の結果で構成され、タスク終了時にループの全結果がマージされる様子。
検証
検証環境
macOS 10.15.6
ansible 2.9.4
python version = 3.8.1
検証playbook
site.yml
---
- hosts: all
gather_facts: false
tasks:
- loop: [ 0, 1, 2 ]
shell: |-
echo {{ item }}
if [[ {{ item }} == 1 ]]
then
echo changed # {{ item }} が1の場合にchangedとさせたい
fi
register: r
changed_when: '"changed" in r.stdout_lines' # このrはloop未適用
# 終了後はloopによる構造となる為、以下はVARIABLE IS NOT DEFINED!
- debug: var=r.stdout_lines
# rの構造を確認
- debug: var=r
# 参考1: 各ループのstdout_linesのみ抽出
- debug: var=r.results|map(attribute='stdout_lines')|list
# 参考2: 各ループのitemをkey、stdout_linesをvalueとする例
- debug: msg="{{ dict( r.results|map(attribute='item')|zip(r.results|map(attribute='stdout_lines'))) }}"
出力
$ ansible-playbook -i localhost, -c local site.yml
PLAY [all] **********************************************************************************************************
TASK [shell] ********************************************************************************************************
ok: [localhost] => (item=0)
changed: [localhost] => (item=1) # 意図通りitem=1の際にのみchanged
ok: [localhost] => (item=2)
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"r.stdout_lines": "VARIABLE IS NOT DEFINED!"
}
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"r": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"cmd": "echo 0\nif [[ 0 == 1 ]]\nthen\n echo changed\nfi",
"delta": "0:00:00.019091",
"end": "2020-09-18 10:25:45.664820",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "echo 0\nif [[ 0 == 1 ]]\nthen\n echo changed\nfi",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": 0,
"rc": 0,
"start": "2020-09-18 10:25:45.645729",
"stderr": "",
"stderr_lines": [],
"stdout": "0",
"stdout_lines": [
"0"
]
},
{
... (中略) ...
"stdout": "1\nchanged",
"stdout_lines": [
"1",
"changed"
]
},
{
... (中略) ...
"stdout": "2",
"stdout_lines": [
"2"
]
}
]
}
}
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"r.results|map(attribute='stdout_lines')|list": [
[
"0"
],
[
"1",
"changed"
],
[
"2"
]
]
}
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"msg": {
"0": [
"0"
],
"1": [
"1",
"changed"
],
"2": [
"2"
]
}
}
PLAY RECAP **********************************************************************************************************
localhost : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0