Forum: CFEngine Help
Subject: Re: CFEngine Help: weird error on variable and report not working
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,23210,23485#msg-23485

Jerome,

The "duplicate selection" message appears because the variable is defined 
twice: on the first pass before the file is fixed, and on the second pass after 
the line has been inserted. You may notice that if the line is already there, 
the message does not appear - this is because in this case there is no second 
pass, because the promise was already kept (second and third passes occur when 
something has been fixed, to check if there is something else to do).

Moreover, your policy is much more complex than it needs to be. You do not need 
to check if the line already exists, because cfengine's insert_lines: promise 
does that for you automatically. The following code does exactly the same, and 
is much shorter and clearer:

body common control {
  bundlesequence => { "exampleInsertLine" };
  inputs => { "cfengine_stdlib.cf" };
}

bundle common g
{
  vars:
    "alias_vi"          string  => "alias vi=\"vim\"";
    "bashrc_loc"        string  => "/etc/bash.bashrc";
}

bundle agent exampleInsertLine
{
  files:
     "$(g.bashrc_loc)"
        edit_line => append_if_no_line("$(g.alias_vi)"),
        classes => if_repaired("alias_inserted");

  reports:
    alias_inserted::
      "Alias for vi has been inserted.";
    !alias_inserted::
      "Alias for vi pointing to vim already exists.";
}

If you look at append_if_no_line in cfengine_stdlib.cf, you will see that it's 
a bare insert_lines: promise. The default behavior is to insert the line only 
if it's not there already.

Best regards,

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

Reply via email to