Skip to content

Instantly share code, notes, and snippets.

@adrienthebo
Created June 14, 2016 20:11
Show Gist options
  • Save adrienthebo/0889b4db24ee95e8c77f46bb1a8ae845 to your computer and use it in GitHub Desktop.
Save adrienthebo/0889b4db24ee95e8c77f46bb1a8ae845 to your computer and use it in GitHub Desktop.
def flatten_structure(path, structure)
results = {}
if structure.is_a? Hash
structure.each_pair do |name, value|
new_path = "#{path}_#{name}".gsub(/\-|\//, '_')
results.merge! flatten_structure(new_path, value)
end
elsif structure.is_a? Array
structure.each_with_index do |value, index|
new_path = "#{path}_#{index}"
results.merge! flatten_structure(new_path, value)
end
else
results[path] = structure
end
results
end
# If dmidecode isn't present, all sorts of fucky things will happen
output=%x{/usr/sbin/dmidecode --type 11 2>/dev/null}
output_hash={}
output.each_line do |line|
if(line[/tring /])
key=line.split(':')[0].strip.downcase.tr(" ", "_")
value=line.match /(?<=:).*/
output_hash[key] = value.to_s.strip
end
end
flattened=flatten_structure('dmi_oem',output_hash)
flattened.each_pair do |factname, factvalue|
Facter.add(factname) do
confine :osfamily => 'RedHat'
setcode { factvalue }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment