Forum: CFEngine Help
Subject: Re: Methods and default variables
Author: sauer
Link to topic: https://cfengine.com/forum/read.php?3,23009,23055#msg-23055

neilhwatson Wrote:
-------------------------------------------------------
> Theoretical question.  Consider this
> 
> methods:
> 'any' usebundle => foo("x","y","z");
> 
> ...
> 
> bundle foo (x,y,z) {
> 
> Do something
> }
> 
> Can there be defaults for the three variables
> inside foo so that one can pass just a subset of x
> y and z leaving the defaults to fill in what is
> missing?

I'd be inclined to do something along these lines, since you apparently can't 
overload bundles like you can bodies.  Basically set the variable, and have the 
bundle define a class based on whether or not the external var is set.


sauer@machine
$ ./test.cf3 -D DOVAR
R: Set file to /tmp/nosuchfile
sauer@machine
$ ./test.cf3
R: Set file to /something
sauer@machine
$ cat test.cf3
#!/var/cfengine/bin/cf-agent -Kf
body common control{ bundlesequence => { "a" }; }
bundle agent a {
methods:
  DOVAR:: "x" usebundle => setvar("file", "/tmp/nosuchfile");
  any::   "y" usebundle => doreport;
}
bundle agent doreport {
vars:
  "defaultfile" string => "/something";
classes:
  "hasvar" expression => isvariable("setvar.file");
reports:
  hasvar::
  "Set file to $(setvar.file)";
  !hasvar::
  "Set file to $(defaultfile)";
}
bundle agent setvar(var,val) {
vars:
  "$(var)" string => "$(val)";
}


Of course, you need to be somewhat careful with overwriting the variables when 
calling the bundle more than once, and this gets into somewhat sketchy 
namespace management.  But it does work without warnings. :)

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

Reply via email to