Puppet Function
As we know, Puppet uses Ruby programming language, and like other programming languages, Ruby also supports the function. We can write the functions in Ruby language and can be distributed in puppet modules. Puppet provides two different types of functions:
statement: This type of function did not return any value and used to perform standalone tasks. It can be used to import Puppet modules in the new manifest file.
rvalue: In Puppet, if you want to define a function with their return type, then you can use rvalue functions. rvalue can only be used when the statement needs a value, like a case statement or assignment. A function can only take two parameters as an argument.
In Puppet, we can create our functions according to our requirements. Even Puppet also provides many built-in functions. Let’s see the list of some built-in functions:
Function | Description | Syntax | Type |
---|---|---|---|
abs | It will return the absolute value of a Numeric value. | abs(Numeric($str_val)) | rvalue |
alert | This function is used for logging a message on the server at level alert. | alert(Any *$values) | statement |
break | Breaks an innermost iteration as if an end of input was found. This function doesn’t accept any argument. | break() | statement |
binary_file | This function loads a binary file from a module or file system and returns binary content. | binary_file(String $path) | rvalue |
call | Used to call an arbitrary function by name. | call(String $function_name, Any *$arguments, Optional[Callable] &$block) | statement |
create_resources | This function is used to change a hash into a collection of resources and inserts them into the catalog. | create_resources($type, $resources) | statement |
debug | Logs a message on the server at level debug. | debug(Any *$values) | statement |
crit | Logs a message on the server at the level crit. | crit(Any *$values) | statement |
defined | Verify whether a given class or resource type is defined. | defined(Variant[String, Type[CatalogEntry], Type[Type[CatalogEntry]]] *$vals) | rvalue |
emerg | This function is used for logging a message on the server at level emerg. | emerg(Any *$values) | statement |
fail | Fail with a parser error. | fail() | statement |
err | This function is used for logging a message on the server at level err. | err(Any *$values) | statement |
file | It takes a file from a module and returns its contents as a string. | file() | rvalue |
fqdn_rand | Used to generate random numbers based on the fqdn of the node. | fqdn_rand(MAX, [SEED]) | rvalue |
include | This is used to evaluate one or more classes. | include(Any *$names) | statement |
info | This function is used for logging a message on the server at level info. | info(Any *$values) | statement |
inline_template | This is used to evaluate a template string and return its value. | inline_template() | rvalue |
join | This function is used to join an array of values into a string with elements separated by a delimiter. | join(Array $arg, Optional[String] $delimiter) | rvalue |
keys | This function is used to return the keys of a hash as an Array. | keys(Hash $hsh) | rvalue |
md5 | This function is used to return an MD5 hash value from a given string. | md5() | rvalue |
new | This function creates a new instance of an object of a specified data type. | new(Type $type, Any *$args, Optional[Callable] &$block) | statement |
notice | This function is used for logging a message on the server at level notice. | notice(Any *$values) | statement |
realize | This is used to make a virtual object real. | realize() | statement |
require | This is used to evaluate one or more classes and adding the required class as a dependency. | require(Any *$names) | statement |
round | This is used to return an Integer value rounded to the nearest value. | round(Numeric $val) | rvalue |
rstrip | Strips trailing spaces from a String | rstrip(String $arg) | rvalue |
sha1 | This is used to return a SHA1 hash value from a given string. | sha1() | rvalue |
sha256 | This function is used to Return a SHA256 hash value from a given string. | sha256() | rvalue |
shellquote | This function is used to quote and concatenate arguments for use in Bourne shell. | shellquote() | rvalue |
size | Used to return the size of String, Hash, Array, or Binary value. | size(Variant[Collection, String, Binary] $arg) | rvalue |
length | Used to return the length of String, Hash, Array, or Binary value. | length(Variant[Collection, String, Binary] $arg) | rvalue |
split | Splits a string into an array using a given pattern. | split(String $str, String $pattern) | rvalue |
sprintf | This function does printf-style formatting of text. | sprintf() | rvalue |
strip | Strips leading and trailing spaces from a String | strip(Iterable[Variant[String, Numeric]] $arg) | rvalue |
tag | This function is used to add the given tags to the containing class or to the definition. | tag() | statement |
tagged | This function is used to test whether the latest container is tagged with the given tags or not. It will return the Boolean value. | tagged() | rvalue |
template | This function first loads an ERB template from the module and then evaluates the output value as a string. | template() | rvalue |
upcase | This function is used to convert a String, Array, or Hash (recursively) into the upper case. | upcase(String $arg) | rvalue |
values | This function is used to return the values of a hash as an Array. | values(Hash $hsh) | rvalue |
versioncmp | This function is used to compare two version numbers. | versioncmp(String $a, String $b) | rvalue |
warning | This function is used for logging a message on the server at the warning level. | warning(Any *$values) | statement |