templatescfengine

CFEngine 3.12: how to propagate templates to hosts?


CFEngine is great but I can't figure out how to copy the templates defined on the policy servers to the related hosts.

For example, I'm looking to deploy an nginx.conf, I made a policy on my main server:

bundle agent loadbalancers{

 files:
  ubuntu::
   "/etc/nginx/nginx.conf"
    create => "true",
    edit_template => "/tmp/nginx.conf.template",
    template_method => "mustache",
    template_data => parsejson('
       {
          "worker_processes": "auto",
          "worker_rlimit_nofile": 32768,
          "worker_connections": 16384,
        }
    ');
}

But obliviously, CFEngine can't find /tmp/nginx.conf.template on all others clients...

It looks like templates are not copied from the server to the clients, what I missed? I guess I miss understood something...

Documentation doesn't explain how to propagate template files, so I hope you could help me, thanks!


Solution

  • I'm glad you're enjoying CFEngine. If you want one file to be a copy of another file, you use a copy_from body to specify it's source.

    For example:

    bundle agent loadbalancers{
    
      files:
        ubuntu::
    
          "/tmp/nginx.conf.template"
            comment => "We want to be sure and have an up to date template",
            copy_from => remote_dcp( "/var/cfengine/masterfiles/templates/nginx.conf.mustache",
                                     $(sys.policy_hub));
    
          "/etc/nginx/nginx.conf"
            create => "true",
            edit_template => "/tmp/nginx.conf.template",
            template_method => "mustache",
            template_data => parsejson('
           {
              "worker_processes": "auto",
              "worker_rlimit_nofile": 32768,
              "worker_connections": 16384,
           }
        ');
    
    }
    

    Some people arrange for their templates to be copied as part of their normal policy updates, then it's very conveniant to just reference a template relateive to your policy file.

    For example, lets say your policy is in services/my_nginx_app/policy/loadbalancers.cf, and your template is services/my_nginx_app/templates/nginx.conf.mustache. Then, if that tempalte is updated as part of the normal policy update you don't have to promise a seperate file copy, instead just reference the path to the template relateve to the policy file.

    bundle agent loadbalancers{
    
      files:
        ubuntu::
    
          "/etc/nginx/nginx.conf"
            create => "true",
            edit_template => "$(this.promise_dirname)/../templates/nginx.conf.mustache",
            template_method => "mustache",
            template_data => parsejson('
           {
              "worker_processes": "auto",
              "worker_rlimit_nofile": 32768,
              "worker_connections": 16384,
           }
        ');
    
    }
    

    It's not always appropriate to send your templates to all hosts as part of your main policy set, it really depends on the needs of your environment.