Skip to content

Instantly share code, notes, and snippets.

@phaer
Created August 25, 2015 01:19
Show Gist options
  • Save phaer/890d261712e42bcd1aa7 to your computer and use it in GitHub Desktop.
Save phaer/890d261712e42bcd1aa7 to your computer and use it in GitHub Desktop.
Cfg file to defaults/main.yml
import sys
import re
"""
Loops over a file with configuration in the form of
#key = value
and produces a yaml file suitable for ansibles defaults/main.yml
"""
if __name__ == '__main__':
if len(sys.argv) is not 3:
print('usage: {} config-file yaml-file'.format(sys.argv[0]))
sys.exit(1)
with open(sys.argv[1], 'r') as f:
lines = f.readlines()
with open(sys.argv[1], 'w') as f, open(sys.argv[2], 'w') as d:
d.write('---\n')
for line in lines:
match = re.match('^#([^ ][^=]+)=(.*)$', line)
if match:
key, value = (match.group(1).strip(), match.group(2).strip())
d.write('{key}: "{value}"\n'.format(
key=key.lower(),
value=value
))
f.write('{0} = {{{{{1}}}}}'.format(key, key.lower()))
else:
f.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment