# with\_dict fatal error after conditional include

**URL:** <https://forum.ansible.com/t/with-dict-fatal-error-after-conditional-include/17378>\
**Category:** Ansible Project\
**Created:** [January 26, 2015, 11:39pm UTC](https://forum.ansible.com/t/with-dict-fatal-error-after-conditional-include/17378 "2015-01-26T23:39:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cselden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cselden/32/4405_2.png) [@cselden](https://forum.ansible.com/u/cselden)\
**Post date:** [January 26, 2015, 11:39pm UTC](https://forum.ansible.com/t/with-dict-fatal-error-after-conditional-include/17378/1 "2015-01-26T23:39:28Z")

</div>

Ansible Community,

I’m using a conditional include statement to call a task file which has a task that uses a “with\_dict” loop. When the include statement conditional fails, I’d like the task in “option.yml” to be skipped as the task would result in an error anyway.

When this is run, I get an error “with\_dict expects a dict” since there is no dictionary “groups[{{ grps[2] }}]”. Why does Ansible try to evaluate the task at all since it should be skipped because of the include conditional?

Ideally, there would be a way to use a “with\_items” loop with an include statement, but I am aware that that is intentionally not allowed.

(Note: I know this is a terrible example of how/when to use an include statement, but it shows the gist of how the error occurs. My actual usage is much more complicated.)

## variables

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [May 1, 2015, 10:33am UTC](https://forum.ansible.com/t/with-dict-fatal-error-after-conditional-include/17378/2 "2015-05-01T10:33:28Z")

</div>

I just ran into this same problem. Here is a much simpler test case:

- debug: msg=“DICT {{item.key}}”  
when: undefined\_dict is defined  
with\_dict: undefined\_dict

results in:

TASK: [consul | debug msg=“DICT {{item.key}}”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [consul-client-1] =\> with\_dict expects a dict

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [May 1, 2015, 10:37am UTC](https://forum.ansible.com/t/with-dict-fatal-error-after-conditional-include/17378/3 "2015-05-01T10:37:00Z")

</div>

[https://github.com/ansible/ansible/issues/10892](https://github.com/ansible/ansible/issues/10892)

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [May 1, 2015, 10:43am UTC](https://forum.ansible.com/t/with-dict-fatal-error-after-conditional-include/17378/4 "2015-05-01T10:43:23Z")

</div>

I just found [https://github.com/ansible/ansible/issues/8546](https://github.com/ansible/ansible/issues/8546), which explains clearly that when is for each item in the loop. (Which I now recall reading in the docs). So this is clearly not a bug.

And the workaround in that issues works just fine for my case.

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [May 1, 2015, 10:47am UTC](https://forum.ansible.com/t/with-dict-fatal-error-after-conditional-include/17378/5 "2015-05-01T10:47:12Z")

</div>

But related, and where this started for me, was running into this via a conditional include:

main.yml

- { include: service\_def.yml, when: consul\_services is defined }

service\_def.yml

- name: Create service definitions

template: \> …  
with\_dict: consul\_services

I did not expect service\_def.yml to even be loaded in this case, but it clearly does. What exactly is happening with task includes with a ‘when: false’? Does each task in the include get a hidden ‘when: false’ added to it? That would explain the behavior, but its definately not the behavior of least surprise to me.
