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 <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