I try to use a conditional when inside a task with a nested loop. Specifically first of all I want to read different states per user, register the variable after that and want to iterate over this variable in another task.
Register the variable:
- name: "Desktop & Screen Saver - Get values for corner configuration"
shell: "defaults read /Users/{{ item[0] }}/Library/Preferences/com.apple.dock {{ item[1] }}"
register: result_CornerConfiguration
loop: "{{ query('nested', ['{{ result_GetUsers.stdout_lines }}'], ['wvous-bl-corner', 'wvous-br-corner', 'wvous-tl-corner', 'wvous-tr-corner']) }}"
Now I want to set the state of each of those 4 configurations (wvous-bl-corner, wvous-br-corner, wvous-tl-corner, wvous-tr-corner) to 1, but only if the actual state is 6:
- name: "Desktop & Screen Saver - Secure screen saver corners"
osx_defaults:
domain: "/Users/{{ item[0] }}/Library/Preferences/com.apple.dock"
key: "{{ item[1] }}"
type: int
value: 1
loop: "{{ query('nested', ['{{ result_GetUsers.stdout_lines }}'], ['wvous-bl-corner', 'wvous-br-corner', 'wvous-tl-corner', 'wvous-tr-corner']) }}"
when:
- result_CornerConfiguration[item[0]][item[1]].stdout|int == 6
I tried multiple ways to access result_CornerConfiguration as an array or as an hash but nothing really works. Debug of the variable looks like this:
ok: [localhost] => {
"result_CornerConfiguration": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_label": [
"user",
"wvous-bl-corner"
],
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "defaults read /Users/user/Library/Preferences/com.apple.dock wvous-bl-corner",
"delta": "0:00:00.024526",
"end": "2018-08-08 14:12:46.834554",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "defaults read /Users/user/Library/Preferences/com.apple.dock wvous-bl-corner",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": [
"user",
"wvous-bl-corner"
],
"rc": 0,
"start": "2018-08-08 14:12:46.810028",
"stderr": "",
"stderr_lines": [],
"stdout": "1",
"stdout_lines": [
"1"
]
},
so on and so on ...
]
}
}
I would be really thankful if anyone has an idea for my problem, maybe also with a more straight forward solution if my one doesn't make sense in an Ansible way. :)
Best wishes.
Edit:
Thanks to @Vladimir Botka I got the correct value, now I try to iterate over it in this task:
- name: "Desktop & Screen Saver - Secure screen saver corners"
osx_defaults:
domain: "/Users/{{ item[0] }}/Library/Preferences/com.apple.dock"
key: "{{ item[1] }}"
type: int
value: 1
loop: "{{ query('nested', ['{{ result_GetUsers.stdout_lines }}'], ['wvous-bl-corner', 'wvous-br-corner', 'wvous-tl-corner', 'wvous-tr-corner'], [0, 1, 2, 3]) }}"
when:
- result_CornerConfiguration.results[item[2]].stdout|int == 6
But it always changed the value to 1 and also seems not very elegant because of the third list ([0, 1, 2, 3]).