Skip to main content

Interpolate Yaml files with env vars

Project description

Build Status PyPi package

Interpolate YaML files with environmental variables and other YaML files.

Given a YaML string like:

a: ${A}
b: 2

and an environmental variable $A with value hello, yamlenv.load would return:

{
    a: 'hello',
    b: 2
}

Including YAML literals as environment variables is also supported - so if the environment variable $A was set to false, yamlenv.load would return:

{
    a: False,
    b: 2
}

Default values are supported:

yamlenv.load('''
    a: ${A:-hello}
    b: 2
''') == {
    'a': 'hello',
    'b': 2
}

As in Bash, defaulting can be done with either :- (to not allow empty defaults) or with - to allow empty values.

The environmental variable can be embedded in a larger string, too:

yamlenv.load('''
    a: foo ${A:-bar} baz
    b: 2
''') == {
    'a': 'foo bar baz',
    'b': 2
}

More than one environmental variable can appear in a string:

yamlenv.load('''
    a: foo ${A:-bar} ${B:-baz}
    b: 2
''') == {
    'a': 'foo bar baz',
    'b': 2
}

YaML files can include other YaML files, too. E.g. if b.yaml contains “2”, then:

yamlenv.load('''
    a: 1
    b: !include b.yaml
''') == {
    'a': 1
    'b': 2
}

The included YaML file can be as complex as necessary.

More examples are available in the tests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yamlenv-0.7.1.tar.gz (4.9 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page