-
Notifications
You must be signed in to change notification settings - Fork 17
/
uids.rb
58 lines (50 loc) · 1.57 KB
/
uids.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'puppet'
#
# uids.rb
#
# This fact provides mapping between user logins and their UIDs as a Hash
#
# Usage: $::uids['root'] # => 0
#
Facter.add(:uids) do
confine :kernel => :linux
setcode do
uids = {}
#
# Modern Linux distributions provide "/etc/passwd" in the following format:
#
# root:x:0:0:root:/root:/bin/bash
# (...)
#
# Above line has the follwing fields separated by the ":" (colon):
#
# <user name>:<password>:<user ID>:<group ID>:<comment>:<home directory>:<command shell>
#
# We only really care about "user name" and "user ID" fields.
#
#
# We use "getent" binary first if possible to look-up what users are currently
# available on system. This is possibly due to an issue in Puppet "user" type
# which causes Facter to delay every Puppet run substantially especially when
# LDAP is in place to provide truth source about users etc ...
#
# In the unlikely event in which the "getent" binary is not available we simply
# fall-back to using Puppet "user" type ...
#
if File.exists?('/usr/bin/getent')
# We work-around an issue in Facter #10278 by forcing locale settings ...
ENV['LC_ALL'] = 'C'
Facter::Util::Resolution.exec('/usr/bin/getent passwd').each_line do |line|
line.strip!
user = line.split(':')
uids[user[0]] = user[2].to_i
end
else
Puppet::Type.type('user').instances.each do |user|
instance = user.retrieve
uids[user.name] = instance[user.property(:uid)].to_i
end
end
uids
end
end