From 5ba7f21ce68d2f0e9db74c6a754692b4c6f78e0d Mon Sep 17 00:00:00 2001 From: Sai Sindhur Malleni Date: Fri, 9 Sep 2016 17:08:01 -0400 Subject: [PATCH] Fix Metadata Generation With the current model of dumping ansible facts with the template and regexing it to make it JSON readable, metadata generation into files is failing in some cases. It is better to have the template handle creation of a serializable JSON object and that is what this commit does. Also some unnecessary keys in lib/Metadata.py used to parse the JSON have been removed since all of that is now handled by the template. Change-Id: I7d4a4f9fde0af8a52aa72374614c20f4b8abc421 Co-Authored-By: Alex Krzos --- ansible/gather/dump_facts.j2 | 7 ++++--- lib/Metadata.py | 12 +++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/ansible/gather/dump_facts.j2 b/ansible/gather/dump_facts.j2 index 9b4819650..9b413be8d 100644 --- a/ansible/gather/dump_facts.j2 +++ b/ansible/gather/dump_facts.j2 @@ -1,12 +1,13 @@ +[ {% for host in groups['controller'] %} -{{hostvars[host]| to_nice_json}} +{{hostvars[host]| to_nice_json}}, {% endfor %} {% for host in groups['compute'] %} -{{hostvars[host]| to_nice_json}} +{{hostvars[host]| to_nice_json}}, {% endfor %} {% for host in groups['undercloud'] %} {{hostvars[host]| to_nice_json}} {% endfor %} - +] diff --git a/lib/Metadata.py b/lib/Metadata.py index a8ef41002..37745562b 100644 --- a/lib/Metadata.py +++ b/lib/Metadata.py @@ -29,15 +29,13 @@ class Metadata: except IOError: print("Machine facts json is missing") exit(1) - new_json = re.sub(r"}\n{", r"},\n{", json_str, re.M) - convert = "{ \"machines\": [" + new_json + "] }" sys_data = {} - sys_data['system_data'] = json.loads(convert) + sys_data['system_data'] = json.loads(json_str) return sys_data def get_hardware_metadata(self, sys_data): hard_dict = {} - for item in sys_data['system_data']['machines']: + for item in sys_data['system_data']: if 'hardware_details' not in hard_dict: hard_dict['hardware_details'] = [] hardware_dict = {} @@ -57,7 +55,7 @@ class Metadata: def get_environment_metadata(self, sys_data): env_dict = {} - for item in sys_data['system_data']['machines']: + for item in sys_data['system_data']: if 'environment_setup' not in env_dict: env_dict['environment_setup'] = {} for key, value in item.items(): @@ -67,7 +65,7 @@ class Metadata: def get_software_metadata(self, sys_data): soft_all_dict = {} - for item in sys_data['system_data']['machines']: + for item in sys_data['system_data']: if 'software_details' not in soft_all_dict: soft_all_dict['software_details'] = {} nodes = ['controller', 'undercloud', 'compute'] @@ -93,7 +91,7 @@ class Metadata: def write_metadata_file(self, data, filename): with open(filename, 'w') as json_file: - json.dump(data, json_file) + json.dump(data, json_file, indent=4, sort_keys=True) def main():