All the custom functions are implemented as separate .rb files and are distributed among modules.
One needs to put custom functions in lib/puppet/parser/function.
Creating a New Function :
New functions are created or defined using the newfunction method inside the puppet::parser::Functions module.
One needs to pass the function name as a symbol to newfunction method and the code to run as a block.
The following example is a function, which is used to write a string to the file inside the /user directory.
module Puppet::Parser::Functions
newfunction(:write_line_to_file) do |args|
filename = args[0]
str = args[1]
File.open(filename, 'a') {|fd| fd.puts str }
end
end
Once the user has the function declared, it can be used in the manifest file as shown below.
write_line_to_file('/user/Sam.txt, "Hello Sam!")