From 945ae444952f3322c4687059f0ba6b7308750feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 10 Sep 2017 11:26:18 +0200 Subject: ldapvar facter --- modules/base_installation/lib/facter/ldapvar.rb | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 modules/base_installation/lib/facter/ldapvar.rb (limited to 'modules') 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 @@ +require 'ldap' +require 'puppet/util/ldap/connection' + +Facter.add("ldapvar") do + setcode do + if Puppet[:node_terminus].to_sym != :ldap + data = [] + else + begin + conn = Puppet::Util::Ldap::Connection.instance + conn.start + connection = conn.connection + rescue ::LDAP::ResultError => e + raise Puppet::ParseError, ("ldapquery(): LDAP ResultError - #{e.message}") + end + + host = Facter.value('ec2_metadata')["hostname"] + base = Puppet[:ldapbase] + scope = ::LDAP::LDAP_SCOPE_SUBTREE + filter = "(objectclass=*)" + + data = { + :self => {}, + :other => [], + } + + connection.search(base, scope, filter) do |entry| + data_ = entry.to_hash + data_['vars'] = (data_[Puppet[:ldapstackedattrs]] || []) + .map { |var| var.split("=", 2) } + .group_by { |(key, value)| key } + .map { |key, value| [key, value.map(&:last)] } + .to_h + + data[:other] << data_ + + if data_["cn"].any? { |cn| cn == host } + data[:self] = data_ + end + end + + data + end + end +end + -- cgit v1.2.3