# merge dictionary list in jinja2

**URL:** <https://forum.ansible.com/t/merge-dictionary-list-in-jinja2/26799>\
**Category:** Ansible Project\
**Created:** [February 20, 2018, 9:46am UTC](https://forum.ansible.com/t/merge-dictionary-list-in-jinja2/26799 "2018-02-20T09:46:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deewon](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@deewon](https://forum.ansible.com/u/deewon)\
**Post date:** [February 20, 2018, 9:46am UTC](https://forum.ansible.com/t/merge-dictionary-list-in-jinja2/26799/1 "2018-02-20T09:46:59Z")

</div>

Hi all,

I’m relatively new to using ansible and hoping someone can assist with some insight around this.

I’m trying to create a jinja2 template that generates authentication credentials for a database in json format

I’ve defined the variables using the format below (using dictionaries)

vars:  
db\_roles:  
user1:

- { db: “lab2”, privs: “READ,DELETE” }  
user2:
- { db: “lab1”, privs: “INSERT,DELETE” }
- { db: “lab2”, privs: “UPDATE” }

The idea is to loop through the users (user1 and user2 in this example) and assign the “privs” on the “db” for each user across multiple databases as needed

To do this, I created a jinja2 template that looks like the below:

{% for item in db\_roles %}  
{% for dict\_item in db\_roles[item] %}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 20, 2018, 11:34am UTC](https://forum.ansible.com/t/merge-dictionary-list-in-jinja2/26799/2 "2018-02-20T11:34:00Z")

</div>

You need to move your { "db" up one level.

{% for item in db\_roles %}

---

<div class="post-metadata">

**Author:** ![George\_Shuklin](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@George\_Shuklin](https://forum.ansible.com/u/George_Shuklin)\
**Post date:** [February 20, 2018, 1:24pm UTC](https://forum.ansible.com/t/merge-dictionary-list-in-jinja2/26799/3 "2018-02-20T13:24:08Z")

</div>

Try to use 'combine' filter:

one: {foo: bar}  
two: {baz: bazbaz }  
three: '{{one|combine(two)}}'

Result: three: {foo: bar, baz: bazbaz}

---

<div class="post-metadata">

**Author:** ![deewon](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@deewon](https://forum.ansible.com/u/deewon)\
**Post date:** [February 21, 2018, 5:23pm UTC](https://forum.ansible.com/t/merge-dictionary-list-in-jinja2/26799/4 "2018-02-21T17:23:25Z")

</div>

Thanks Kai.

The example you gave came pretty close (see result below for user2) but it kind of pointed me in the right direction and I think I’ve fixed it

{ “db” : “admin”, “userName” : “user2”,

“roles” : [

{  
“db” : “lab1”,  
“role” : “INSERT”  
},

{  
“db” : “lab1”,  
“role” : “DELETE”  
}

“roles” : [

{  
“db” : “lab2”,  
“role” : “UPDATE”  
}

}

From the above, I still get 2 separate roles array for a single user but in addition to the above, If I move the dictionary items lookup into the "roles: [ " section, I seem to achieve the desired result i.e.

{% for item in db\_roles %}

---

<div class="post-metadata">

**Author:** ![deewon](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@deewon](https://forum.ansible.com/u/deewon)\
**Post date:** [February 21, 2018, 5:25pm UTC](https://forum.ansible.com/t/merge-dictionary-list-in-jinja2/26799/5 "2018-02-21T17:25:32Z")

</div>

Thanks for the hint
