# with\_dict expects a dict - when evaluating a variable in ansible 2.0+

**URL:** <https://forum.ansible.com/t/with-dict-expects-a-dict-when-evaluating-a-variable-in-ansible-2-0/21607>\
**Category:** Ansible Project\
**Created:** [May 30, 2016, 4:53pm UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-when-evaluating-a-variable-in-ansible-2-0/21607 "2016-05-30T16:53:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Florian\_B](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@Florian\_B](https://forum.ansible.com/u/Florian_B)\
**Post date:** [May 30, 2016, 4:53pm UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-when-evaluating-a-variable-in-ansible-2-0/21607/1 "2016-05-30T16:53:09Z")

</div>

Hi everyone,

I’m trying to get this example to work with ansible 2.0+. It does work correctly with 1.9.

`

- hosts: localhost  
vars:

input: “top”

base: &base  
? a  
? b

top: &top  
\<\< : \*base  
? c  
? d

peak: &peak  
\<\< : \*top  
? e  
? f

tasks:

- debug: var=“{{ input|lower|regex\_replace(’ ‘,’') }}”

- name: Loop  
command: echo {{ item.key }}  
with\_dict: “{{ input|lower|regex\_replace(’ ‘,’') }}”  
`

In short, I want to merge the dicts base, top and peak and then loop over all key/value pairs in the corresponding dictionary, depending on the input another function produces (strings).

With ansible 2.0 I get the following error:

`  
% ansible --version  
ansible 2.0.0.2 (stable-2.0.0.1 7de237c5a1) last updated 2016/03/15 11:07:49 (GMT +200)  
lib/ansible/modules/core: (detached HEAD ce6619bf5d) last updated 2016/03/15 11:07:51 (GMT +200)  
lib/ansible/modules/extras: (detached HEAD 29af26884e) last updated 2016/03/15 11:07:51 (GMT +200)  
config file = /home/fbuchmeier/.ansible.cfg  
configured module search path = Default w/o overrides

% ansible-playbook -c local with\_dict.yml  
[WARNING]: provided hosts list is empty, only localhost is available

PLAY \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“top”: {  
“a”: null,  
“b”: null,  
“c”: null,  
“d”: null  
}  
}

TASK [Loop] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “ERROR! with\_dict expects a dict”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=0 unreachable=0 failed=1  
`

In ansible 1.9 I get my desired result:

`

% ansible --version  
ansible 1.9.6 (detached HEAD 10a38a7652) last updated 2016/04/29 15:14:07 (GMT +200)  
lib/ansible/modules/core: (detached HEAD 6972d0a291) last updated 2016/04/29 15:14:13 (GMT +200)  
lib/ansible/modules/extras: (detached HEAD 2c073442b0) last updated 2016/04/29 15:14:13 (GMT +200)  
configured module search path = None

% ansible-playbook -i environments/localhost -c local with\_dict.yml

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK: [debug var=“{{ input|lower|regex\_replace(’ ‘,’') }}”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“var”: {  
“top”: {  
“a”: null,  
“b”: null,  
“c”: null,  
“d”: null  
}  
}  
}

TASK: [Loop] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost] =\> (item={‘key’: ‘a’, ‘value’: None})  
changed: [localhost] =\> (item={‘key’: ‘c’, ‘value’: None})  
changed: [localhost] =\> (item={‘key’: ‘b’, ‘value’: None})  
changed: [localhost] =\> (item={‘key’: ‘d’, ‘value’: None})

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=3 changed=1 unreachable=0 failed=0  
`

Do you have any idea how I can get this to work?

Thank you very much,

Florian.

---

<div class="post-metadata">

**Author:** ![Florian\_B](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@Florian\_B](https://forum.ansible.com/u/Florian_B)\
**Post date:** [August 1, 2016, 7:01pm UTC](https://forum.ansible.com/t/with-dict-expects-a-dict-when-evaluating-a-variable-in-ansible-2-0/21607/2 "2016-08-01T19:01:15Z")

</div>

The solution is to address the variable like this (vars[‘variableName’]):

`

- name: Loop  
command: echo {{ item.key }}  
with\_dict: “{{ vars[input|lower|regex\_replace(’ ‘,’')] }}”

`
