# "hash\_behaviour = merge" issue

**URL:** <https://forum.ansible.com/t/hash-behaviour-merge-issue/24376>\
**Category:** Ansible Project\
**Created:** [May 2, 2017, 2:01pm UTC](https://forum.ansible.com/t/hash-behaviour-merge-issue/24376 "2017-05-02T14:01:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Clark](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@Tim\_Clark](https://forum.ansible.com/u/Tim_Clark)\
**Post date:** [May 2, 2017, 2:01pm UTC](https://forum.ansible.com/t/hash-behaviour-merge-issue/24376/1 "2017-05-02T14:01:07Z")

</div>

Hi,

I would like to build up a variable from defaults, group\_vars and host\_vars - but hash\_behaviour doesn’t seem to be working as I’d expect it to.

The plan is to have a default config (in this case haproxy) and then allow additional frontends to be defined at group and host\_var level.

I’ve created a very simple example which hopefully explains the issue:

[https://github.com/tdac/Ansible-HashMerge-Issue](https://github.com/tdac/Ansible-HashMerge-Issue)

I’m wondering if the “-” notation in the inventory stops Ansible seeing this as mergable?

Thanks in advance for any pointers you can give me!

Tim

---

<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:** [May 2, 2017, 7:01pm UTC](https://forum.ansible.com/t/hash-behaviour-merge-issue/24376/2 "2017-05-02T19:01:17Z")

</div>

What you have is a list not a hash aka dictionary.  
The documentation says it only works on hash/dictionary so I guess thats why.

---

<div class="post-metadata">

**Author:** ![Tim\_Clark](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@Tim\_Clark](https://forum.ansible.com/u/Tim_Clark)\
**Post date:** [May 5, 2017, 12:34pm UTC](https://forum.ansible.com/t/hash-behaviour-merge-issue/24376/3 "2017-05-05T12:34:53Z")

</div>

Thanks Kai,

That would explain it - is there a ‘standard’ way of merging a list in this context?

Tim

---

<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:** [May 5, 2017, 2:21pm UTC](https://forum.ansible.com/t/hash-behaviour-merge-issue/24376/4 "2017-05-05T14:21:22Z")

</div>

Standard in what situation?  
List is usually used in a loop, and the with\_items can have several entries so you'll have a merge there.

with\_items:  
&nbsp;&nbsp;&nbsp;- haproxy\_frontend\_host | default()  
&nbsp;&nbsp;&nbsp;- haproxy\_frontend\_group | default()

---

<div class="post-metadata">

**Author:** ![Tim\_Clark](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@Tim\_Clark](https://forum.ansible.com/u/Tim_Clark)\
**Post date:** [May 11, 2017, 8:09pm UTC](https://forum.ansible.com/t/hash-behaviour-merge-issue/24376/5 "2017-05-11T20:09:51Z")

</div>

Hi Kai,

Thanks - so if I’ve understood correctly - I could set:

haproxy\_frontend\_group:

- name: http

in group\_vars

and

haproxy\_frontend\_host:

- name: https

and then use your ‘with items’ suggestion to combine them…I suppose my dream was to have list variables that work like hashes do. For example If one defines a default configuration for a group of servers in group\_vars - and then want to add ‘special cases’ at host\_vars level - that’s currently not possible with lists (which is a shame - as lists offer a richer way to define services I think?).

Is there a theoretical reason why lists can’t be merged the same way as hashes?

Tim

---

<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:** [May 12, 2017, 2:13pm UTC](https://forum.ansible.com/t/hash-behaviour-merge-issue/24376/6 "2017-05-12T14:13:00Z")

</div>

Yes, but I forgot the curly brackets, it should be.

&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;- '{{ haproxy\_frontend\_host | default() }}'  
&nbsp;&nbsp;&nbsp;- '{{ haproxy\_frontend\_group | default() }}'
