# How to use dictionnary with multiple elements ?

**URL:** <https://forum.ansible.com/t/how-to-use-dictionnary-with-multiple-elements/21394>\
**Category:** Ansible Developer\
**Created:** [May 10, 2016, 1:22pm UTC](https://forum.ansible.com/t/how-to-use-dictionnary-with-multiple-elements/21394 "2016-05-10T13:22:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clement\_VALENTIN](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Clement\_VALENTIN](https://forum.ansible.com/u/Clement_VALENTIN)\
**Post date:** [May 10, 2016, 1:22pm UTC](https://forum.ansible.com/t/how-to-use-dictionnary-with-multiple-elements/21394/1 "2016-05-10T13:22:53Z")

</div>

Hi,

I need help for use an dictionnary with multiple element, but i don’t find how to 😕

Exemple :

**_vars/mains.yml :_**

> users:  
> alice:  
> name: Alice Appleworth  
> telephone:
> 
> - “123-456-7890”
> - “123-456-7891”

**_task/main.yml :_**

> - name: “Test”  
> debug: msg=“User {{ item.key }} is {{ item.value.name }} ({{ item.value.telephone }})”  
> with\_dict: “{{users}}”

**_Output :_**

> ok: [XXXXXXXXXXXXXX] =\> (item={‘value’: {u’name’: u’Alice Appleworth’, u’telephone’: [u’123-456-7890’, u’123-456-7891’]}, ‘key’: u’alice’}) =\> {  
> “item”: {  
> “key”: “alice”,  
> “value”: {  
> “name”: “Alice Appleworth”,  
> “telephone”: [  
> “123-456-7890”,  
> “123-456-7891”  
> ]  
> }  
> },  
> “msg”: "User alice is Alice Appleworth **([u’123-456-7890’, u’123-456-7891’])"**  
> }

I would have two messages with phone number,  
Exemple :

“msg”: "User alice is Alice Appleworth **(123-456-7890)"**

“msg”: "User alice is Alice Appleworth **(123-456-7891)"**

Anybody have idea plz ? 😕

---

<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:** [May 10, 2016, 1:31pm UTC](https://forum.ansible.com/t/how-to-use-dictionnary-with-multiple-elements/21394/2 "2016-05-10T13:31:02Z")

</div>

You could use with\_subelements, however your var structure doesn’t lend itself well to using with\_subelements.

If you changed your var structure to look like the following (a list of users rather than a dict of users):

users:

- username: alice  
name: Alice Appleworth  
telephone:
- “123-456-7890”
- “123-456-7891”

You could then use:

- debug:  
msg: “User {{ item.0.username }} is {{ [item.0.name](http://item.0.name) }} ({{ item.1 }})”  
with\_subelements:
- “{{ users }}”
- telephone

---

<div class="post-metadata">

**Author:** ![Clement\_VALENTIN](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Clement\_VALENTIN](https://forum.ansible.com/u/Clement_VALENTIN)\
**Post date:** [May 10, 2016, 1:36pm UTC](https://forum.ansible.com/t/how-to-use-dictionnary-with-multiple-elements/21394/3 "2016-05-10T13:36:17Z")

</div>

Hi Matt,

Thx for your help 🙂

I have already try to use “with\_subelement”, but when i try this i have this error all the time :  
=\> [WARNING]: Failure when attempting to use callback plugin (\<ansible.plugins.callback.default.CallbackModule object at 0x2024d10\>): not all arguments converted during string formatting

Idem, with your exemple ☹

---

<div class="post-metadata">

**Author:** ![Clement\_VALENTIN](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Clement\_VALENTIN](https://forum.ansible.com/u/Clement_VALENTIN)\
**Post date:** [May 10, 2016, 2:15pm UTC](https://forum.ansible.com/t/how-to-use-dictionnary-with-multiple-elements/21394/4 "2016-05-10T14:15:35Z")

</div>

idem with short subelement :

Exemple :

vars =\>  
project:

- name: alice  
test:
- test1
- test2

task =\>

- debug:  
msg: “{{ item.0.name }}”  
with\_subelements:
- “{{ project }}”
- test

output :

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[WARNING]: Failure when attempting to use callback plugin (\<ansible.plugins.callback.default.CallbackModule object at 0x2c20d10\>): not all arguments converted during string formatting

I rage… 😢

---

<div class="post-metadata">

**Author:** ![Clement\_VALENTIN](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Clement\_VALENTIN](https://forum.ansible.com/u/Clement_VALENTIN)\
**Post date:** [May 10, 2016, 2:25pm UTC](https://forum.ansible.com/t/how-to-use-dictionnary-with-multiple-elements/21394/5 "2016-05-10T14:25:58Z")

</div>

I have try on localhost ansible install with this playbook :

> - hosts: 127.0.0.1  
> user: cvalentin  
> vars:  
> project:
> - name: alice  
> test:
> - test1
> - test2  
> tasks:
> - debug:  
> msg: “{{ item.0.name }}”  
> with\_subelements:
> - “{{ project }}”
> - test

Output :

> ok: [127.0.0.1] =\> (item=({‘name’: ‘alice’}, ‘test1’)) =\> {  
> “item”: [  
> {  
> “name”: “alice”  
> },  
> “test1”  
> ],  
> “msg”: “alice”  
> }  
> ok: [127.0.0.1] =\> (item=({‘name’: ‘alice’}, ‘test2’)) =\> {  
> “item”: [  
> {  
> “name”: “alice”  
> },  
> “test2”  
> ],  
> “msg”: “alice”  
> }

And on my remote host server :

> [WARNING]: provided hosts list is empty, only localhost is available
> 
> PLAY [127.0.0.1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [127.0.0.1]  
> TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> [WARNING]: Failure when attempting to use callback plugin (\<ansible.plugins.callback.default.CallbackModule object at 0x28c21d0\>): not all arguments converted during string formatting
> 
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> 127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0

Ansible bug in my remote-host version ??  
Remote Host =\> 2.1.0  
Localhost =\> 1.5.4

😢

---

<div class="post-metadata">

**Author:** ![Clement\_VALENTIN](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Clement\_VALENTIN](https://forum.ansible.com/u/Clement_VALENTIN)\
**Post date:** [May 10, 2016, 3:30pm UTC](https://forum.ansible.com/t/how-to-use-dictionnary-with-multiple-elements/21394/6 "2016-05-10T15:30:28Z")

</div>

I have try with version 2.1.0 in localhost and work fine…

Python version ?  
local =\> Python 2.7.6  
remote =\> Python 2.7.3

I try to upgrade Python version…
