Advertisement
Guest User

change-nested-dict-key-value-ansible

a guest
Jun 19th, 2017
1,229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 1.10 KB | None | 0 0
  1. ---
  2. - hosts: localhost
  3.   connection: local
  4.   gather_facts: no
  5.  
  6.   vars:
  7.     dict_one:
  8.       dict_two:
  9.         variables:
  10.           test: please-change-me # <-- I want to just change this variable.
  11.           another: value
  12.       dict_four:
  13.         test: another unique example
  14.  
  15.   tasks:
  16.     - name: Change dict_one.dict_two.variables.test to "changed".
  17.       set_fact:
  18.         dict_one_changes: "{{ dict_one.dict_two.variables|combine({'test': 'changed'}) }}"
  19.  
  20.     - debug: var=dict_one_changes
  21.     # "dict_one_changes": {
  22.     #   "another": "value",
  23.     #   "test": "changed"
  24.     # }
  25.  
  26.     - name: Merge changes back into dict_one.
  27.       set_fact:
  28.         dict_one: "{{ dict_one|combine(dict_one_changes) }}"
  29.  
  30.     - debug: var=dict_one
  31.     # "dict_one": {
  32.     #   "another": "value",
  33.     #   "dict_four": {
  34.     #     "test": "example to not be overridden"
  35.     #   },
  36.     #   "dict_two": {
  37.     #     "variables": {
  38.     #       "another": "value",
  39.     #       "test": "please-change-me"
  40.     #     }
  41.     #   },
  42.     #   "test": "changed" <-- combined dict change goes to root level
  43.     # }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement