# \[Ansible 2.5\] Question about with\_dict and hash\_behaviour = merge

**URL:** <https://forum.ansible.com/t/ansible-2-5-question-about-with-dict-and-hash-behaviour-merge/26855>\
**Category:** Ansible Project\
**Created:** [February 26, 2018, 8:30pm UTC](https://forum.ansible.com/t/ansible-2-5-question-about-with-dict-and-hash-behaviour-merge/26855 "2018-02-26T20:30:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [February 26, 2018, 8:30pm UTC](https://forum.ansible.com/t/ansible-2-5-question-about-with-dict-and-hash-behaviour-merge/26855/1 "2018-02-26T20:30:36Z")

</div>

According to this blog post: [https://www.ansible.com/blog/loop-plays-past-present-future](https://www.ansible.com/blog/loop-plays-past-present-future) with\_dict is going away. All my roles rely heavily on `with_dict` and `hash_behaviour = merge` merging to avoid configuring the same values over and over again. If I need to add or override a value, I just give it the same key in the correct vars file or on the command line.

As in:

roles/common/default.yml

`champions: therock: "Yeah!" me: "Uh oh."`

project/group\_vars/web/main.yml

`champions: me: "I win." them: "We lose."`

project/host\_vars/node1.yml (in web group)

`champions: therock: "boom."`

project/host\_vars/node2.yml (in web group)

`  
#champions just uses defaults.

`

So, node1 would look like:

`champions: therock: "boom." me: "I win." them: "We lose."`

node2:

`champions: therock: "Yeah!" me: "I win." them: "We lose."`

That lets me define common values in as few places as possible.

Is there a way to emulate using lookups?

Is there a better way to avoid defining common values in as few places as possible that I just haven’t seen yet?

What I’m trying to avoid is having a list of common, say iptable rules, configured in group\_vars, but I have to modify or add just one of those rules a bit on a specific node. Using a list, or not merging hashes, would make me copy every single rule in the list from the group\_vars file to the host\_vars file. Otherwise I’d lose all but the one I modified or added.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 26, 2018, 8:35pm UTC](https://forum.ansible.com/t/ansible-2-5-question-about-with-dict-and-hash-behaviour-merge/26855/2 "2018-02-26T20:35:49Z")

</div>

With with\_dict \<- you are already using a lookup, `lookup("dict",  
....`, all with\_\<lookup name\> constructs ARE lookups.

Using a lookup or not, the hash\_behaviour stays unchanged. Also there  
is a filter dict2items that you might want to use instead of the  
lookup.

---

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [February 26, 2018, 8:49pm UTC](https://forum.ansible.com/t/ansible-2-5-question-about-with-dict-and-hash-behaviour-merge/26855/3 "2018-02-26T20:49:43Z")

</div>

So, replace:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: "{{ champions }}"

with:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('dict', champions }}"

?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 26, 2018, 8:50pm UTC](https://forum.ansible.com/t/ansible-2-5-question-about-with-dict-and-hash-behaviour-merge/26855/4 "2018-02-26T20:50:50Z")

</div>

That is one possibility, another:

loop: '{{champions|dict2items}}'
