Functions

Table of Contents

Functions take zero or more values as arguments and return a value. Argument values need to be of the type and range as documented for each function. Some functions are documented with a ..., in which case they take an arbitrary amount of arguments.

They can return scalar and list values:

    vars:
      "random" int => randomint("0", "100");
      "list" slist => readstringlist("/tmp/listofstring", "#.*", "\s", 10, 400);

In addition, functions with return type boolean evaluate to true or false. The class on the left-hand side is set if the function evaluates to true. If the function evaluates to false, then the class remains unchanged.

    bundle agent test
    {
    vars:
      "five" int => "5";
      "seven" " int => "7";
    classes:
      "ok" expression => islessthan("$(five)","$(seven)");

    reports:

      ok::
        "$(five) is smaller than $(seven)";

     !ok::
        "$(seven) is smaller than $(five)";

    }

Underneath, CFEngine functions that return boolean will actually return a context expression like any or !any which will then be deemed true or false by the CFEngine evaluator. Note the truth of a context expression or the result of a function call may change during evaluation, but a class, once defined, will stay defined.

Functions that return a boolean can thus sometimes be used in places where a string is accepted as well, but this behavior is not clearly defined or supported. Use at your own discretion.

Function caching

During convergence, CFEngine's evaluation model will evaluate functions multiple times, which can be a performance concern.

Some system functions are particularly expensive:

When enabled cached functions are not executed on every pass of convergence. Instead, the function will only be executed once during the agent evaluation step and its result will be cached until the end of that agent execution.

Note: Function caching is per-process, so results will not be cached between separate components e.g. cf-agent, cf-serverd and cf-promises. Additionally functions are cached by hashing the function arguments. If you have the exact same function call in two different promises (it does not matter if they are in the same bundle or not) only the first executed function will be cached. That cached result will be re-used for other identical function occurrences.

Function caching can be disabled by setting cache_system_functions in body common control to false.

Function Skipping

If a variable passed to a function is unable to be resolved the function will be skipped. The function will be evaluated during a later pass when all variables passed as arguments are able to be resolved. The function will never be evaluated if any argument contains a variable that never resolves.

Collecting Functions

Some function arguments are marked as collecting which means they can "collect" an argument from various sources. The data is normalized into the JSON format internally, so all of the following data types have consistent behavior.

  • If a key inside a data container is specified (mycontainer[key]), the value under that key is collected. The key can be a string for JSON objects or a number for JSON arrays.

  • If a single data container, CFEngine array, or slist is specified (mycontainer or myarray or myslist), the contents of it are collected.

  • If a single data container, CFEngine array, or slist is specified with @() around it (@(mycontainer) or @(myarray) or @(myslist)), the contents of it are collected.

  • If a function call that returns a data container or slist is specified, that function call is evaluated and the results are inserted, so you can say for instance sort(data_expand(...), "lex") to expand a data container then sort it.

  • If a list (slist, ilist, or rlist) is named, its entries are collected.

  • If any CFEngine "classic" array (array[key]) is named, it's first converted to a JSON key-value map, then collected.

  • If a literal JSON string like [ 1,2,3 ] or { "x": 500 } is provided, it will be parsed and used.

  • If any of the above-mentioned ways to reference variables are used inside a literal JSON string they will be expanded (or the function call will fail). This is similar to the behavior of Javascript, for instance. For example, mergedata('[ thing, { "mykey": otherthing[123] } ]') will wrap the thing in a JSON array; then the contents of otherthing[123] will be wrapped in a JSON map which will also go in the array.

Delayed Evaluation Functions

Since CFEngine 3.10, some functions are marked as delayed evaluation which means they can evaluate a function call across every element of a collection. This makes intuitive sense for the collection traversing functions maparray(), maplist(), and mapdata().

The practical use is for instance maplist(format("%03d", $(this)), mylist) which will evaluate that format() call once for every element of mylist.

Before 3.10, the same call would have resulted in running the format() function before the list is traversed, which is almost never what the user wants.

List of all functions

There are a large number of functions built into CFEngine. The following tables might make it easier for you to find the function you need.

Functions by Category

