diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/base_installation/lib/facter/ldapvar.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/modules/base_installation/lib/facter/ldapvar.rb b/modules/base_installation/lib/facter/ldapvar.rb new file mode 100644 index 0000000..ff8e898 --- /dev/null +++ b/modules/base_installation/lib/facter/ldapvar.rb | |||
@@ -0,0 +1,46 @@ | |||
1 | require 'ldap' | ||
2 | require 'puppet/util/ldap/connection' | ||
3 | |||
4 | Facter.add("ldapvar") do | ||
5 | setcode do | ||
6 | if Puppet[:node_terminus].to_sym != :ldap | ||
7 | data = [] | ||
8 | else | ||
9 | begin | ||
10 | conn = Puppet::Util::Ldap::Connection.instance | ||
11 | conn.start | ||
12 | connection = conn.connection | ||
13 | rescue ::LDAP::ResultError => e | ||
14 | raise Puppet::ParseError, ("ldapquery(): LDAP ResultError - #{e.message}") | ||
15 | end | ||
16 | |||
17 | host = Facter.value('ec2_metadata')["hostname"] | ||
18 | base = Puppet[:ldapbase] | ||
19 | scope = ::LDAP::LDAP_SCOPE_SUBTREE | ||
20 | filter = "(objectclass=*)" | ||
21 | |||
22 | data = { | ||
23 | :self => {}, | ||
24 | :other => [], | ||
25 | } | ||
26 | |||
27 | connection.search(base, scope, filter) do |entry| | ||
28 | data_ = entry.to_hash | ||
29 | data_['vars'] = (data_[Puppet[:ldapstackedattrs]] || []) | ||
30 | .map { |var| var.split("=", 2) } | ||
31 | .group_by { |(key, value)| key } | ||
32 | .map { |key, value| [key, value.map(&:last)] } | ||
33 | .to_h | ||
34 | |||
35 | data[:other] << data_ | ||
36 | |||
37 | if data_["cn"].any? { |cn| cn == host } | ||
38 | data[:self] = data_ | ||
39 | end | ||
40 | end | ||
41 | |||
42 | data | ||
43 | end | ||
44 | end | ||
45 | end | ||
46 | |||