Functions

Table of Contents

Functions are parameterized "RVALUES". Parameter values need to be of the type and range as documented for each functions. Some functions are documented with a ..., in which case they take an arbitrary amount of parameters.

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:

As of 3.6.0, the new cache_system_functions body common parameter is set to true by default and CFEngine's evaluator will use it. Although you can override it to false, in practice you should almost never need to do so. The effect of having it true (the default) is that the expensive system functions will be run just once and then their result will be cached.

Note that caching is per-process so results will not be cached between runs of e.g. cf-agent and cf-promises.

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 io data
accessedbefore() host2ip() classesmatching() getenv() countlinesmatching() accumulated()
changedbefore() hostrange() classmatch() getuid() data_readstringarray() ago()
dirname() hostsseen() countclassesmatching() getusers() data_readstringarrayidx() and()
diskfree() hostswithclass() datastate() groupexists() parseintarray() bundlesmatching()
fileexists() hubknowledge() execresult() hostinnetgroup() parsejson() canonify()
filesexist() ip2host() getclassmetatags() now() parserealarray() canonifyuniquely()
filesize() iprange() getvariablemetatags() packagesmatching() parsestringarray() classify()
filestat() ldaparray() isvariable() packageupdatesmatching() parsestringarrayidx() concat()
findfiles() ldaplist() returnszero() registryvalue() readfile() difference()
isdir() ldapvalue() splayclass() userexists() readintarray() escape()
isexecutable() peerleader() usemodule() readintlist() eval()
islink() peerleaders() variablesmatching() readjson() every()
isnewerthan() peers() readrealarray() filter()
isplain() readtcp() readreallist() format()
laterthan() regldap() readstringarray() getfields()
lsdir() remoteclassesmatching() readstringarrayidx() getgid()
translatepath() remotescalar() readstringlist() getindices()
selectservers() regline() getvalues()
grep()
hash()
hashmatch()
ifelse()
intersection()
irange()
isgreaterthan()
islessthan()
join()
lastnode()
length()
makerule()
maparray()
maplist()
max()
mean()
mergedata()
min()
none()
not()
nth()
on()
or()
product()
randomint()
regarray()
regcmp()
regextract()
reglist()
reverse()
rrange()
shuffle()
some()
sort()
splitstring()
storejson()
strcmp()
strftime()
string_downcase()
string_head()
string_length()
string_reverse()
string_split()
string_tail()
string_upcase()
sublist()
sum()
unique()
variance()

Functions by Return Type

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