Forum: Cfengine Help
Subject: Re: Search and replace... need some regex help, please. handling the 
scenario where the setting has drifted.
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,22667,22681#msg-22681

Aleksey,

I think for this you could use my modified set_variable_values promise 
http://github.com/zzamboni/cfengine-copbl/blob/master/cfengine_stdlib.cf#L170 :

bundle edit_line set_variable_values(v)

 # Sets the RHS of configuration items in the file of the form
 #   LHS = RHS
 # If the line is commented out with #, it gets uncommented and set to
 # the correct value.
 # Adds a new line if none exists.
 
 # To use:
 #   1) Define an array, where the keys are the LHS and the values are the RHS
 #        "stuff" string => "rhs1";
 #        "stuff" string => "rhs2";
 #   2) The parameter passed to the edit_line promise is the fully qualified
 #      name of the array (i.e., "bundlename.stuff") WITHOUT any "$" or "@"

{
vars:
  "index" slist => getindices("$(v)");

  # Be careful if the index string contains funny chars
  "cindex[$(index)]" string => canonify("$(index)");

replace_patterns:
  # If the line is there, maybe commented out, uncomment and replace with
  # the correct value
  "^[\s#]*$(index)\s*=.*$"
    replace_with => value("$(index)=$($(v)[$(index)])"),
    classes => always("replace_attempted_$(cindex[$(index)])");

insert_lines:
  "$(index)=$($(v)[$(index)])"
    ifvarclass => "replace_attempted_$(cindex[$(index)])";

}


The trick for this to work is to tweak normal order inside the edit_line 
promise (insert_lines:, then replace_patterns:) by setting a class only after 
the replace_patterns promise is evaluated, regardless of the result. When 
insert_lines is evaluated, it will only insert the new line if it is not there 
yet (this is, if it was never there in the first place. If replace_patterns: 
fixed a previously-existing line, maybe by uncommenting it, then it will not be 
inserted again).

The "always" body part looks like this:

body classes always(x)

# Define a class no matter what the outcome of the promise is

{
  promise_repaired => { "$(x)" };
  promise_kept => { "$(x)" };
  repair_failed => { "$(x)" };
  repair_denied => { "$(x)" };
  repair_timeout => { "$(x)" };
}


Hope this helps.

_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to