Variables

27 views
Skip to first unread message

thierry thunot

unread,
Nov 23, 2021, 10:32:25 AM11/23/21
to help-cfengine
Hello everyone, I have a little problem with Cf. I want to use parameterized variables to create classes more easily .. I tested this for apache masi it is the philosophy that interests me to do this kind of test

bundle agent    configuration_test_apache
{
        vars:
                "liste_module_apache" slist =>  {"rewrite","ssl","session"};
                "test_module_${liste_module_apache}_apache" string => execresult("/usr/sbin/apache2ctl -M|grep ${liste_module_apache}","noshell");
       
classes:
                "module_${liste_module_apache}_nok" expression => strcmp("${test_module_${liste_module_apache}_apache}","");
        reports:
                module_${liste_module_apache}_nok::
                "MODULE APACHE ${liste_module_apache} pas présent";
                

}

Can you advise me on the best way to proceed?
Thank's !!! ;)

Nick Anderson

unread,
Nov 23, 2021, 3:05:37 PM11/23/21
to thierry thunot, help-c...@googlegroups.com

thierry thunot <tth...@gmail.com> writes:

Hello everyone, I have a little problem with Cf. I want to use parameterized variables to create classes more easily .. I tested this for apache masi it is the philosophy that interests me to do this kind of test

bundle agent configuration_test_apache { vars: "liste_module_apache" slist => {"rewrite","ssl","session"}; "test_module_${liste_module_apache}_apache" string => execresult("/usr/sbin/apache2ctl -M|grep \({liste_module_apache}","noshell");
> > classes: > "module_\){liste_module_apache}_nok" expression =>
strcmp("\({test_module_\){liste_module_apache}_apache}",""); reports: module_${liste_module_apache}_nok:: "MODULE APACHE ${liste_module_apache} pas présent";


}

Can you advise me on the best way to proceed? Thank's !!! ;)

Hi Thierry

Not sure what specifically you are looking for, but on a quick scan of your policy:

I don't believe that you can pipe inside an execresult() without using a shell.

execresult("/usr/sbin/apache2ctl -M|grep ${liste_module_apache}","noshell");

Also, I don't think it applies to apache modules since I don't think there are any whose names contain invalid class characters (e.g. -), but if you are creating classes from arbitrary strings, you should canonify() when checking.

"MODULE APACHE ${liste_module_apache} pas présent"
  if => canonify( ${liste_module_apache} );

And finally, you must quote a variable class expression, again, beware if any liste_module_apache entry contains any characters that are not valid in a class name, this won't work.

thierry thunot

unread,
Nov 26, 2021, 12:44:47 PM11/26/21
to help-cfengine
hi !!
I did this to meet my needs

vars:
       "services_testes" slist => { "sshd","apache2","cups" };
       "test_$(services_testes)" string => execresult("/usr/bin/ps aux|grep $(services_testes)|/usr/bin/grep -v grep |/usr/bin/grep -v \@","useshell");
       classes:
       "service_ok_$(services_testes)" expression => strcmp("","$(test_$(services_testes))");

       reports:
               "Service present $(services_testes)"
               unless => "service_ok_$(services_testes)";

               "Service NON present $(services_testes)"
               if => "service_ok_$(services_testes)";

Thank's again for your Help Nick!

Nick Anderson

unread,
Nov 29, 2021, 8:23:23 PM11/29/21
to thierry thunot, help-cfengine

--
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to help-cfengin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/fb1c2ea7-dba9-4e11-a4d7-5a029b391c85n%40googlegroups.com.

thierry thunot

unread,
Nov 30, 2021, 2:23:54 AM11/30/21
to help-cfengine
Ho yes Nick i am not using these functions, it was an example to use parametrics classes.  Thanks for your help (Again and again ;) )
Reply all
Reply to author
Forward
0 new messages