1 # Digs into the facts hash using dot-notation
6 # fact('os.architecture')
10 # fact('mountpoints."/dev".options.1')
12 # Fact containing a "." in the name:
14 # fact('vmware."VRA.version"')
16 Puppet::Functions.create_function(:fact) do
18 param 'String', :fact_name
21 def to_dot_syntax(array_path)
22 array_path.map { |string|
23 string.include?('.') ? %("#{string}") : string
28 facts = closure_scope['facts']
30 # Transform the dot-notation string into an array of paths to walk. Make
31 # sure to correctly extract double-quoted values containing dots as single
32 # elements in the path.
33 path = fact_name.scan(%r{([^."]+)|(?:")([^"]+)(?:")}).map { |x| x.compact.first }
36 path.reduce(facts) do |d, k|
37 return nil if d.nil? || k.nil?
41 result = d[Integer(k)]
42 rescue ArgumentError => e # rubocop:disable Lint/UselessAssignment : Causes errors if assigment is removed.
43 Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is an array; cannot index to '#{k}'")
49 Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is not a collection; cannot walk to '#{k}'")