Can I access the current KEY when iterating in Mustache?

Yes, CFEngine's implementation of mustache includes an extension to be able to access the current key when iterating. @ expands the current key being iterated.

This example defines d as a simple json data structure with index-1 and =index-2= being top level keys. The mustache template iterates over the entirety of the structure leveraging -top- (another cfengine specific extension to mustache) and prints the key using @. Note the use of three curly braces. This is per the mustache spec to ensure that the rendered value is not html escaped (not usually what you want in the context of rendering configuration files).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  bundle agent example
  {
    vars:

        'd' data => '{
    "index-1": {
      "value": "one"
    },
    "index-2": {
      "value": "two"
    }
  }';

    reports:
        "CFEngine $(sys.cf_version)"; 
        "$(with)"
          with => string_mustache( "Iterate over -top- and print keys:$(const.n){{#-top-}}{{{@}}}$(const.n){{/-top-}}", d );
  }
  bundle agent __main__
  {
    methods: "example";
  }

Policy Output:

R: CFEngine 3.14.0a.4e12fcf75
R: Iterate over -top- and print keys:
index-1
index-2