Thanks for the explanation. The default piece was tripping me up but it all makes total sense now.
On Friday, June 26, 2020 at 11:37:53 AM UTC-4, Vladimir Botka wrote: > > On Fri, 26 Jun 2020 06:46:04 -0700 (PDT) > Cade Lambert <[email protected] <javascript:>> 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/5c2f6757-b027-48de-9ddb-da4785aced68o%40googlegroups.com.
