Puppet Function: params_lookup
- Defined in:
- lib/puppet/parser/functions/params_lookup.rb
- Function type:
- Ruby 3.x API
Overview
This fuction looks for the given variable name in a set of different sources:
-
Hiera, if available (‘modulename_varname’)
-
Hiera, if available (if second argument is ‘global’)
-
::modulename_varname
-
::varname (if second argument is ‘global’)
-
::modulename::params::varname
If no value is found in the defined sources, it returns an empty string (”)
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/puppet/parser/functions/params_lookup.rb', line 22 newfunction(:params_lookup, :type => :rvalue, :doc => <<-EOS This fuction looks for the given variable name in a set of different sources: - Hiera, if available ('modulename_varname') - Hiera, if available (if second argument is 'global') - ::modulename_varname - ::varname (if second argument is 'global') - ::modulename::params::varname If no value is found in the defined sources, it returns an empty string ('') EOS ) do |arguments| raise(Puppet::ParseError, "params_lookup(): Define at least the variable name " + "given (#{arguments.size} for 1)") if arguments.size < 1 value = '' var_name = arguments[0] module_name = parent_module_name # Hiera Lookup if Puppet::Parser::Functions.function('hiera') value = function_hiera(["#{module_name}_#{var_name}", '']) return value if (not value.nil?) && (value != :undefined) && (value != '') value = function_hiera(["#{var_name}", '']) if arguments[1] == 'global' return value if (not value.nil?) && (value != :undefined) && (value != '') end # Top Scope Variable Lookup (::modulename_varname) catch (:undefined_variable) do begin value = lookupvar("::#{module_name}_#{var_name}") rescue Puppet::ParseError => e raise unless e.to_s =~ /^Undefined variable / end end return value if (not value.nil?) && (value != :undefined) && (value != '') # Look up ::varname (only if second argument is 'global') if arguments[1] == 'global' catch (:undefined_variable) do begin value = lookupvar("::#{var_name}") rescue Puppet::ParseError => e raise unless e.to_s =~ /^Undefined variable / end end return value if (not value.nil?) && (value != :undefined) && (value != '') end # needed for the next two lookups classname = self.resource.name.downcase loaded_classes = catalog.classes # self::params class lookup for default value if loaded_classes.include?("#{classname}::params") value = lookupvar("::#{classname}::params::#{var_name}") return value if (not value.nil?) && (value != :undefined) && (value != '') end # Params class lookup for default value if loaded_classes.include?("#{module_name}::params") value = lookupvar("::#{module_name}::params::#{var_name}") return value if (not value.nil?) && (value != :undefined) && (value != '') end return '' end |