Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference environment variables in yaml #141

Closed
somnmos opened this issue Mar 7, 2018 · 2 comments
Closed

Reference environment variables in yaml #141

somnmos opened this issue Mar 7, 2018 · 2 comments
Labels

Comments

@somnmos
Copy link

somnmos commented Mar 7, 2018

I've found that many people have the need to reference environment variables in yaml files, which must be a better way to popularize yaml usage. Although I can do this by replace the regex, i think it better for yaml to provides this functionality.

@perlpunk
Copy link
Member

perlpunk commented Mar 7, 2018

It should not be something a YAML processor does by default. (It is also not nice regarding security.)
Also, the syntax how to reference the environment variables is missing in your suggestion.

@ingydotnet
Copy link
Member

@perlpunk is right that this is not a desirable default behavior, but here is a simple solution that might interest you:

import os, yaml

yaml_text = """\
user: !env USER
home: !env HOME
path: !env PATH
"""

yaml.add_constructor('!env', lambda loader, node: os.environ[node.value])

print yaml.load(yaml_text)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants