Mustache is a template system logic-less(no ifs, else’s or for loops) and can be used for HTML, config files, source code and so on.

CFEngine 3.6.x have support for Mustache, in this tutorial we will create a template and a CFEngine rule to parse the template and generate the file that we want.

Create the template file

/tmp/template.mustache

My hostname is: {{hostname}}
My name is: {{name}}
Fruits that I like: {{fruits}}

Our CFEngine file

/tmp/parse_template.cf

body common control
{
  bundlesequence => { "main", };
}

bundle agent main
{
  methods:
      "any" usebundle => parse;
}

bundle agent parse
{
  files:
      "/tmp/template.conf"
        edit_template => "/tmp/template.mustache",
        create => "true",
        template_method => "mustache",
        template_data => parsejson('{"hostname": "$(sys.uqhost)", "name": "Danilo", "fruits": "apple,banana,watermelon" }');
}

Lets run it and check the result

[root@myserver ~]# cf-agent -f template_parser.cf 

[root@myserver ~]# cat /tmp/template.conf
My hostname is: myserver
My name is: Danilo
Fruits that I like: apple,banana,watermelon

It’s simple yet powerful. You can use it in many ways, I use for configuration files(httpd.conf, nginx.conf, etc.).