Puppet Function: simplib::lookup

Defined in:
lib/puppet/functions/simplib/lookup.rb
Function type:
Ruby 4.x API

Overview

simplib::lookup(String $param, Optional[Any] $options)Any

A function for falling back to global scope variable lookups when the Puppet 4 “lookup()“ function cannot find a value.

While “lookup()“ will stop at the back-end data sources, “simplib::lookup()“ will check the global scope first to see if the variable has been defined.

This means that you can pre-declare a class and/or use an ENC and look up the variable whether it is declared this way or via Hiera or some other back-end.

Examples:

No defaults

simplib::lookup('foo::bar::baz')

With a default

simplib::lookup('foo::bar::baz', { 'default_value' => 'Banana' })

With a typed default

simplib::lookup('foo::bar::baz', { 'default_value' => 'Banana', 'value_type' => String })

Parameters:

  • param (String)

    The parameter that you wish to look up

  • options (Optional[Any])

    Hash of options for regular “lookup()“

    • This must follow the syntax rules for the

    Puppet “lookup( [<NAME>], <OPTIONS HASH> )“ version of “lookup()“

    • No other formats are supported!

Returns:

  • (Any)

    The value that is found in the system for the passed parameter.

See Also:

Author:

  • Trevor Vaughan <tvaughan@onyxpoint.com>



12
13
14
15
16
17
18
19
20
21
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
89
90
91
# File 'lib/puppet/functions/simplib/lookup.rb', line 12

Puppet::Functions.create_function(:'simplib::lookup') do
  # @param param The parameter that you wish to look up
  #
  # @param options Hash of options for regular ``lookup()``
  #
  #   * This **must** follow the syntax rules for the
  #   Puppet ``lookup( [<NAME>], <OPTIONS HASH> )`` version of ``lookup()``
  #   * No other formats are supported!
  #
  # @see https://docs.puppet.com/puppet/latest/function.html#lookup - Lookup Function
  #
  # @return [Any] The value that is found in the system for the passed
  #   parameter.
  #
  # @example No defaults
  #   simplib::lookup('foo::bar::baz')
  #
  # @example With a default
  #   simplib::lookup('foo::bar::baz', { 'default_value' => 'Banana' })
  #
  # @example With a typed default
  #   simplib::lookup('foo::bar::baz', { 'default_value' => 'Banana', 'value_type' => String })
  #
  dispatch :lookup do
    param          'String', :param
    optional_param 'Any',    :options
  end

  def lookup(param, options = nil)
    class_name = param.split('::')

    if class_name.size < 2
      # This is a global variable
      param_name = class_name
      class_name = nil
    else
      param_name = class_name.pop
      class_name = class_name.join('::')
    end

    if param_name
      if class_name
        global_scope = closure_scope.find_global_scope
        catalog = global_scope.catalog

        active_resource = catalog.resource("Class[#{class_name}]")
        if active_resource
          active_resource_param = active_resource.parameters[param_name.to_sym]
          if active_resource_param
            global_param = active_resource_param.value
          end
        end
      else
        # Save the state of the strict vars setting. Ignore it here since we do
        # legitimate global scope lookups but make sure to restore it.
        strict_vars = nil
        begin
          if Puppet[:strict]
            strict_vars = Puppet[:strict]
            Puppet[:strict] = :off
          end

          global_param = closure_scope.find_global_scope.lookupvar(param)
        ensure
          if strict_vars
            Puppet[:strict] = strict_vars
          end
        end
      end
    end

    return global_param if global_param

    if options
      return call_function('lookup', param, options )
    else
      return call_function('lookup', param )
    end
  end
end