Puppet Custom Function
Puppet allows us to create custom functions. Custom functions help to boost and extend the capacity of Puppet modules and manifest files.
As we know, Puppet provides a list of built-in functions, but if you want to do some modification on the data of the built-in functions or interact with third-party services when you are compiling the catalog and if those built-in functions are not sufficient for your requirement, then you can create custom functions for Puppet.
Once you create the custom function, it will work like a built-in function, you can call them, and you can use it to return some value. Even Puppet offers to share these functions with other users.
Puppet allows two interfaces to write the custom functions:
Path to Put Custom Function
The custom function contains .rb files, and these files are distributed among modules. Custom functions are located in lib/puppet/parser/function. To load the function from .rb files, use the following path:
Creating Custom Function
Puppet provides newfunction method inside the puppet::parser::Functions module to define the new function. Use any function name with the newfunction method.
Let's build a function for writing a string to a file in the directory /tmp:
If the function has been defined, it can be used in the manifest file, as shown below:
Here, write_line_to_file function is an example of a statement function. This function does operation and does not return any value.