Setting default variables in CFEngine


Do you want to set a default value for variables in CFEngine? Here are two methods. The first uses defaults promises. Defaults promises can be bit confusing and I've seen bugs in them in the past, so test carefully when using them. See the CFEngine reference for more details on defaults.

The second method is a normal variables promise, but uses the ifelse function. Ifelse takes the form of

"myvar" string => ifelse(
   "class1", "value1",
   "classn+1", "valuen+1",
   "default value" );

The agent checks each class condition and assigns the value of the variable for the correct class. Assume that the first correct value will be assigned even if more than one class is true, but variable policy bugs may deem otherwise (see, so test carefully. If no class condition is meet, the final value, that has no class, is assigned.

body common control
   bundlesequence => { "g", "main", };

bundle common g
      "a" string => "default value";

      "b" string => ifelse(
         "set", "set value for 'b'",
         "default value for 'b'"

         "a" string => "A 'set' value";

bundle agent main
      "a is ${g.a}";
      "b is ${g.b}";

neil@ettin ~/.cfagent/inputs $ cf-agent -Kf ./
R: a is default value
R: b is default value for 'b'

neil@ettin ~/.cfagent/inputs $ cf-agent -Kf ./ -D set
R: a is A 'set' value
R: b is set value for 'b'
submit to reddit