Last active
November 3, 2015 20:18
-
-
Save dgrstl/9762f8980e9b4305c33d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'csv' | |
require 'puppet/face' | |
# Requires installation of https://forge.puppetlabs.com/dalen/puppetdbquery | |
# comma separated list of fact names you care about | |
whitelist = File.open('whitelist.csv').read.chomp | |
headers = whitelist.split(',') | |
# get yo data | |
Puppet.initialize_settings | |
nodes = Puppet::Face[:query, :current].facts('hostname~".*"', :facts => whitelist) | |
out_csv = CSV.open('inventory.csv', 'wb') do |csv| | |
#create header row | |
csv << ['node'] + headers | |
# get row data | |
nodes.each do |node_name, facts| | |
values = Array.new | |
values.push(node_name) | |
headers.each do |header| | |
if facts.has_key?(header) | |
values.push(facts.fetch(header)) | |
else | |
values.push('') | |
end | |
end | |
csv << values | |
end | |
end | |
puts "CSV output to inventory.csv" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment