# Conditional with dict2item is falied

**URL:** <https://forum.ansible.com/t/conditional-with-dict2item-is-falied/33187>\
**Category:** Ansible Project\
**Created:** [June 15, 2020, 7:39pm UTC](https://forum.ansible.com/t/conditional-with-dict2item-is-falied/33187 "2020-06-15T19:39:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafael\_Tomelin](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rafael\_Tomelin](https://forum.ansible.com/u/Rafael_Tomelin)\
**Post date:** [June 15, 2020, 7:39pm UTC](https://forum.ansible.com/t/conditional-with-dict2item-is-falied/33187/1 "2020-06-15T19:39:10Z")

</div>

Hi guys,

I have a task with equal below, but when I validate it is defined it not recognized.

- name: include vars virtual\_machine  
include\_tasks: virtual\_machine.yml  
when:
- gms\_az\_vm\_action == ‘create\_virtual\_machine’
- virtual\_machine is defined  
loop: “{{ virtual\_machine|dict2items }}”

How I do for doesn’t read loop and not stop playbook when virtual\_machine not defined?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 15, 2020, 7:42pm UTC](https://forum.ansible.com/t/conditional-with-dict2item-is-falied/33187/2 "2020-06-15T19:42:09Z")

</div>

In the case of using `when` and `loop`, the `when` statements is applied to each iteration of the `loop`, and not before it.

As such, you cannot prevent a loop from happening using a when statement.

Instead you want to do this in loop, like:

loop: ‘{{ virtual\_machine|default({})|dict2items }}’

---

<div class="post-metadata">

**Author:** ![Rafael\_Tomelin](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rafael\_Tomelin](https://forum.ansible.com/u/Rafael_Tomelin)\
**Post date:** [June 15, 2020, 8:06pm UTC](https://forum.ansible.com/t/conditional-with-dict2item-is-falied/33187/3 "2020-06-15T20:06:34Z")

</div>

OK, thanks!

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [June 15, 2020, 8:33pm UTC](https://forum.ansible.com/t/conditional-with-dict2item-is-falied/33187/4 "2020-06-15T20:33:55Z")

</div>

> Hi guys,
> 
> I have a task with equal below, but when I validate it is defined it not recognized.  
> - name: include vars virtual\_machine  
> include\_tasks: virtual\_machine.yml  
> when:  
> - gms\_az\_vm\_action == 'create\_virtual\_machine'  
> - virtual\_machine is defined  
> loop: "{{ virtual\_machine|dict2items }}"
> 
> How I do for doesn't read loop and not stop playbook when virtual\_machine not defined?

&nbsp;&nbsp;loop: "{{ virtual\_machine | default({}) | dict2items }}"

This produces an empty dict ({}) if virtual\_machine is undefined and the loop goes over zero iterations.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
