variable if class

68 views
Skip to first unread message

Xander Cage

unread,
Nov 9, 2021, 7:18:45 AM11/9/21
to help-cfengine

hi,

i read this -> https://cmdln.org/2018/05/24/what-is-evaluated-first-in-cfengine-3-vars-or-classes/  and i tried to use this higher kind of wisdom, but ...noooo

just want to read in a file in a string var if a specific class is defined eg. "the file exists".

body file control
{
  inputs => { "$(sys.libdir)/stdlib.cf", };
             }



bundle agent var_if_test {

    vars:
     "user" string => "schast";

     "ssh_key_dir" string => "/root/cfe_testbed/user_ssh_keys";

     "ssh_key" string => readfile("$(ssh_key_dir)/$(user).cf"),
               if => ( "exists_ssh_key" );


    classes:


            "exists_ssh_key"          expression => fileexists("$(ssh_key_dir)/$(user).cf");

    reports:

        "CFEngine $(sys.cf_version)";

      "$(ssh_key)"
        if => "exists_ssh_key";


}



bundle agent __main__
{
  methods:
      "var_if_test";
}

error:

root@aixtest01: /root/cfe_testbed # /var/cfengine/bin/cf-agent -KI  -f ./var_if_test.cf
./var_if_test.cf:17:23: error: syntax error
               if => ( "exists_ssh_key" );
                      ^
./var_if_test.cf:17:23: error: Invalid r-value type '('
               if => ( "exists_ssh_key" );
                      ^
./var_if_test.cf:17:40: error: Check previous line, Expected ';', got '"exists_ssh_key"'
               if => ( "exists_ssh_key" );
                                       ^
./var_if_test.cf:17:42: error: Expected promiser string, got ')'
               if => ( "exists_ssh_key" );
                                         ^
   error: There are syntax errors in policy files
   error: Policy failed validation with command '"/var/cfengine/bin/cf-promises" -c "./var_if_test.cf"'
   error: Failsafe condition triggered. Interactive session detected, skipping failsafe.cf execution.
   error: Error reading CFEngine policy. Exiting...

Bas van der Vlies

unread,
Nov 9, 2021, 8:02:46 AM11/9/21
to Xander Cage, help-cfengine
Hi Xander,

your syntax is not correct: if => ( "exists_ssh_key" );
must be: if => "exists_ssh_key";

regards
> --
> 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
> <mailto:help-cfengin...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/help-cfengine/684af289-094e-420e-85ab-af4030b20decn%40googlegroups.com
> <https://groups.google.com/d/msgid/help-cfengine/684af289-094e-420e-85ab-af4030b20decn%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Bas van der Vlies
| HPCV Supercomputing | Internal Services | SURF |
https://userinfo.surfsara.nl |
| Science Park 140 | 1098 XG Amsterdam | Phone: +31208001300 |
| bas.van...@surf.nl

Nick Anderson

unread,
Nov 9, 2021, 9:21:25 AM11/9/21
to Bas van der Vlies, Xander Cage, help-c...@googlegroups.com

Bas van der Vlies <bas.van...@surf.nl> writes:

Hi Xander,

your syntax is not correct: if => ( "exists_ssh_key" ); must be: if => "exists_ssh_key";

Right,

Alternatively, if there is just a single promise like this that you are guarding you can instead use a function.

For example:

reports:

    "CFEngine $(sys.cf_version)";

    "$(ssh_key)"
      # if => "exists_ssh_key";
      if => fileexists( "$(ssh_key_dir)/$(user).cf" );

But the benefit of having the class is that it can apply to multiple promises without having to re-check the file or re-state the dependancy.

bundle agent var_if_test {

  vars:
      "user" string => "schast";

      "ssh_key_dir" string => "/root/cfe_testbed/user_ssh_keys"
;

    exists_ssh_key::
      "ssh_key" string => readfile("$(ssh_key_dir)/$(user).cf");

  
classes:

      "exists_ssh_key"
        expression => fileexists("$(ssh_key_dir)/$(user).cf");

  reports:

      "CFEngine $(sys.cf_version)"
;

    exists_ssh_key::
      "$(ssh_key)";

      "another promise, only if exists_ssh_key is defined";

Xander Cage

unread,
Nov 9, 2021, 10:00:34 AM11/9/21
to help-cfengine
ah...cool, but  it somehow dont want to iterate now when using a list of users, btw...this is a problem which makes me run in circles quite often...

bundle agent var_if_test {

    vars:
     "user" slist => {"schast", "wimm", "schama" };

     "ssh_key_dir" string => "/root/cfe_testbed/user_ssh_keys";

     "ssh_key"      string => readfile("$(ssh_key_dir)/$(user).cf"),
                       if => "exists_ssh_key_$(user)";


    classes:


            "exists_ssh_key_$(user)"          expression => fileexists("$(ssh_key_dir)/$(user).cf");

    reports:

        "CFEngine $(sys.cf_version)";

      "$(user) -> $(ssh_key)"
        if => "exists_ssh_key";



}



bundle agent __main__
{
  methods:
      "var_if_test";
}

root@aixtest01: /root/cfe_testbed # /var/cfengine/bin/cf-agent -KI --show-evaluated-vars=var_if_test -f ./var_if_test.cf
R: CFEngine 3.15.4
Variable name                            Variable value                                               Meta tags
default:var_if_test.ssh_key              ssh-schamakey                                                source=promise
default:var_if_test.ssh_key_dir          /root/cfe_testbed/user_ssh_keys                              source=promise
default:var_if_test.user                  {"schast","wimm","schama"}                                  source=promise

only key of last item in list (key, user) is shown...

Nick Anderson

unread,
Nov 9, 2021, 11:41:45 AM11/9/21
to Xander Cage, help-c...@googlegroups.com

Xander Cage <christia...@itsv.at> writes:

Hi Xander,

You were missing the $(user) variable in your condition guarding your reports type promise. Also, you were populating ssh_key with the value of the iterated $(user), so it will only ever hold the last iterated value. Instead, you can define a variable for each user …

bundle agent var_if_test {

  vars:
      
"user" slist => {"schast", "wimm", "schama" }
;

      #"ssh_key_dir" string => "/root/cfe_testbed/user_ssh_keys";
      "ssh_key_dir" string => "/tmp";

      "ssh_key_$(user)"      string => readfile("$(ssh_key_dir)/$(user).cf"),
        if => "exists_ssh_key_$(user)";

  
classes:

      "exists_ssh_key_$(user)"
        expression => fileexists("$(ssh_key_dir)/$(user).cf");

  reports:

      "CFEngine $(sys.cf_version)"
;

      "$(user) -> $(ssh_key_$(user))"
        if => "exists_ssh_key_$(user)";

      "ABSENT: $(user)"
        unless => "exists_ssh_key_$(user)";

}

bundle agent __main__
{
  methods:
      "var_if_test";
}

R: CFEngine 3.19.0a.da01eaa81
R: wimm -> hello
R: ABSENT: schast
R: ABSENT: schama
Reply all
Reply to author
Forward
0 new messages