Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Puppet Enterprise Node Group (Classification) data export
#!/usr/bin/env ruby
# Export node group data from Puppet Enterprise
# Setup:
# export PATH=$PATH:/opt/puppetlabs/puppet/bin
# gem install --user-install puppetclassify
# puppet-access login --lifetime 8h
require 'csv'
require 'json'
require 'puppetclassify'
HOSTNAME = `hostname -f`.strip()
TOKEN_PATH = "#{ENV['HOME']}/.puppetlabs/token"
auth_info = {
"ca_certificate_path" => "/etc/puppetlabs/puppet/ssl/certs/ca.pem",
"certificate_path" => "/etc/puppetlabs/puppet/ssl/certs/#{HOSTNAME}.pem",
"token_path" => TOKEN_PATH
}
classifier_url = "https://#{HOSTNAME}:4433/classifier-api"
puppetclassify = PuppetClassify.new(classifier_url, auth_info)
groups = puppetclassify.groups.get_groups
groups.sort_by! { |g| g['name'] }
# Export raw data
File.open('groups.json', 'w') do |f|
f.write(JSON.pretty_generate(groups))
end
# Export CSVs
csv_default_options = {
write_headers: true,
force_quotes: true
}
groups_csv = CSV.open(
'groups.csv',
'wb',
headers: [
'name',
'environment',
'classes',
'variables',
],
**csv_default_options
)
groups_rules_csv = CSV.open(
'groups_rules.csv',
'wb',
headers: [
'name',
'environment',
'rules'
],
**csv_default_options
)
groups.each do |grp|
groups_csv << [
grp['name'],
grp['environment'],
grp['classes'].keys.join(','),
grp['variables'].collect { |k,v| "#{k}=#{v}" }.join(',')
]
groups_rules_csv << [
grp['name'],
grp['environment'],
grp['rule'].to_json
]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment