Functions are plug-ins, written in Ruby, that you can call during catalog compilation. A call to any function is an expression that resolves to a value. Most functions accept one or more values as arguments, and return a resulting value.
Puppet supports functions.It supports two types of functions known with the name of statement and rvalue functions.
- Statements stand on their own and they do not have any return type. They are used for performing standalone tasks like importing other Puppet modules in the new manifest file.
- Rvalue returns values and can only be used when the statement requires a value, such as an assignment or a case statement.
You can write your own functions in the Puppet language to transform data and construct values. A function can optionally take one or more parameters as arguments. A function returns a calculated value from its final expression.
Function Syntax in Puppet:
function <MODULE NAME>::<NAME>(<PARAMETER LIST>) >> <RETURN TYPE> { ... body of function ... final expression, which will be the returned value of the function}
Function Example in Puppet:
function apache::bool2http(Variant[String, Boolean] $arg) >> String { case $arg { false, undef, /(?i:false)/ : { 'Off' } true, /(?i:true)/ : { 'On' } default : { "$arg" } }}