# Merge Dictionary

**URL:** <https://forum.ansible.com/t/merge-dictionary/33006>\
**Category:** Ansible Project\
**Created:** [May 22, 2020, 11:23am UTC](https://forum.ansible.com/t/merge-dictionary/33006 "2020-05-22T11:23:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isa](https://avatars.discourse-cdn.com/v4/letter/i/c68b51/32.png) [@Isa](https://forum.ansible.com/u/Isa)\
**Post date:** [May 22, 2020, 11:23am UTC](https://forum.ansible.com/t/merge-dictionary/33006/1 "2020-05-22T11:23:51Z")

</div>

Hi All

How can I merge dictionary with the same name.

Example:

**groups\_vars/all**

`  
users:

- name: jsmith  
comment: ‘John Smith’  
shell: /bin/bash  
groups: wheel  
``

`

**group\_vars/postgresql.yml**

` `users:` `- name: tdoe` `comment: ‘Tom Doe’` `shell: /bin/bash` `groups: wheel`

```auto

`

**host_vars/server1**

`
``users:``
``- name: iwhite``
``comment: 'Ian White'``
``shell: /bin/bash``
``groups: wheel``
`

The usecase for this is some users should only have access to a few servers while other users have access to all etc. I also dont want to duplicate users in many yaml files.

If the above dictionary was merged then if my inventory looks like this

`
[postgres]

sererp

[haproxy]
server1
[nonprod]

serverd
`

jsmith will haev access to serverp/1/d
tdoe will have access to serverp
iwhite will have access to server1

I have found similar question on the internet, but most were a few years old. I hoping something has changed.
```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 22, 2020, 1:13pm UTC](https://forum.ansible.com/t/merge-dictionary/33006/2 "2020-05-22T13:13:26Z")

</div>

The merging of the dictionaries would require unique keys. Instead, it's  
possible to concatenate a list of dictionaries. For example

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_users: "{{ my\_users|default() + my\_vars.users }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- group\_vars/all  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- group\_vars/postgresql.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- host\_vars/server1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_vars: "{{ lookup('file', item)|from\_yaml }}"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Isa](https://avatars.discourse-cdn.com/v4/letter/i/c68b51/32.png) [@Isa](https://forum.ansible.com/u/Isa)\
**Post date:** [May 22, 2020, 4:58pm UTC](https://forum.ansible.com/t/merge-dictionary/33006/3 "2020-05-22T16:58:10Z")

</div>

Thanks that works , I wish it was cleaner though. Appreciate the help

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 22, 2020, 6:37pm UTC](https://forum.ansible.com/t/merge-dictionary/33006/4 "2020-05-22T18:37:00Z")

</div>

You're welcome. Just curious what might be cleaner? Could you describe your  
idea in any language, or meta perhaps?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 22, 2020, 10:05pm UTC](https://forum.ansible.com/t/merge-dictionary/33006/5 "2020-05-22T22:05:51Z")

</div>

(list in the loop)

---

<div class="post-metadata">

**Author:** ![Shaun\_Rampersad](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shaun\_Rampersad](https://forum.ansible.com/u/Shaun_Rampersad)\
**Post date:** [May 24, 2020, 2:10pm UTC](https://forum.ansible.com/t/merge-dictionary/33006/6 "2020-05-24T14:10:36Z")

</div>

An Simple example from Puppet that we have working. We have a firewall manifest that applies rules based on a hash. We simply lookup all hashes and create resources.

Puppet Code

`

$fhl\_firewall = hiera\_hash(‘profile::base::firewall’, undef)  
if $fhl\_firewall != undef {  
create\_resources(firewall, $fhl\_firewall)  
}

`

Heira layout:

`

├── common.yaml

├── internal  
│ ├── nonprod  
│ │ ├── elasticsearch.yaml  
│ │ └── logstash.yaml

`

common.yaml:

`

profile::base::firewall:  
‘100 allow Prometheus node\_exporter’:  
proto:

- ‘tcp’  
dport:
- ‘9100’  
action: ‘accept’

`

internal/nonprod/elasticsearch.yaml

`

profile::base::firewall:  
‘100 allow 9200 from host1’:  
proto: ‘tcp’  
dport: ‘9200’  
action: ‘accept’  
source: ‘192.168.101.15/32’  
‘100 allow 9200 from host2’:  
proto: ‘tcp’  
dport: ‘9200’  
action: ‘accept’  
source: ‘192.168.101.16/32’

`

internal/nonprod/logstash.yaml

`

profile::base::firewall:  
‘100 allow kibana’:  
proto: ‘tcp’  
dport: ‘5601’  
action: ‘accept’  
‘100 allow log\_forwarder’:  
proto: ‘tcp’  
dport:

- ‘10510-10520’  
action: ‘accept’

`

Hope this makes sense.

PS: I’m part of Isa’s team 🙂 I’ll also take a look at the ansible plugin

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 25, 2020, 9:25am UTC](https://forum.ansible.com/t/merge-dictionary/33006/7 "2020-05-25T09:25:38Z")

</div>

Let me know if this might meet the high standards of the code.
