Puppet Function: mapval

Defined in:
lib/puppet/parser/functions/mapval.rb
Function type:
Ruby 3.x API

Overview

mapval(String $regex, Stdlib::Absolutepath $filename)Stri

This function pulls a mapped value from a text file with the format:

<key> | <value>

Only the last value matched will be returned

Parameters:

  • regex (String)

    Ruby regular expression that will be mapped. Do not add starting ^ or ending $

  • filename (Stdlib::Absolutepath)

    The filename from which to pull the value

Returns:

  • (Stri)

    Stri



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/parser/functions/mapval.rb', line 2

newfunction(:mapval, :type => :rvalue, :doc => <<-EOM) do |args|

  regex = args[0]
  This function pulls a mapped value from a text file with the format:

  `<key> | <value>`

  Only the **last** value matched will be returned

  @param regex [String]
    Ruby regular expression that will be mapped.
    Do not add starting `^` or ending `$`

  @param filename [Stdlib::Absolutepath]
    The filename from which to pull the value

  @return [String]
  EOM
  filename = args[1]
  retval = ''
  File.open(filename, 'r') do |file|
    while line = file.gets
      line.chomp
      line = line.split(" | ")
      if ( line[0] =~ /^#{regex}$/ )
        line.shift
        retval = line.join.to_s.chomp
      end
    end
  end

  retval
end