CFEngine makes it easy to render JSON data. This is incredibly useful for coordinating and exchanging data with other systems. Sometimes, data provided to other systems needs to be constructed from multiple parts. When preparing a data container for rendering with mergedata() beware that the true and false values are special and will not be replaced by data containers of the same name.

This example illustrates how the true and false bare values are not expanded even when data containers matching those names exist.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  bundle agent example
  {
    vars:
        "false" data => '{ "one": "two", "Buckle": "Shoe" }';
        "true" data => '{ "three": "four", "Shut": "Door" }';
        "f" data => '{ "one": "two", "Buckle": "Shoe" }';
        "t" data => '{ "three": "four", "Shut": "Door" }';

    reports:
        "CFEngine $(sys.cf_version)";

        "NOTE 'f' and 't' are expanded$(with)"
          with => string_mustache( "{{$-top-}}",
                                   mergedata(
                                              '{
  "configured": true,
  "watch_out_for_false": f,
  "watch_out_for_true": t
                                              }') );

        "NOTE 'false' and 'true' are NOT expanded$(with)"
          with => string_mustache( "{{$-top-}}",
                                   mergedata(
                                              '{
  "configured": true,
  "watch_out_for_false": false,
  "watch_out_for_true": true
                                              }') );
  }
  bundle agent __main__
  {
    methods:
        "example";
  }
R: CFEngine 3.14.0a.ed0158a8e
R: NOTE 'f' and 't' are expanded{"configured":true,"watch_out_for_false":{"Buckle":"Shoe","one":"two"},"watch_out_for_true":{"Shut":"Door","three":"four"}}
R: NOTE 'false' and 'true' are NOT expanded{"configured":true,"watch_out_for_false":false,"watch_out_for_true":true}