files communication utils system internal io data
accessedbefore() host2ip() bundlestate() data_sysctlvalues() callstack_callers() countlinesmatching() accumulated()
changedbefore() hostrange() classesmatching() findprocesses() callstack_promisers() data_readstringarray() ago()
dirname() hostsseen() classmatch() getenv() data_readstringarrayidx() and()
diskfree() hostswithclass() countclassesmatching() getuid() parseintarray() bundlesmatching()
file_hash() hubknowledge() datastate() getuserinfo() parsejson() canonify()
fileexists() ip2host() execresult() getusers() parserealarray() canonifyuniquely()
filesexist() iprange() getclassmetatags() groupexists() parsestringarray() classify()
filesize() isipinsubnet() getvariablemetatags() hostinnetgroup() parsestringarrayidx() concat()
filestat() ldaparray() isvariable() now() parseyaml() data_expand()
findfiles() ldaplist() returnszero() packagesmatching() readcsv() data_regextract()
isdir() ldapvalue() splayclass() packageupdatesmatching() readdata() difference()
isexecutable() network_connections() usemodule() processexists() readenvfile() escape()
islink() peerleader() registryvalue() readfile() eval()
isnewerthan() peerleaders() sysctlvalue() readintarray() every()
isplain() peers() userexists() readintlist() expandrange()
laterthan() readtcp() readjson() filter()
lsdir() regldap() readrealarray() format()
translatepath() remoteclassesmatching() readreallist() getfields()
remotescalar() readstringarray() getgid()
selectservers() readstringarrayidx() getindices()
url_get() readstringlist() getvalues()
readyaml() grep()
regline() hash()
hashmatch()
ifelse()
intersection()
irange()
isgreaterthan()
islessthan()
join()
lastnode()
length()
makerule()
maparray()
mapdata()
maplist()
max()
mean()
mergedata()
min()
none()
not()
nth()
on()
or()
product()
randomint()
regarray()
regcmp()
regex_replace()
regextract()
reglist()
reverse()
rrange()
shuffle()
some()
sort()
splitstring()
storejson()
strcmp()
strftime()
string_downcase()
string_head()
string_length()
string_mustache()
string_reverse()
string_split()
string_tail()
string_upcase()
sublist()
sum()
unique()
variablesmatching()
variablesmatching_as_data()
variance()

Functions by Return Type

real (i,r)range string int (i,r,s)list data class
mean() irange() and() accumulated() bundlesmatching() bundlestate() accessedbefore()
product() rrange() canonify() ago() callstack_promisers() callstack_callers() changedbefore()
sum() canonifyuniquely() countclassesmatching() classesmatching() data_expand() classify()
variance() concat() countlinesmatching() difference() data_readstringarray() classmatch()
dirname() diskfree() expandrange() data_readstringarrayidx() every()
escape() filesize() filter() data_regextract() fileexists()
eval() getfields() findfiles() data_sysctlvalues() filesexist()
execresult() getgid() getclassmetatags() datastate() groupexists()
file_hash() getuid() getindices() findprocesses() hashmatch()
filestat() length() getusers() getuserinfo() hostinnetgroup()
format() now() getvalues() mapdata() hostrange()
getenv() on() getvariablemetatags() mergedata() iprange()
hash() parseintarray() grep() network_connections() isdir()
host2ip() parserealarray() hostsseen() packagesmatching() isexecutable()
hubknowledge() parsestringarray() hostswithclass() packageupdatesmatching() isgreaterthan()
ifelse() parsestringarrayidx() intersection() parsejson() isipinsubnet()
ip2host() randomint() ldaplist() parseyaml() islessthan()
join() readintarray() lsdir() readcsv() islink()
lastnode() readrealarray() maparray() readdata() isnewerthan()
ldapvalue() readstringarray() maplist() readenvfile() isplain()
makerule() readstringarrayidx() peerleaders() readjson() isvariable()
max() selectservers() peers() readyaml() laterthan()
min() string_length() readintlist() url_get() ldaparray()
not() readreallist() variablesmatching_as_data() none()
nth() readstringlist() processexists()
or() reverse() regarray()
peerleader() shuffle() regcmp()
readfile() sort() regextract()
readtcp() splitstring() regldap()
regex_replace() string_split() regline()
registryvalue() sublist() reglist()
remotescalar() unique() remoteclassesmatching()
storejson() variablesmatching() returnszero()
strftime() some()
string_downcase() splayclass()
string_head() strcmp()
string_mustache() usemodule()
string_reverse() userexists()
string_tail()
string_upcase()
sysctlvalue()
translatepath()