0
votes

I have to use a tool at work that makes use of Puppet 3.6 (I'm not interested in answers that suggest other versions, I'm stuck with what I'm given!). I am constrained by this tool to putting all my functionality into a single manifest. I can't find the syntax for Puppet 3 as to how to create custom functions, as the docs for Puppet 3 Functions just has a generic link that redirects to the v5.6 docs.

I have to parse some inputs at check for the presence of illegal chars.

define my_module::my_manifest($param1, $param2) {
    # $param1 & $param2 are passed to this manifest by the tool I'm working with

    function check_for_illegal_chars(String $check_string) {
         $illegal_chars = "[&|;]"
         if $check_string =~ $illegal_chars {
             fail("Illegal char(s) detected in '${check_string}', cannot contain any of '${illegal_chars}'")
         }
    }

    # sample usage
    check_for_illegal_chars($param1)

    # rest of my_manifest...
}

When I do this, however, I get an error:

Error: Could not parse for environment production: Syntax error at 'check_string'; expected ')' at /path/to/my_manifest.pp: 4

What is the correct syntax please?

1

1 Answers

1
votes

I did this by adding a ruby file to the following location within the module: lib/puppet/parser/functions/keys.rb

In my case I needed a function to get keys because I was using an old version (the same version as you I believe). The keys.rb file looked like below:

#
# keys.rb
#

module Puppet::Parser::Functions
  newfunction(:keys, :type => :rvalue, :doc => <<-EOS
Returns the keys of a hash as an array.
    EOS
  ) do |arguments|

    raise(Puppet::ParseError, "keys(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1

    hash = arguments[0]

    unless hash.is_a?(Hash)
      raise(Puppet::ParseError, 'keys(): Requires hash to work with')
    end

    result = hash.keys

    return result
  end
end

# vim: set ts=2 sw=2 et :

And in puppet manifest simply call the function with keys($hash)

Hope this helps as an example.