Last active
August 29, 2015 13:57
-
-
Save zipkid/9366491 to your computer and use it in GitHub Desktop.
Store fact as JSON
This file contains hidden or 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
begin | |
require 'rubygems' | |
require 'json' | |
rescue LoadError => e | |
require File.expand_path(File.join(File.dirname(__FILE__), 'vendor')) | |
Vendor.load_vendored | |
end | |
users_hash = nil | |
if Facter.value(:kernel) == 'Linux' or Facter.value(:kernel) == 'SunOS' | |
users_hash = Hash.new | |
if File.exists?('/usr/bin/getent') | |
# We work-around an issue in Facter #10278 by forcing locale settings ... | |
ENV['LANG'] = 'POSIX' | |
ENV['LC_ALL'] = 'POSIX' | |
Facter::Util::Resolution.exec('/usr/bin/getent passwd').each_line do |line| | |
# Remove bloat ... | |
line.strip! | |
# Turn line into a set of tokens ... | |
user = line.split(':') | |
user_hash = Hash.new | |
user_hash['user'] = user[0] | |
user_hash['uid'] = user[2] | |
user_hash['gid'] = user[3] | |
user_hash['comment'] = user[4] | |
user_hash['home'] = user[5] | |
user_hash['shell'] = user[6] | |
users_hash[user[0]] = user_hash | |
end | |
else | |
Puppet::Type.type('user').instances.each do |user| | |
# Get details about the user from the corresponding instance ... | |
instance = user.retrieve | |
user_hash = Hash.new | |
user_hash['user'] = user.name | |
#user_hash['uid'] = user.uid | |
#user_hash['gid'] = user.gid | |
#user_hash['comment'] = user.comment | |
#user_hash['home'] = user.home | |
#user_hash['shell'] = user.shell | |
users_hash[user.name] = user_hash | |
end | |
end | |
elsif Facter.value(:kernel) == 'AIX' | |
users_hash = Hash.new | |
Facter::Util::Resolution.exec('cat /etc/passwd').each_line do |line| | |
# Remove bloat ... | |
line.strip! | |
# Turn line into a set of tokens ... | |
user = line.split(':') | |
user_hash = Hash.new | |
user_hash['user'] = user[0] | |
user_hash['uid'] = user[2] | |
user_hash['gid'] = user[3] | |
user_hash['comment'] = user[4] | |
user_hash['home'] = user[5] | |
user_hash['shell'] = user[6] | |
users_hash[user[0]] = user_hash | |
end | |
end | |
Facter.add('users') do | |
data = nil | |
begin | |
data = JSON.generate( users_hash ) | |
rescue => e | |
# JSON failed here | |
end | |
setcode do | |
data | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment