# Trying to skip a task when a dict variable is not defined.

**URL:** <https://forum.ansible.com/t/trying-to-skip-a-task-when-a-dict-variable-is-not-defined/15343>\
**Category:** Ansible Project\
**Created:** [May 27, 2014, 7:25pm UTC](https://forum.ansible.com/t/trying-to-skip-a-task-when-a-dict-variable-is-not-defined/15343 "2014-05-27T19:25:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Heath\_Henjum](https://avatars.discourse-cdn.com/v4/letter/h/9d8465/32.png) [@Heath\_Henjum](https://forum.ansible.com/u/Heath_Henjum)\
**Post date:** [May 27, 2014, 7:25pm UTC](https://forum.ansible.com/t/trying-to-skip-a-task-when-a-dict-variable-is-not-defined/15343/1 "2014-05-27T19:25:17Z")

</div>

Hi,

I have a task defined as follows:

- name: copy defgen paramfile  
template: src={{ item.key }}.prm.j2 dest={{ ogg\_home }}/{{ item.value.pfileloc }}/{{ item.key }}.prm backup=yes  
when: defgensrc is defined and oggaction == ‘defgensrc’  
with\_dict: defgensrc

It is operating on two hosts in a group, but one of the hosts does not have the dictionary defgensrc defined.

So, I get this error:

fatal: [[mn-ldvora01.rxhub.net](http://mn-ldvora01.rxhub.net)] =\> with\_dict expects a dict

Is there a way to conditionally skip this task when the dictionary is not defined,

or would it be better to further refine my groups so that the host that is a “defgensrc” is in a specific child group and the one that is not a “defgensrc”

The two hosts are defined in a group called [mn-ogg] because they are both oracle golden gate servers, but one is the source of “defgen” and the other is the destination of the results of “defgen”.  
(not that you probably need to know the details of all that)

Thanks in advance,  
Heath Henjum

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 28, 2014, 12:01pm UTC](https://forum.ansible.com/t/trying-to-skip-a-task-when-a-dict-variable-is-not-defined/15343/2 "2014-05-28T12:01:22Z")

</div>

You migh want to use set\_fact on the hosts, when the dict is not defined, to give it some default value instead. As you’ve noticed, the “when” clause is applied to each item in the loop, so it’s evaluated after the with\_dict is parsed.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 28, 2014, 12:10pm UTC](https://forum.ansible.com/t/trying-to-skip-a-task-when-a-dict-variable-is-not-defined/15343/3 "2014-05-28T12:10:21Z")

</div>

I don’t like suggesting anyone should ever need set\_fact, except in cases where it’s occasionally needed to multiply a variable or something, as in most cases Ansible lazy-evaluates variables, and in most cases those expressions can just be written in the “vars” section.

I’d do this:

with\_dict: defgensrc | default({})

In this case, the dict is empty when it has no value, which means the statement is skipped.

---

<div class="post-metadata">

**Author:** ![Heath\_Henjum](https://avatars.discourse-cdn.com/v4/letter/h/9d8465/32.png) [@Heath\_Henjum](https://forum.ansible.com/u/Heath_Henjum)\
**Post date:** [May 29, 2014, 3:18pm UTC](https://forum.ansible.com/t/trying-to-skip-a-task-when-a-dict-variable-is-not-defined/15343/4 "2014-05-29T15:18:26Z")

</div>

Thanks! I’m new to Ansible, and just learning.
