Prototype: data_expand(data_container)

Return type: data

Description: Transforms a data container to expand all variable references.

This function will take a data container and expand variable references once in all keys and values.

This function can accept many types of data parameters.

Any compound (arrays or maps) data structures will be expanded recursively, so for instance data in a map inside another map will be expanded.

This function is chiefly useful if you want to read data from an external source and it can contain variable references.

Arguments:

  • data_container: string - CFEngine variable identifier or inline JSON - in the range: .*

Example:

Prepare to run the example policy:

code
echo '{ "$(main.x)": "$(main.y)" }' > /tmp/expand.json

Policy:

code
bundle agent main
{
  vars:
      "x" string => "the expanded x";
      "y" string => "the expanded y";

      "read" data => readjson("/tmp/expand.json", inf);
      "expanded" data => data_expand(read);

      "expanded_str" string => format("%S", expanded);

  reports:
      "$(this.bundle): the x and y references expanded to $(expanded_str)";
}

Output:

code
R: main: the x and y references expanded to {"the expanded x":"the expanded y"}

Notes:

History: Was introduced in version 3.7.0 (2015). The collecting functions behavior was added in 3.9.

See also: readcsv(), readjson(), readyaml(), mergedata(), readenvfile(), classfiltercsv(), about collecting functions, and data documentation.