Forum: CFEngine Help
Subject: Re: convert class name into a string
Author: sauer
Link to topic: https://cfengine.com/forum/read.php?3,24123,24184#msg-24184

msvob...@linkedin.com Wrote:
-------------------------------------------------------
> Sauer, good suggestion, but allclasses.txt are
> only global classes.  Local classes wouldn't work.

Nope - allclasses.txt is kept up-to-date as cf-agent works its way through the 
promises.  At the end of execution (which is what you see when you look at the 
file if cf-agent isn't actively running), you're probably only seeing global 
classes because your last bundle probably doesn't define any local classes.  
Demonstration time!


$ cat ./classes.cf
#!/var/cfengine/bin/cf-agent -Kf
body common control{ bundlesequence => { "a", "b" }; }
bundle agent a {
classes:
  "localclass"     or => { "linux", "!linux" };
commands:
  "/bin/grep class '$(sys.workdir)/state/allclasses.txt'";
reports:
  someotherclass:: "a Some Other Class";
  localclass::     "a Local Class";
}
bundle agent b {
classes:
  "someotherclass" or => { "linux", "!linux" };
commands:
  "/bin/grep class  '$(sys.workdir)/state/allclasses.txt'";
reports:
  someotherclass:: "b Some Other Class";
  localclass::     "b Local Class";
}

$ ./classes.cf
Q: ".../bin/grep class": localclass
I: Last 1 quoted lines were generated by promiser "/bin/grep class 
'/home/user/.cfagent/state/allclasses.txt'"
R: a Local Class
Q: ".../bin/grep class": someotherclass
I: Last 1 quoted lines were generated by promiser "/bin/grep class  
'/home/user/.cfagent/state/allclasses.txt'"
R: b Some Other Class

$ grep class ~user/.cfagent/state/allclasses.txt
someotherclass


Note how allclasses.txt contains the classes which were in effect when the last 
bundle was processed at the end.  In each bundle, however, the grep found that 
a locally-defined class existed in allclasses.txt.  In bundle b, the 
locally-defined class from bundle a is also not found.

Something the more attentive reader might note: I had to slightly change the 
grep command in bundle b, because cf-agent was initially skipping it as the 
same command had already been validated in a previous bundle.  That's why the 
one in bundle b has an extra space before the filename argument; that's enough 
to throw off the "was this validated before" check.

In any event, from this one can infer that it's also possible to use a module 
script to parse allclasses.txt and set variables, in the event that the 
CFEngine syntax does not allow for easy enough handling of the file / selection 
of variable names.  Neil could probably write a quick shell/perl script which 
looks for a pattern in class names and sets variables from those patterns, for 
example. :)

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

Reply via email to