browbeat/ansible/gather/roles/common/files/openstack-config-parser.py
jkilpatr 522f9031e1 Openstack Metadata refactor
This moves openstack metadata collection to a much more
general format, instead of specifying exactly what to collect
a short python script has been created that grabs all key/value
pairs from the various openstack config files.

Co-Authored-By: Joe Talerico <jtaleric@redhat.com>

Change-Id: I6b653dac344c5ca27fa1010f345bc99a5591379e
2016-09-19 13:36:12 -04:00

56 lines
1.6 KiB
Python

import sys
# usage: openstack-config-parser.py [service] [config file] [output file]
def parse_config(serviceName, fileName):
# a dict containing key/value
# pairs, last value is what is
# stored.
values = {}
with open(fileName) as config:
for line in config:
pair = line.replace('#', '')
pair = pair.replace('\n', '')
pair = pair.replace('"', '')
pair = pair.replace('\\', '')
pair = pair.replace(' ', '')
pair = pair.replace('<', '')
pair = pair.replace('>', '')
pair = pair.split('=')
# excludes any line without a key/val pair
valid_line = not line.startswith(
"# ") and '[' not in line and line != '\n' and line != '#\n' and "password" not in line.lower()
if '#' not in line and valid_line:
values["openstack_" + serviceName + "_" + pair[0]] = pair[1]
return values
def try_type(val):
try:
int(val)
return val
except ValueError:
try:
float(val)
return val
except ValueError:
if val.lower() in ("true", "false"):
return val
else:
return "\"" + val + "\""
def print_vars_file(values, fileName):
with open(fileName, 'w') as output:
for key in values:
output.write(key + ": " + try_type(values[key]) + "\n")
def main():
output = parse_config(sys.argv[1], sys.argv[2])
print_vars_file(output, sys.argv[3])
if __name__ == '__main__':
sys.exit(main())