Skip to content

Instantly share code, notes, and snippets.

@nickanderson
Created December 20, 2021 20:33
Show Gist options
  • Select an option

  • Save nickanderson/7ffaccf0a2bb6cf2e9ddfc0996dbc544 to your computer and use it in GitHub Desktop.

Select an option

Save nickanderson/7ffaccf0a2bb6cf2e9ddfc0996dbc544 to your computer and use it in GitHub Desktop.
Accessing current iterated dict key name and key values with Mustache in CFEngine
  • @ Renders the currently iterated key
  • . Accesses a value

    So, if you are iterating over the dict aliases which is a dict of dicts:

    {
        "aliases": {
            "key1": {
                "msg": "Hello from 1",
                "msg2": "Hello2 from 1"
            },
            "key2": {
                "msg": "Hello from 2",
                "msg2": "Hello2 from 2"
            }
        }
    }
        

You could do this:

bundle agent __main__
{
  vars:
      "d" data => '{
  "aliases": {
    "key1": {
      "msg": "Hello from 1", 
      "msg2": "Hello2 from 1" 
    },
    "key2": {
      "msg": "Hello from 2", 
      "msg2": "Hello2 from 2" 
    }
  }
}';

  reports:
       "$(with)"
         with => string_mustache( "$(const.n){{#aliases}} {{@}}:$(const.t){{.msg}}|{{{.msg2}}}$(const.n){{/aliases}}", d );


}
R: 
 key1:	Hello from 1|Hello2 from 1
 key2:	Hello from 2|Hello2 from 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment