Skip to content

Instantly share code, notes, and snippets.

@discentem
Last active October 12, 2016 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save discentem/9b46aeb10a230ea5a4974a56db25ff2c to your computer and use it in GitHub Desktop.
Save discentem/9b46aeb10a230ea5a4974a56db25ff2c to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment