On 2/18/20 3:45 PM, Nuno Jordão wrote:
> Hello,
> 
> Thank you for your responses.
> Some times I dont like to use the loop: "{{ nulo|default([]) }}" because it 
> doesn't say "skipping" and in complex
> playbook with lots of hosts I like to have that feedback. 
> 
> I tested several options and and everything works, even the case of the issue 
> 45976
> <https://github.com/ansible/ansible/issues/45976> (should have tested that 
> one before). I think that, probably, the
> behaviour changed, and the documentation should be more clear in this 
> regard... The conditional is not applied only per
> item but also to the task...
> 

I suppose Ansible determines whether the conditional contains a loop variable. 
In that case it applies it for every
item. In the other case it evaluates it only once and skips the task when the 
conditional results in a false value.

The documentation seems not be completely accurate.

Regards
         Racke

>   vars:
>     tstbool: False
>     tstnull:
> 
>   tasks:
> 
>     - name: "test loop"
>       debug:
>         msg: "test"
>       loop: "{{nulo|default([])}}"
> 
>     - name: "test loop"
>       debug:
>         msg: "test"
>       loop: "{{nulo}}"
>       when: nulo is defined
> 
>     - name: "test loop"
>       debug:
>         msg: "test"
>       loop: "{{nulo}}"
>       when: nulo|default(False)
> 
>     - name: "test loop"
>       debug:
>         msg: "test"
>       loop: "{{nulo}}"
>       when: nulo|default([])|length > 0
> 
>     - name: "test loop"
>       debug:
>         msg: "test"
>       loop: "{{nulo}}"
>       when: tstbool
>   
>     - name: "test loop"
>       debug:
>         msg: "test"
>       loop: "{{nulo}}"
>       when: tstnull
> 
> Returns:
> 
> PLAY [localhost] 
> *****************************************************************************************************
> 
> TASK [test loop] 
> *****************************************************************************************************
> 
> TASK [test loop] 
> *****************************************************************************************************
> skipping: [localhost]
> 
> TASK [test loop] 
> *****************************************************************************************************
> skipping: [localhost]
> 
> TASK [test loop] 
> *****************************************************************************************************
> skipping: [localhost]
> 
> TASK [test loop] 
> *****************************************************************************************************
> skipping: [localhost]
> 
> TASK [test loop]
> *****************************************************************************************************[DEPRECATION
> WARNING]: evaluating None as a bare variable, this behaviour will go away and 
> you might need to add
> |bool to the expression in the future. Also see CONDITIONAL_BARE_VARS 
> configuration toggle.. This feature will be
> removed in version 2.12. Deprecation warnings can be disabled by setting 
> deprecation_warnings=False in ansible.cfg.
> skipping: [localhost]
> 
> PLAY RECAP
> ***********************************************************************************************************localhost
>     
>              : ok=0    changed=0    unreachable=0    failed=0    skipped=6    
> rescued=0    ignored=0
> 
> The final count for skipped is 6 but the first task doesn't report skipping
> 
> 
> On Mon, Feb 17, 2020 at 9:10 PM Vladimir Botka <[email protected] 
> <mailto:[email protected]>> wrote:
> 
>     On Mon, 17 Feb 2020 20:11:07 +0000
>     Nuno Jordão <[email protected] <mailto:[email protected]>> wrote:
> 
>     >     - name: "test loop"
>     >       debug:
>     >         msg: "test"
>     >       loop: "{{nulo}}"
>     >       when: nulo is defined
>     >
>     > where "nulo" is an undefined variable. Here the task skips instead of
>     > failing.
> 
>     Instead of testing whether the variable is defined or not, use the 
> "default"
>     filter. For example
> 
>          - name: "test loop"
>            debug:
>              msg: "test"
>            loop: "{{ nulo|default([]) }}"
> 
>     The loop will be skipped if "nulo" is undefined. Test "nulo" separately if
>     you want the playbook to fail. For example
> 
>          - name: Fail when nulo undefined
>            fail:
>              msg: "Variable nulo undefined"
>            when: nulo if undefined
> 
>     HTH,
> 
>             -vlado
> 
> -- 
> 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] 
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAEAA%3Dtv3rn4Wh_txsEr7P92ec5g7DSwWp6uhWRZDeHoyZQ-z2w%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAEAA%3Dtv3rn4Wh_txsEr7P92ec5g7DSwWp6uhWRZDeHoyZQ-z2w%40mail.gmail.com?utm_medium=email&utm_source=footer>.


-- 
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

-- 
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/412b990e-4a63-7c57-2e32-94a9826195b8%40linuxia.de.

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to