# Condition before lookup

**URL:** <https://forum.ansible.com/t/condition-before-lookup/16298>\
**Category:** Ansible Project\
**Created:** [September 22, 2014, 10:44am UTC](https://forum.ansible.com/t/condition-before-lookup/16298 "2014-09-22T10:44:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Daniel\_Schroeder](https://forum.ansible.com/u/Daniel_Schroeder)\
**Post date:** [September 22, 2014, 10:44am UTC](https://forum.ansible.com/t/condition-before-lookup/16298/1 "2014-09-22T10:44:54Z")

</div>

Hello list,

I’m having a problem with adding a condition before a lookup plugin.

A simplified example that everyone can reproduce is this:

`

- debug: msg=“This debug message should not be triggerd since some\_dict is not defined”  
when: some\_dict is defined  
with\_dict: some\_dict  
`

That doesn’t work because _with\_dict_ is processed before the condition is checked. That’s not wrong, that’s a good feature, because it enables you to do things like this:

`

- debug: msg=“Only process item a”  
when: item == “a”  
with\_items:
- a
- b  
`

How could I solve my problem with having a condition before the task fails, because some\_dict is not defined in my first example?

Not even an _ignore\_errors: yes_ helps in this case. I guess because it is a fatal error raised by the plugin and not simply a failed task.

`  
TASK: [debug msg=“This debug message should not be triggerd since some\_dict is not defined”] \*\*\*  
fatal: [test1.local] =\> with\_dict expects a dict

FATAL: all hosts have already failed – aborting  
`

Thanks,  
Daniel

---

<div class="post-metadata">

**Author:** ![Daniel\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Daniel\_Schroeder](https://forum.ansible.com/u/Daniel_Schroeder)\
**Post date:** [September 22, 2014, 11:15am UTC](https://forum.ansible.com/t/condition-before-lookup/16298/2 "2014-09-22T11:15:32Z")

</div>

OK, I found a workaround. My task actually is inside a role. Therefore I was able to define a default value for _some\_dict_.

myRole/defaults/main.yml:

`some_dict: {}`

The condition still is ignored, but with no elements nothing is processed, obviously.

Is there another solution to this? If this task would not be inside a role (yes, I know you always should work with roles ;)) I wouldn’t be able to define a default value.

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 22, 2014, 11:46am UTC](https://forum.ansible.com/t/condition-before-lookup/16298/3 "2014-09-22T11:46:02Z")

</div>

conditionals are for tasks, not for lookups, these always get executed

---

<div class="post-metadata">

**Author:** ![Daniel\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Daniel\_Schroeder](https://forum.ansible.com/u/Daniel_Schroeder)\
**Post date:** [September 22, 2014, 12:14pm UTC](https://forum.ansible.com/t/condition-before-lookup/16298/4 "2014-09-22T12:14:58Z")

</div>

Not really a solution for the problem, is it? 😉

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 22, 2014, 12:44pm UTC](https://forum.ansible.com/t/condition-before-lookup/16298/5 "2014-09-22T12:44:41Z")

</div>

actually the solution is what you already used or

with\_dict: some\_dict|default({})

---

<div class="post-metadata">

**Author:** ![Daniel\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Daniel\_Schroeder](https://forum.ansible.com/u/Daniel_Schroeder)\
**Post date:** [September 22, 2014, 1:30pm UTC](https://forum.ansible.com/t/condition-before-lookup/16298/6 "2014-09-22T13:30:59Z")

</div>

Ah, great, didn’t know you can use _default_ in the yml itself. Only had been using it in jinja templates. Thanks, that works and is more elegant that creating the additional defaults file.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 22, 2014, 1:42pm UTC](https://forum.ansible.com/t/condition-before-lookup/16298/7 "2014-09-22T13:42:09Z")

</div>

"with\_" fields are 'auto templated', you can use jinja2 in all fields  
that are so

---

<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:** [September 22, 2014, 5:23pm UTC](https://forum.ansible.com/t/condition-before-lookup/16298/8 "2014-09-22T17:23:02Z")

</div>

For some further elucidation, it should be pointed out that the “when” gets evaluated for every step of the loop, which is why it can’t be applied to decide whether to loop in the first place.

when: foo is defined and   
with\_items: foo

Will work as a looping construct, if you don’t want to the default trick.
