Defaults promises are related to variables. If a variable or parameter in a promise bundle is undefined, or its value is defined to be invalid, a default value can be promised instead.

CFEngine does not use Perl semantics: i.e. undefined variables do not map to the empty string, they remain as variables for possible future expansion. Some variables might be defined but still contain unresolved variables. To handle this you will need to match the $(abc) form of the variables.

code
body common control
{
bundlesequence => { "main" };
}

bundle agent main
{
methods:

  "example"  usebundle => test("one","x","","$(four)");

}

bundle agent test(a,b,c,d)
{
defaults:

 "a" string => "default a", if_match_regex => "";
 "b" string => "default b", if_match_regex => "x";
 "c" string => "default c", if_match_regex => "";
 "d" string => "default d", if_match_regex => "\$\([a-zA-Z0-9_.]+\)";

reports:

   "a = '$(a)', b = '$(b)', c = '$(c)' d = '$(d)'";
}

Another example:

code
bundle agent example
{
defaults:

  "X" string => "I am a default value";
  "Y" slist => { "I am a default list item 1", "I am a default list item 2" };

methods:

 "example" usebundle => mymethod("","bbb");

reports:

   "The default value of X is $(X)";
   "The default value of Y is $(Y)";
}

###########################################################

bundle agent mymethod(a,b)
{
vars:

  "no_return" string => "ok"; # readfile("/dont/exist","123");

defaults:

  "a" string => "AAAAAAAAA",   if_match_regex => "";
  "b" string => "BBBBBBBBB",   if_match_regex => "";
  "no_return" string => "no such file";

reports:

     "The value of a is $(a)";
     "The value of b is $(b)";

     "The value of no_return is $(no_return)";
}

Attributes

if_match_regex

Description: If this anchored regular expression matches the current value of the variable, replace it with default.

If a parameter or variable is already defined in the current context, and the value matches this regular expression, it will be deemed invalid and replaced with the default value.

Type: string

Allowed input range: (arbitrary string)

Example:

code
bundle agent main

{     
  defaults:

      # We can have default values even if variables are not defined at all.
      # This is equivalent to a variable definition, so not particularly useful.

      "X" string => "I am a default value";
      "Y" slist => { "I am a default list item 1", "I am a default list item 2" };

  methods:

      # More useful, defaults if parameters are passed to a param bundle

      "example" usebundle => mymethod("","bbb");

  reports:

      "The default value of X is $(X)";
      "The default value of Y is $(Y)";
}


bundle agent mymethod(a,b)

{
  vars:

      "no_return" string => "ok"; # readfile("/dont/exist","123");

  defaults:

      "a" string => "AAAAAAAAA",   if_match_regex => "";

      "b" string => "BBBBBBBBB",   if_match_regex => "";

      "no_return" string => "no such file";

  reports:

      "The value of a is $(a)";
      "The value of b is $(b)";

      "The value of no_return is $(no_return)";

}
code
R: The value of a is AAAAAAAAA
R: The value of b is bbb
R: The value of no_return is ok
R: The default value of X is I am a default value
R: The default value of Y is I am a default list item 1
R: The default value of Y is I am a default list item 2

This policy can be found in /var/cfengine/share/doc/examples/defaults.cf and downloaded directly from github.