# when: item is defined not working

**URL:** <https://forum.ansible.com/t/when-item-is-defined-not-working/38154>\
**Category:** Ansible Project\
**Created:** [June 23, 2023, 7:09pm UTC](https://forum.ansible.com/t/when-item-is-defined-not-working/38154 "2023-06-23T19:09:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [June 23, 2023, 7:09pm UTC](https://forum.ansible.com/t/when-item-is-defined-not-working/38154/1 "2023-06-23T19:09:06Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 25, 2023, 10:09pm UTC](https://forum.ansible.com/t/when-item-is-defined-not-working/38154/2 "2023-06-25T22:09:13Z")

</div>

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. 🙂

```auto
**---**  **- 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,

---

<div class="post-metadata">

**Author:** ![Kosala\_Atapattu1](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@Kosala\_Atapattu1](https://forum.ansible.com/u/Kosala_Atapattu1)\
**Post date:** [June 26, 2023, 2:15am UTC](https://forum.ansible.com/t/when-item-is-defined-not-working/38154/3 "2023-06-26T02:15:28Z")

</div>

Just wonder what’s stopping you from doing this:
