]>
Commit | Line | Data |
---|---|---|
248bd83e IB |
1 | begin |
2 | require 'ldap' | |
3 | require 'puppet/util/ldap/connection' | |
4 | ||
5 | Facter.add("ldapvar") do | |
6 | setcode do | |
7 | if Puppet[:node_terminus].to_sym != :ldap | |
8 | data = [] | |
9 | else | |
10 | begin | |
11 | conn = Puppet::Util::Ldap::Connection.instance | |
12 | conn.start | |
13 | connection = conn.connection | |
14 | rescue ::LDAP::ResultError => e | |
15 | raise Puppet::ParseError, ("ldapquery(): LDAP ResultError - #{e.message}") | |
16 | end | |
945ae444 | 17 | |
248bd83e IB |
18 | host = Facter.value('ec2_metadata')["hostname"] |
19 | base = Puppet[:ldapbase] | |
20 | scope = ::LDAP::LDAP_SCOPE_SUBTREE | |
21 | filter = "(objectclass=*)" | |
22 | ||
23 | data = { | |
24 | :self => {}, | |
25 | :other => [], | |
26 | } | |
27 | ||
28 | connection.search(base, scope, filter) do |entry| | |
29 | data_ = entry.to_hash | |
30 | data_['vars'] = (data_[Puppet[:ldapstackedattrs]] || []) | |
31 | .map { |var| var.split("=", 2) } | |
32 | .group_by { |(key, value)| key } | |
33 | .map { |key, value| [key, value.map(&:last)] } | |
34 | .to_h | |
35 | ||
36 | data[:other] << data_ | |
37 | ||
38 | if data_["cn"].any? { |cn| cn == host } | |
39 | data[:self] = data_ | |
40 | end | |
945ae444 | 41 | end |
945ae444 | 42 | |
248bd83e IB |
43 | data |
44 | end | |
945ae444 IB |
45 | end |
46 | end | |
248bd83e IB |
47 | rescue LoadError |
48 | # No facts | |
945ae444 | 49 | end |