Created
August 25, 2015 01:19
-
-
Save phaer/890d261712e42bcd1aa7 to your computer and use it in GitHub Desktop.
Cfg file to defaults/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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