Skip to content

Instantly share code, notes, and snippets.

@jtopjian
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save jtopjian/fb03d5ef74932010c7c0 to your computer and use it in GitHub Desktop.

Select an option

Save jtopjian/fb03d5ef74932010c7c0 to your computer and use it in GitHub Desktop.
require 'etc'
# First get a list of users via the homedir_users fact
#
# An example of this file looks like:
# $ cat /etc/facter/facts.d/homedir_users.yaml
# ---
# homedir_users:
# - root
# - jdoe
#
# Missing users will be skipped
#
# If this fact does not exist, all users in /etc/passwd
# will be obtained.
#
users = {}
homedir_users = Facter.value(:homedir_users)
if homedir_users != nil and homedir_users.size > 0
homedir_users.each do |u|
begin
user = Etc.getpwnam(u)
users[user.name] = user.dir
rescue
next
end
end
else
Etc.passwd do |user|
users[user.name] = user.dir
end
end
users.each do |user_name, user_dir|
Facter.add("home_#{user_name}") do
setcode do
user_dir
end
end
end
require 'etc'
# First get a list of users via the homedir_users fact
#
# An example of this file looks like:
# $ cat /etc/facter/facts.d/homedir_users.yaml
# ---
# homedir_users:
# - root
# - jdoe
#
# Missing users will be skipped
#
# If this fact does not exist, all users in /etc/passwd
# will have their keys discovered.
#
users = {}
homedir_users = Facter.value(:homedir_users)
if homedir_users != nil and homedir_users.size > 0
homedir_users.each do |u|
begin
pw = Etc.getpwnam(u)
user = pw.name.gsub(/[^a-zA-Z0-9_]/, '')
homedir = pw.dir
key = false
if File.exists?("#{homedir}/.ssh/id_rsa.pub")
key = IO.read("#{homedir}/.ssh/id_rsa.pub")
elsif File.exists?("#{homedir}/.ssh/id_dsa.pub")
key = IO.read("#{homedir}/.ssh/id_dsa.pub")
end
if key
users[user] = key
end
rescue
next
end
end
else
Etc.passwd do |pw|
user = pw.name.gsub(/[^a-zA-Z0-9_]/, '')
homedir = pw.dir
key = false
if File.exists?("#{homedir}/.ssh/id_rsa.pub")
key = IO.read("#{homedir}/.ssh/id_rsa.pub")
elsif File.exists?("#{homedir}/.ssh/id_dsa.pub")
key = IO.read("#{homedir}/.ssh/id_dsa.pub")
end
if key
users[user] = key
end
end
end
users.each do |user_name, user_key|
Facter.add("sshpubkey_#{user_name}") do
setcode do
user_key
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment