-
-
Save hatifnatt/099dcbff9dc753fa05b09ba7242aae93 to your computer and use it in GitHub Desktop.
Recursive dictionary merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
userconf: | |
auto-upgrade: False | |
test: False | |
sharedcmd: | |
schedule: | |
minute: 30 | |
hour: 0 | |
daymonth: '*' | |
month: '*' | |
dayweek: '*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
acme_sh: | |
root: | |
userconf: | |
accountemail: nnm@nm.uk | |
home: /root/.acme.sh | |
auto-upgrade: False | |
test: False | |
sharedcmd: | |
command: some-command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{%- 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