On Mon, Feb 17, 2020 at 3:11 PM Nuno Jordão <
nuno....@gmail.com> wrote:
>
> Hello,
>
> I have a doubt regarding loops and conditionals that maybe someone can make it clear to me.
> In the documentation is stated that:
>
> "When combining Conditionals with a loop, the when: statement is processed separately for each item."
>
I am not sure since in your example the when is applied to the
debug, i.e. if when is false the debug task is not run.
> But if I do this:
>
> - name: "test loop"
> debug:
> msg: "test"
> loop: "{{nulo}}"
> when: nulo is defined
>
That is an interesting way to loop.
> where "nulo" is an undefined variable. Here the task skips instead of failing. If I read the documentation to the letter it should fail as it would only do the test inside the loop.
> Also, this issue:
>
https://github.com/ansible/ansible/issues/45976
>
> has a slightly different variation of my loop which fails instead of skipping.
>
The differences are more than slightly. having "null_var" as
the when test is not the same as "null_var is defined."
when: null_var is defined
is testing whether null_var is defined, returning true or false; it
could not care less about its contents though. So, it does not break
when it is not defined.
when: null_var | default(False)
would also work since it also gives an option for when null_var is not
defined. I believe
when: null_var
would work if (1) it is always defined and (2) you set it to true or
false or something. Someone more knowledgeable can check my claims.
> My observed behaviour is of course the better one as allows to test the list before running loop, but should I trust it? It is the same in all versions of Ansible?
>
> Thank you for your help. Regards,
>
> Nuno Jordão
>
> --
> 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
ansible-proje...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAEAA%3Dts5OMuUg535jFC0xwx2V6YiJ%3DoHAuW-XGKneejDC4QfkA%40mail.gmail.com.