datastate

Table of Contents

Prototype: datastate()

Return type: data

Description: Returns the current evaluation data state.

The returned data container will have the keys classes and vars.

Under classes you'll find a map with the class name as the key and true as the value. Namespaced classes will be prefixed as usual.

Under vars you'll find a map with the bundle name as the key (namespaced if necessary). Under the bundle name you'll find another map with the variable name as the key. The value is converted to a data container (JSON format) if necessary. The example should make it clearer.

Mustache templates (see template_method), if not given a template_data, will use the output of datastate() as their input.

Example:

body common control
{
      bundlesequence => { holder, test };
}

bundle common holder
{
  classes:
      "holderclass" expression => "any"; # will be global

  vars:
      "s" string => "Hello!";
      "d" data => parsejson('[4,5,6]');
      "list" slist => { "element1", "element2" };
}

bundle agent test
{
  vars:
      "state" data => datastate();

      # all the variables in bundle "holder" defined as of the execution of datastate() will be here
      "holderstate" string => format("%S", "state[vars][holder]");
      # all the classes defined as of the execution of datastate() will be here
      "allclasses" slist => getindices("state[classes]");

  classes:
      "have_holderclass" expression => some("holderclass", allclasses);

  reports:
      "holder vars = $(holderstate)";
    have_holderclass::
      "I have the holder class";
}

Output:

R: holder vars = {"d":[4,5,6],"list":["element1","element2"],"s":"Hello!"}
R: I have the holder class

See also: getindices(), classesmatching(), variablesmatching(), mergedata(), template_method, mustache, inline_mustache, bundlestate()

Notes:

  • Beware, when assigning datastate() to a variable, multiple passes will result in recursive growth of the data structure. Consider guarding against re-definition of a variable populated by datastate().

Example illustrating how to prevent recursive growth of variable populated by datastate().

bundle agent main
{
  vars:
    "_state"
      data => datastate(),
      unless => isvariable( $(this.promiser) );
}

History:

  • Introduced in CFEngine 3.6.0