Forum: CFEngine Help
Subject: Re: classes visibility in edit_template?
Author: tdr
Link to topic: https://cfengine.com/forum/read.php?3,26375,26388#msg-26388

Hi,

I hit this when first trying out edit_template as well (just this past 
weekend...).  I'd like to suggest that the documentation and example of 
edit_template use be updated to more cover the scope issue.  I think the 
documentation should cover this scope issue explicitly and show use of a user 
defined class instead of just built-in (global scoped) classes.   
http://cfengine.com/manuals/st-editing#Contextual-adaptation-of-a-file-template 
 One more thing on the docs for edit_template, it would be nice to cover the 
default behavior of stripping duplicate lines from the template.

In the end, it seems simple enough, the edit_template is executed in a 
different scope than the calling bundle, so classes and variables local to the 
calling bundle are not local to the template.  Variables in the non-local scope 
can just be referenced with the $(bundlename.varname) syntax, but there is no 
equivalent syntax for classes.  I just made the classes I wanted available in 
the template global scope by putting them in a common bundle.  This is just 
what Riccardo showed.  I guess it is also possible to pass the classes into the 
scope of the edit_template execution, but I didn't try that.

I'm pulling (or checking) the template from the server to client on each run 
placing it in a /var/cfengine/stage and then making (or checking) the final 
file.  I didn't see any instances in testing where this didn't correctly update 
the final output file in one run of cf-agent.  So, I didn't hit any updating 
issue.

My test_template.cf file looked like this (sorry this is from memory and not 
tested in this exact form):

bundle common test_common
{
  classes:
     "test_class" expression => "linux";
}

bundle agent test_template
{
   vars:
     "test_var" string => "la la la";

     "source_file" string => "/var/cfengine/masterfiles-devel/stash/test.tmpl";
     "stage_file" string => "/var/cfengine/stage/test.tmpl";
     "result_file" string => "/tmp/test_tmpl.out";

   files:
     "$(stage_file)"
        comment => "the staged template",
        perms => mog("0600","root","root"),
        copy_from => secure_cp("$(source_file)","cfe-server.local");

    "$(result_file)"
       create => "true",
       perms => mog("0644","root","root"),
       edit_template => "$(stage_file)";
}


Notes: CFEngine automatically uses the common bundle; don't need to call it 
explicitly.  We're using the community stdlib, but I copied the 3 bodies used 
into this same file for testing.  Can run above with: cf-agent -f 
./test_template.cf -b test_template  Can use "test_class::" and 
"$(test_template.test_var)" in template file.

In the end I did understand scope a bit better after working though this, but...

Cheers,
Tom Rockwell

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to