diff options
-rw-r--r-- | environments/production/hiera.yaml | 2 | ||||
-rw-r--r-- | modules/base_installation/lib/facter/ldapvar.rb | 46 |
2 files changed, 47 insertions, 1 deletions
diff --git a/environments/production/hiera.yaml b/environments/production/hiera.yaml index 095a110..f5e5bc2 100644 --- a/environments/production/hiera.yaml +++ b/environments/production/hiera.yaml | |||
@@ -10,7 +10,7 @@ hierarchy: | |||
10 | path: "nodes/%{facts.ec2_metadata.hostname}.yaml" | 10 | path: "nodes/%{facts.ec2_metadata.hostname}.yaml" |
11 | 11 | ||
12 | - name: "Per-role data" | 12 | - name: "Per-role data" |
13 | mapped_paths: [roles, role, "roles/%{role}.yaml"] | 13 | mapped_paths: [ldapvar.self.vars.roles, role, "roles/%{role}.yaml"] |
14 | 14 | ||
15 | - name: "Per-type data" | 15 | - name: "Per-type data" |
16 | path: "types/%{facts.ec2_metadata.instance-type}.yaml" | 16 | path: "types/%{facts.ec2_metadata.instance-type}.yaml" |
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 | |||