Re: variable if class

Xander Cage <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
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...



On Tuesday, November 9, 2021 at 3:21:25 PM UTC+1 Nick Anderson wrote:

> Bas van der Vlies <[email protected]> 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: 
> Listing 1: Example Snippet
>
> 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. 
> Listing 2: Example Policy
>
> 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";
>
>
> }bundle agent __main__{
>   methods:
>     "var_if_test";}
>
>

-- 
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 [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/e2718853-739e-4fa0-9f01-a672118abb02n%40googlegroups.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.