On Fri, 26 Jun 2020 06:46:04 -0700 (PDT)
Cade Lambert <
crl...@gmail.com> wrote:
> Can you explain what's happening in this statement:
> condition: "{{ (result|default({'rc': 1})).rc == 0 }}"
Sure. Let's start with the fact that "result" is available at controller
after each iteration. It's necessary to keep in mind that the remote host is
the origin of the "result". Hence, the data must be sent from the remote host
to the controller after each iteration.
If you want to see what data is available in the dictionary "result" after
each iteration run this task
- shell: 'echo {{ condition }} && [ "{{ item }}" -gt "2" ]'
loop: "{{ range(1, 3 + 1)|list }}"
register: result
ignore_errors: true
vars:
condition: '{{ result|default({"rc": 1}) }}'
- debug:
var: result
You'll see that "result" of each iteration will be concatenated into the
list "result.results". Knowing this, simply pickup an attribute to test the
success of each iteration. You've already found out how.
Now to the "condition". The "when" statement is evaluated at each iteration
and before the first iteration as well. There is no variable "result"
available before the first iteration and the task would crash. Hence,
"default" value is needed. In this case
result|default({"rc": 1})
The attribute "rc" is tested for success. Hence the default value {"rc": 1}
because we always want to run the first iteration. Next iterations evaluate
de facto the following expression because "result" was provided by the
previous iteration
result.rc == 0
HTH,
-vlado
--
Vladimir Botka