Forum: CFEngine Help
Subject: Re: issue using PCRE regex to filter part of the line
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,24600,24608#msg-24608

This is a bit of a kludge, but it works. The add_parameter() bundle reads the 
file and determines which ones start with the desired keyword ("kernel" in this 
case), and which ones already contain the parameter ("elevator" in this case). 
Then it adds the parameter only to the files that start with the keyword AND do 
not contain already the parameter.



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

bundle agent main
{
  files:
      "/tmp/grub.conf"
        edit_line => add_parameter("kernel ", "elevator", "noop", 
"/tmp/grub.conf");
}

bundle edit_line add_parameter(lineselect, p, value, file)
{
  vars:
      "lines" slist => readstringlist("$(file)", "#.*", "\n", 9999999, 9999999);
      "clines[$(lines)]"   string => canonify("$(lines)");

  classes:
      # Set classes for the lines that start with $(lineselect) and already 
contain $(p)=
      "has_param_$(clines[$(lines)])"  expression => 
regcmp("$(lineselect).*$(p)=.*", "$(lines)");
      # Set classes for lines that start with $(lineselect)
      "must_edit_$(clines[$(lines)])"  expression => regcmp("$(lineselect).*", 
"$(lines)");

  field_edits:
      "$(lines)"
        edit_field => line("^", "1", "$(p)=$(value)", "append"),
      # Edit only lines that start with $(lineselect) and do not contain $(p)=
        ifvarclass => 
"must_edit_$(clines[$(lines)])&!has_param_$(clines[$(lines)])";
}


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

Reply via email to