# Dictionary lookups being clobbered when assigning values to children elsewhere

**URL:** <https://forum.ansible.com/t/dictionary-lookups-being-clobbered-when-assigning-values-to-children-elsewhere/19770>\
**Category:** Ansible Project\
**Created:** [November 15, 2015, 1:37pm UTC](https://forum.ansible.com/t/dictionary-lookups-being-clobbered-when-assigning-values-to-children-elsewhere/19770 "2015-11-15T13:37:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmacrae](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@cmacrae](https://forum.ansible.com/u/cmacrae)\
**Post date:** [November 15, 2015, 1:37pm UTC](https://forum.ansible.com/t/dictionary-lookups-being-clobbered-when-assigning-values-to-children-elsewhere/19770/1 "2015-11-15T13:37:27Z")

</div>

Hey People,

Having some trouble using YAML dictionaries.  
I'm refactoring my variable structure in a role a wrote a while ago.

The variables used to all look like: sensu\_api\_host, but I wanted to move to using dictionaries, so when referencing them it wouldn't be:  
{{ sensu\_api\_host }}  
it'd be:  
{{ sensu['api']['host'] }}

In doing so, I've restructured my roles' defaults/main.yml like so:  
sensu:  
&nbsp;&nbsp;api:  
&nbsp;&nbsp;&nbsp;&nbsp;host: "{{ groups['sensu\_masters'][0] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;port: 4567  
&nbsp;&nbsp;&nbsp;&nbsp;ssl: "false"  
&nbsp;&nbsp;&nbsp;&nbsp;user\_name: admin  
&nbsp;&nbsp;&nbsp;&nbsp;password: secret  
&nbsp;&nbsp;&nbsp;&nbsp;uchiwa\_path: ''  
&nbsp;&nbsp;&nbsp;&nbsp;timeout: 5000  
&nbsp;&nbsp;client\_config: client.json.j2  
&nbsp;&nbsp;config\_path: /opt/local/etc/sensu  
&nbsp;&nbsp;gem\_state: present  
&nbsp;&nbsp;plugin\_gem\_state: present  
&nbsp;&nbsp;user\_name: sensu  
&nbsp;&nbsp;group\_name: sensu  
&nbsp;&nbsp;include\_plugins: true  
&nbsp;&nbsp;include\_dashboard: false  
&nbsp;&nbsp;master: false

Though, I have a problem. Lookups for the above variables work fine... until I try to assign them values elsewhere, where it should take precedence.

Some of the key values above are clobbered when they are defined elsewhere, such as host\_vars/hostname.yml or group\_vars/groupname.yml.  
It seems that if I want to define one of the nested keys' value elsewhere, it ruins the dictionary lookup for all other child keys who share that parent.

For example, above you can see that sensu['include\_dashboard'] is set to false.  
If I want to set this to `true` for a particular host, in host\_vars/hostname.yml like so:

---

<div class="post-metadata">

**Author:** ![Rob\_Wilkerson](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@Rob\_Wilkerson](https://forum.ansible.com/u/Rob_Wilkerson)\
**Post date:** [November 15, 2015, 3:43pm UTC](https://forum.ansible.com/t/dictionary-lookups-being-clobbered-when-assigning-values-to-children-elsewhere/19770/2 "2015-11-15T15:43:35Z")

</div>

I’m still new to Ansible, but my understanding is that it overwrites variables by default, including dictionary variables. This is because the default value for hash\_behavior setting is “replace”. If you change that config value to “merge”, you’ll see the behavior you’re expecting, I believe.

I don’t fully understand why replace is preferred, but I’ve found it easier to just avoid dictionary variables just to keep things simple. Seems like some changes have been made in v2.0.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 15, 2015, 3:47pm UTC](https://forum.ansible.com/t/dictionary-lookups-being-clobbered-when-assigning-values-to-children-elsewhere/19770/3 "2015-11-15T15:47:41Z")

</div>

The default behavior of Ansible is to “replace” the entire data structure instead of merging it. This is defined/configured by the hash\_behavior parameter in an ansible.cfg file.

Because switching to merge is a process that requires a user to modify their Ansible configuration, I feel that is most appropriate to _not_ use nested data structures where you expect users to provide configurations for your role, but instead use what you were doing previously with sensu\_api\_host.

---

<div class="post-metadata">

**Author:** ![cmacrae](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@cmacrae](https://forum.ansible.com/u/cmacrae)\
**Post date:** [November 15, 2015, 6:11pm UTC](https://forum.ansible.com/t/dictionary-lookups-being-clobbered-when-assigning-values-to-children-elsewhere/19770/4 "2015-11-15T18:11:07Z")

</div>

You’re absolutely right - thanks for taking the time to point this out!

---

<div class="post-metadata">

**Author:** ![cmacrae](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@cmacrae](https://forum.ansible.com/u/cmacrae)\
**Post date:** [November 15, 2015, 6:11pm UTC](https://forum.ansible.com/t/dictionary-lookups-being-clobbered-when-assigning-values-to-children-elsewhere/19770/5 "2015-11-15T18:11:39Z")

</div>

Thanks, Matt.  
In that case, I’ll keep it the way I’ve written it 🙂
