Skip to content

Instantly share code, notes, and snippets.

@KyleJamesWalker
Last active November 6, 2017 23:51
Show Gist options
  • Save KyleJamesWalker/8a3c8b23263e4bc4034f4f373d84b796 to your computer and use it in GitHub Desktop.
Save KyleJamesWalker/8a3c8b23263e4bc4034f4f373d84b796 to your computer and use it in GitHub Desktop.
Templated yamlsettings example

A quick example to allow easy environment variable injection for yamlsettings in lists. Normally a list can only be completely overriden via environement variables, this allows you to pick specific values to load in to a given entry.

Run with something like:

BAR_PASS=s3cr3t BAZ_PASS=god python example.py
import os
import yamlsettings
from string import Template
def render_environ(path, node):
"""Callback for yamlsettings traverse to render all strings with
environment values
"""
if isinstance(node, str):
return Template(node).substitute(**os.environ)
else:
return None
def main():
settings = yamlsettings.load('settings.yaml')
yamlsettings.update_from_env(settings)
settings.traverse(render_environ)
print(settings)
if __name__ == '__main__':
main()
---
foo: bar
bar_list:
- name: foo bar
secret: ${BAR_PASS}
- name: baz bar
secret: ${BAZ_PASS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment