Skip to content

Instantly share code, notes, and snippets.

@tdickman
Last active March 31, 2020 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tdickman/a44fcf3c275f7183c58dc89fb2d8b0c3 to your computer and use it in GitHub Desktop.
Save tdickman/a44fcf3c275f7183c58dc89fb2d8b0c3 to your computer and use it in GitHub Desktop.
Parse YAML file to bash variables

Usage

eval $(python parse_yaml.py example_file.yaml)

In this example the following variables are accessible:

$key1
$key2_nested
key1: 'test'
key2:
nested: 1
#!/usr/bin/python
import sys
import yaml
def parse(obj, base_prefix=''):
for key in obj:
value = obj[key]
prefix = '{}_{}'.format(base_prefix, key)
if type(value) == dict:
parse(value, prefix)
else:
print('{}="{}"'.format(prefix, value)[1:])
if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
parse(yaml.load(f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment