Refining the interface of your module through custom functions
Functions can be of great help in keeping your manifest clean and maintainable, and some tasks cannot even be implemented without resorting to a Ruby function.
A frequent use of the custom functions (especially in Puppet 3) is input validation. You can do this in the manifest itself, but it can be a frustrating exercise because of the limitations of the language. The resulting Puppet DSL code can be hard to read and maintain. The stdlib
module comes with the validate_X
functions for many basic data types, such as validate_bool
. Typed parameters in Puppet 4 and later versions make this more convenient and natural, because for the supported variable types, no validation function is needed anymore.
As with all the plugins, the functions need not be specific to the module's domain, and they instantly become available for all the manifests. A case in point is the packt_cacti
module that can use the validation functions for the packt_cacti...