Replacing dictionary key values

283 views
Skip to first unread message

John McNulty

unread,
May 12, 2015, 10:59:44 AM5/12/15
to ansible...@googlegroups.com

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

Message has been deleted

Pascal Mathis

unread,
May 12, 2015, 11:06:35 AM5/12/15
to ansible...@googlegroups.com
You do have an option :) 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.

John McNulty

unread,
May 12, 2015, 11:19:32 AM5/12/15
to ansible...@googlegroups.com


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

Adam Hamsik

unread,
May 13, 2015, 12:38:34 PM5/13/15
to ansible...@googlegroups.com
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.

Reply all
Reply to author
Forward
0 new messages