import subprocess import json def hashrocket_flatten_dict(input_dict): """Flattens the output from Facter 3""" result_dict = {} for fact_name, fact_value in input_dict.items(): if type(fact_value) == dict: # Need to recurse at this point # pylint: disable=line-too-long for new_key, new_value in hashrocket_flatten_dict( fact_value).items(): fact_name = fact_name.encode('ascii', 'ignore') new_key = new_key.encode('ascii', 'ignore') try: new_value = new_value.encode('ascii', 'ignore') except AttributeError: pass result_dict['=>'.join([fact_name, new_key])] = new_value else: result_dict[fact_name] = fact_value return result_dict def get_ohai_report(log_path): report = None ohai_path = '/usr/local/bin/ohai' command = [ohai_path, '-L', log_path] report = subprocess.check_output(command) ohai = json.loads(report) return hashrocket_flatten_dict(ohai) ohai = get_ohai_report('/Users/Brandon/Desktop/log') for key, val in ohai.items(): print key, val