Skip to content

Instantly share code, notes, and snippets.

@dgrstl
Last active November 3, 2015 20:18
Show Gist options
  • Save dgrstl/9762f8980e9b4305c33d to your computer and use it in GitHub Desktop.
Save dgrstl/9762f8980e9b4305c33d to your computer and use it in GitHub Desktop.
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