# combining dicts in a list

**URL:** <https://forum.ansible.com/t/combining-dicts-in-a-list/24638>\
**Category:** Ansible Project\
**Created:** [June 2, 2017, 4:26pm UTC](https://forum.ansible.com/t/combining-dicts-in-a-list/24638 "2017-06-02T16:26:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastien.barthelemy](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sebastien.barthelemy](https://forum.ansible.com/u/sebastien.barthelemy)\
**Post date:** [June 2, 2017, 4:26pm UTC](https://forum.ansible.com/t/combining-dicts-in-a-list/24638/1 "2017-06-02T16:26:29Z")

</div>

Hello,

I have a list of dicts, and I would like to add a (key, value) pair to each dict (ie. to each element of the list).

Typically, I have this list:

- port: 2000  
release: trusty
- port: 2001  
release: xenial

And I would like to create this one

- port: 2000  
release: trusty  
name: trusty-2000
- port: 2001  
release: xenial  
name: xenial-2001

Is it possible? I tried using the “map(‘combine’)” filter (see below) but do see how to compute a different value for each element.  
This is probably something obvious that I’m missing.

Thank you!

$ cat list.yaml

- hosts: localhost  
vars:
- containers:
- port: 2000  
release: trusty
- port: 2001  
release: xenial
- containers\_with\_name: “{{ containers | map(‘combine’, {‘name’: ‘TODO:release-port’ }) | list }}”  
tasks:
- debug:  
msg: “containers\_with\_name: {{ containers\_with\_name }}”

$ ansible-playbook list.yaml  
[WARNING]: provided hosts list is empty, only localhost is available  
PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]  
TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “containers\_with\_name: [{u’release’: u’trusty’, u’port’: 2000, ‘name’: ‘TODO:release-port’}, {u’release’: u’xenial’, u’port’: 2001, ‘name’: ‘TODO:release-port’}]”  
}  
PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=0 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 6, 2017, 5:27am UTC](https://forum.ansible.com/t/combining-dicts-in-a-list/24638/2 "2017-06-06T05:27:12Z")

</div>

Try this. Its not exactly pretty, but it seems to do what you need.

$ cat listtest-playbook.yml

---

<div class="post-metadata">

**Author:** ![sebastien.barthelemy](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sebastien.barthelemy](https://forum.ansible.com/u/sebastien.barthelemy)\
**Post date:** [June 12, 2017, 12:46pm UTC](https://forum.ansible.com/t/combining-dicts-in-a-list/24638/3 "2017-06-12T12:46:20Z")

</div>

Hello,

thank you for your answer, and sorry for my late one.

> Try this. Its not exactly pretty,

indeed ☹  
I’m growing increasingly frustrated by the many quirks and lack of expressibility of ansible’s dialect .

This would have been peanuts to write in python. I probably investigate how to write python module/filters in order to avoid jinja-ed yaml.

> but it seems to do what you need.

Yes, thank you.  
For my real use-case, I ended-up using with\_subelements (see below), but I’ll recall how you use set\_fact.

$ cat hosts  
[jenkins\_masters]  
ald-1545-de  
[jenkins\_slaves\_holders]  
ald-1545-de  
ald-1469-de  
[jenkins\_slaves\_holders:vars]  
jenkins\_slaves=[{‘release’: ‘trusty’, ‘ssh\_port’: 2000},{‘release’: ‘xenial’, ‘ssh\_port’: 2001}]

$ cat main.yaml

# let ansible know about each created container

# add\_host “bypasses the play host loop”: it will only be called from one host.

# Let this host be “localhost”, for symmetry.

- hosts: localhost  
tasks:
- add\_host:  
hostname: “{{ item.0.inventory\_hostname\_short }}-build-{{ item.1.release }}”  
container\_holder\_inventory\_hostname\_short: “{{ item.0.inventory\_hostname\_short }}”  
ansible\_port: “{{ item.1.ssh\_port }}”  
ansible\_host: “{{ item.0.ansible\_host }}”  
ansible\_user: ‘root’  
jenkins\_master: “{{ groups[‘jenkins\_masters’] | first | mandatory }}”  
groups: ‘build,jenkinsslaves’  
with\_subelements:
- “{{ groups[‘jenkins\_slaves\_holders’] | map(‘extract’, hostvars) | list }}”
- jenkins\_slaves

Thank you again!
