]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/commitdiff
Add ldap backend for hiera lookup
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 19 Mar 2018 15:02:30 +0000 (16:02 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 19 Mar 2018 15:10:18 +0000 (16:10 +0100)
environments/hiera.yaml
modules/base_installation/lib/puppet/functions/ldap_data.rb [new file with mode: 0644]

index 5a9a6d68958ed81a698224b79492e88b64fbdd0b..eda5eb3ac62d28bce7e2b4d451b370b0a430132a 100644 (file)
@@ -9,6 +9,9 @@ hierarchy:
   - name: "Initialization variables"
     path: "/root/puppet_variables.json"
 
+  - name: "Puppet ldap variables"
+    data_hash: ldap_data
+
   - name: "Per-role environment data"
     mapped_paths: [ldapvar.self.vars.roles, role, "roles/%{role}.yaml"]
 
diff --git a/modules/base_installation/lib/puppet/functions/ldap_data.rb b/modules/base_installation/lib/puppet/functions/ldap_data.rb
new file mode 100644 (file)
index 0000000..ff8d779
--- /dev/null
@@ -0,0 +1,46 @@
+require 'json'
+
+Puppet::Functions.create_function(:ldap_data) do
+  dispatch :ldap_data do
+    param 'Hash', :options
+    param 'Puppet::LookupContext', :context
+  end
+
+  def ldap_data(options, context)
+    begin
+      require 'ldap'
+      require 'puppet/util/ldap/connection'
+    rescue
+      context.not_found
+      return
+    end
+
+    if !context.cache_has_key("ldap_lookup")
+      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 = {}
+      connection.search(base, scope, filter) do |entry|
+        data_ = entry.to_hash
+        jsons = data_["immaePuppetJson"] || []
+        jsons.each do |json|
+          data.merge!(JSON.parse(json))
+        end
+      end
+
+      context.cache("ldap_lookup", data)
+    end
+
+    context.cached_value("ldap_lookup")
+  end
+end