A custom YAML tag for referencing environment variables in YAML files.
Low maintenance activity. Last release 19 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install pyyaml-env-tagpyyaml_env_tag
A custom YAML tag for referencing environment variables in YAML files.
Installation
Install the pyyaml_env_tag package with pip:
pip install pyyaml_env_tag
Enabling the tag
To enable the tag, pass your loader of choice into the add_env_tag function, which will
return the loader with the construstor added to it.
import yaml
from yaml_env_tag import add_env_tag
myLoader = add_env_tag(yaml.Loader)
Then you may use the loader as per usual. For example:
yaml.load(data, Loader=myLoader)
The add_env_tag is a high level helper function. If you need lower level access, you may
add the constructor (yaml_env_tag.construct_env_tag) to the loader directly using the
add_constructor method of the loader. Note that this requires that the tag (!ENV) be
defined as well.
from yaml_env_tag import construct_env_tag
Loader.add_constructor('!ENV', construct_env_tag)
Using the tag
Include the tag !ENV followed by the name of an environment variable in a YAML
file and the value of the environment variable will be used in its place.
key: !ENV SOME_VARIABLE
If SOME_VARIABLE is set to A string!, then the above YAML would result in the
following Python object:
{'key': 'A string!'}
The content of the variable is parsed using YAML's implicit scalar types, such as
string, bool, integer, float, datestamp and null. More complex types are not
recognized and simply passed through as a string. For example, if SOME_VARIABLE
was set to the string , then the above YAML would result in the following: