when: item is defined not working

The below task is not working as expected.
i am expecting it will print message for at least 2 times which is defined and skip rest 2.

TASK [Connect to Isilon Device] ****************************************************************************************************
skipping: [localhost]

I believe the loop: values are evaluated first, before any iterations are performed, in which case any undefined variables in any of the loop: expressions will throw the error you see.

This is probably a desirable behavior in general even though in this case you got caught by surprise.

Here’s a work-around that uses a sentinel value — and blatantly abuses “omit” for which I’m sure to be rightly scorned. :slight_smile:

**---** **- name: "Loop Iteration List"**  **hosts: localhost**  **vars:**  **nfs_prod_target_id: "Khan"**  **nfs_prod_source_id: "Siddque"**  **tasks:**  **- name: "Connect to Isilon Device"**  **ansible.builtin.debug:**  **msg: "{{ item }}"**  **when: item != omit**  **loop:**  **- "{{ nfs_prod_source_id | d(omit) }}"**  **- "{{ nfs_prod_target_id | d(omit) }}"**  **- "{{ nfs_dev_source_id | d(omit) }}"**  **- "{{ nfs_dev_target_id | d(omit) }}"**

Cheers,

Just wonder what’s stopping you from doing this: