module HieraPuppet

Public Instance Methods

hiera() click to toggle source
   # File lib/hiera_puppet.rb
54 def hiera
55   @hiera ||= Hiera.new(:config => hiera_config)
56 end
hiera_config() click to toggle source
   # File lib/hiera_puppet.rb
58 def hiera_config
59   config = {}
60 
61   config_file = hiera_config_file
62   if config_file
63     config = Hiera::Config.load(config_file)
64   end
65 
66   config[:logger] = 'puppet'
67   config
68 end
hiera_config_file() click to toggle source
   # File lib/hiera_puppet.rb
70 def hiera_config_file
71   hiera_config = Puppet.settings[:hiera_config]
72   if Puppet::FileSystem.exist?(hiera_config)
73     hiera_config
74   else
75     Puppet.warning _("Config file %{hiera_config} not found, using Hiera defaults") % { hiera_config: hiera_config }
76     nil
77   end
78 end
lookup(key, default, scope, override, resolution_type) click to toggle source
   # File lib/hiera_puppet.rb
 8 def lookup(key, default, scope, override, resolution_type)
 9   scope = Hiera::Scope.new(scope)
10 
11   answer = hiera.lookup(key, default, scope, override, resolution_type)
12 
13   if answer.nil?
14     raise Puppet::ParseError, _("Could not find data item %{key} in any Hiera data file and no default supplied") % { key: key }
15   end
16 
17   answer
18 end
parse_args(args) click to toggle source
   # File lib/hiera_puppet.rb
20 def parse_args(args)
21   # Functions called from Puppet manifests like this:
22   #
23   #   hiera("foo", "bar")
24   #
25   # Are invoked internally after combining the positional arguments into a
26   # single array:
27   #
28   #   func = function_hiera
29   #   func(["foo", "bar"])
30   #
31   # Functions called from templates preserve the positional arguments:
32   #
33   #   scope.function_hiera("foo", "bar")
34   #
35   # Deal with Puppet's special calling mechanism here.
36   if args[0].is_a?(Array)
37     args = args[0]
38   end
39 
40   if args.empty?
41     raise Puppet::ParseError, _("Please supply a parameter to perform a Hiera lookup")
42   end
43 
44   key      = args[0]
45   default  = args[1]
46   override = args[2]
47 
48   return [key, default, override]
49 end