Forum: CFEngine Help
Subject: Re: Dealing with vars changing
Author: sauer
Link to topic: https://cfengine.com/forum/read.php?3,24603,24614#msg-24614

Because the order in which things appear in the file are more of a suggestion 
than a requirement (declarative language and all) describing the order in which 
promises are evaluated, I prefer to handle this with a pair of bundles.  Bundle 
the first is a common bundle which defines the classes which define a system 
getting a non-default value (based on whatever other criteria), and a 
meta-class which determines if a system gets the default or not (by combining 
all of the override classes).  The second bundle actually sets the vars based 
on the common classes.  So, here's an example.  It could be made a little more 
portable by making the common class name a variable passed in to the common 
bundle as a parameter, making the override suffixes and criteria an array of 
slists (also passed in as a parameter), and using ifvarclass in the vars 
section of the agent bundle rather than regular classes.  But I think the way I 
posted it is way simpler to read. :)

Either way, though, I like this a lot better than using a free policy, because 
there's no warnings about overriding variables or any such craziness - and the 
variables are consistent across all three passes through the agent bundle; 
there's no need to wait for the free variables to calm down on a second or 
third pass.  You separate out the critera for overriding from the actual 
selection of a default or non-default value, resulting in easier-to-read 
variable assignments through abstraction.


sauer@midnight:~$ cat ./conditionalvars.cf 
#!/usr/local/sbin/cf-agent -Kf
body common control { bundlesequence => { "a" }; }
bundle agent a{
methods:
  "prep" usebundle => prep_for_vars;
  "go"   usebundle => dostuff;
}
bundle common prep_for_vars {
classes:
  "var_override_myval_1" or => { "host1", "host2" };
  "var_override_myval_2" or => { "host3", "host4" };
  "var_override_myval"   expression => classmatch("var_override_myval_.*");
}
bundle agent dostuff {
vars:
  any::
    "default_myval" string => "default";
  var_override_myval_1::
    "myval" string => "Overridden to 1";
  var_override_myval_2::
    "myval" string => "Overridden to 2";
  !var_override_myval::
    #"myval" string => "$(default_$(this.promiser))";
    "myval" string => "$(default_myval)";
reports:
  cfengine:: "MyVal is '$(myval)'";
}
sauer@midnight:~$ ./conditionalvars.cf
R: MyVal is 'default'
sauer@midnight:~$ ./conditionalvars.cf -Dhost1
R: MyVal is 'Overridden to 1'
sauer@midnight:~$ ./conditionalvars.cf -Dhost3
R: MyVal is 'Overridden to 2'


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

Reply via email to