Replacing dictionary key values

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

You do have an option :slight_smile: Set the 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.

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

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.