CFEngine 3.6 to increase data loading flexibility - Introducing the new data type 'data'

Posted by Mahesh Kumar
November 27, 2013

sigurd.teigen@cfengine.com R&D, CFEngine CFEngine has previously had multiple ways of loading data from an external file into a policy. This has been useful for basing policy on tablular data. For example, importing a CSV file to create accounts. Once data has been imported, it may be used using the existing CFEngine scalar and list data types. In order to make this more flexible, we are introducing a new data type ‘data’ for version 3.6 of CFEngine. A data container variable is an arbitrarly nested data structure of CFEngine primitives. You may think of a data container as a JSON document, because initially we are only supporting importing from JSON. However, this may change in the future allowing for importing data from YAML, XML and other files. Let us examine the flexibility and ease of use of this data type by way of an example. bundle agent test { vars: “doc” data => parsejson(’{ “a”: [ 1, 2 ], “b”: true }’); reports: “Hello $(doc[a]) $(doc[b])”; } This will output the following. $ cf-agent/cf-agent test.cf 2013-11-25T15:18:32-0500 notice: /main: R: Hello 1 true 2013-11-25T15:18:32-0500 notice: /main: R: Hello 2 true In this example, we use the function parsejson to assign a new data container. A data container may either be iterated as a list or as a scalar, depending on how it’s indexed. Above, we see that we index into a list ‘a’ in ‘doc’. Meanwhile, ‘b’ in ‘doc’ refers to a scalar. A list may contain other container types (list or maps), but only sibling scalars will be iterated over. Indexing works as in JavaScript, for example $(doc[a][1]) would refer to the second element in the list ‘a’ in ‘doc’. Currently, there is no inline syntax for data containers, although this is being planned for. A function readjson is available to import file, while function mergedata may be used to join existing data container variables into a new variable. We hope that data containers will be a practical way of working with structured data in policy, replacing current uses of arrays. Data containers are also used with the new Mustache template feature also in version 3.6. Learn more about CFEngine by visiting our website Evaluate CFEngine for free (upto 25 licenses, unlimited usage)