On Fri, 26 Jun 2020 06:46:04 -0700 (PDT)
Cade Lambert <[email protected]> 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

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200626173732.7899c660%40gmail.com.

Attachment: pgp7YhCCmdT_7.pgp
Description: OpenPGP digital signature

Reply via email to