# Replacing dictionary key values

**URL:** <https://forum.ansible.com/t/replacing-dictionary-key-values/18211>\
**Category:** Ansible Project\
**Created:** [May 12, 2015, 2:59pm UTC](https://forum.ansible.com/t/replacing-dictionary-key-values/18211 "2015-05-12T14:59:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_McNulty](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@John\_McNulty](https://forum.ansible.com/u/John_McNulty)\
**Post date:** [May 12, 2015, 2:59pm UTC](https://forum.ansible.com/t/replacing-dictionary-key-values/18211/1 "2015-05-12T14:59:44Z")

</div>

Hi, can someone explain this to me please.

Say I have a dictionary in a role defaults like this …

`  
at\_service:  
pkg\_name: at  
pkg\_state: present  
svc\_name: atd  
svc\_enabled: no  
svc\_state: stopped

`

I can replace that with a higher priority declaration in group\_vars, or a role vars file and change some of the values, e.g.

`  
at\_service:  
pkg\_name: at  
pkg\_state: present  
svc\_name: atd  
svc\_enabled: yes  
svc\_state: started

`

… and that’s fine. But why can’t I just replace the key valuesI want to change, like this …

`at_service.svc_enabled: yes at_service.svc_state: started`

If I do that the change is just ignored. Seems I have no option but to replace all of it.

Thanks, John

---

<div class="post-metadata">

**Author:** ![Pascal\_Mathis](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@Pascal\_Mathis](https://forum.ansible.com/u/Pascal_Mathis)\
**Post date:** [May 12, 2015, 3:06pm UTC](https://forum.ansible.com/t/replacing-dictionary-key-values/18211/2 "2015-05-12T15:06:35Z")

</div>

You do have an option 🙂 Set the **[hash\_behaviour](http://docs.ansible.com/intro_configuration.html#hash-behaviour)** in your ansible.cfg to **merge**.  
Then you’re able to write your second file like this:

`  
at\_service:  
svc\_enabled: yes  
svc\_state: started

`

Which will result in the following dictionary, after merging it with the first one:

`  
at\_service:  
pkg\_name: at  
pkg\_state: present  
svc\_name: atd  
svc\_enabled: yes  
svc\_state: started

`

Not all people like that setting though, so keep that in mind.

---

<div class="post-metadata">

**Author:** ![John\_McNulty](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@John\_McNulty](https://forum.ansible.com/u/John_McNulty)\
**Post date:** [May 12, 2015, 3:19pm UTC](https://forum.ansible.com/t/replacing-dictionary-key-values/18211/3 "2015-05-12T15:19:32Z")

</div>

So you’re saying it’s not possible to replace the value of a dictionary key without changing hash\_behaviour to “merge”?

---

<div class="post-metadata">

**Author:** ![Adam\_Hamsik](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Adam\_Hamsik](https://forum.ansible.com/u/Adam_Hamsik)\
**Post date:** [May 13, 2015, 4:38pm UTC](https://forum.ansible.com/t/replacing-dictionary-key-values/18211/4 "2015-05-13T16:38:34Z")

</div>

Yes.

Question is if it would be possible to write a module which would be able to do this.

merge\_val: orig=`at_service.svc_state value=started`

`P.S.`  
`I have had problems with this, too. Only solution with which I was able to come up was using filters to create new variable which is then later used. But I'm not very happy with that.`
