Skip to content

Instantly share code, notes, and snippets.

@hatifnatt
Last active August 5, 2017 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatifnatt/099dcbff9dc753fa05b09ba7242aae93 to your computer and use it in GitHub Desktop.
Save hatifnatt/099dcbff9dc753fa05b09ba7242aae93 to your computer and use it in GitHub Desktop.
Recursive dictionary merge
userconf:
auto-upgrade: False
test: False
sharedcmd:
schedule:
minute: 30
hour: 0
daymonth: '*'
month: '*'
dayweek: '*'
acme_sh:
root:
userconf:
accountemail: nnm@nm.uk
home: /root/.acme.sh
auto-upgrade: False
test: False
sharedcmd:
command: some-command
{%- import_yaml slspath ~ "/defaults.yaml" as defaults %}
{%- for username, user in pillar.get('acme_sh', {}).items() %}
# Some trick by @whytewolf https://gist.github.com/whytewolf/7a113e8a748bb150aa6c97bb35df9929
# Docs will be helpful to understand https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.grains.html#salt.modules.grains.filter_by
# We have dictionary 'defaults' with key 'userconf' grains.filter_by will select value of
# defaults['userconf'] because it's filtered by default='userconf', than it recursively
# merge value of defaults['userconf'] with value of 'merge' dict. In our case it is user['userconf']
{%- set userconf = salt.grains.filter_by(defaults, default='userconf', merge=user['userconf']) %}
test_{{ username }}_defaults:
test.configurable_test_state:
- changes: False
- comment: "{{ defaults }}"
test_{{ username }}_defaults_userconf:
test.configurable_test_state:
- changes: False
- comment: "{{ defaults['userconf'] }}"
test_{{ username }}_userconf_updated:
test.configurable_test_state:
- changes: False
- comment: "{{ userconf }}"
{%- endfor -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment