Forum: Cfengine Help
Subject: Re: regexp parameter quoting
Author: jonb1987
Link to topic: https://cfengine.com/forum/read.php?3,21851,21869#msg-21869

OK, so I think cfengine is in the clear after all.
The difficulty I had was with understanding exactly where the problem lay.
With some more detailed invetigation I've ended up with this, that seems to 
work...


#----------------------------------------------------------
bundle agent main {
#----------------------------------------------------------
files:
      any::
         "/etc/passwd"
            handle       => "etc_passwd",
            comment      => "Set compat mode in passwd",
            edit_line    => fix_line("^\+::::::.*", "+::::::/bin/bash");
}
#----------------------------------------------------------
bundle edit_line fix_line(pattern, line) {
#----------------------------------------------------------
# If pattern does not exist
#    Add line
# elsif line does not match
#    replace with line
# So pattern must be anchored and match whole line
#----------------------------------------------------------
  vars:
    # Create a regexp string for line with whitespace replaced by \s+
    "eline" string => escape("$(line)");
    "rline" string => execresult("/bin/echo -e \"$(eline)\c\" | /usr/bin/perl 
-pe 's/\s+/\\s+/g'", "useshell");

  classes:
     # Check for missing pattern, or incorrect line
     "missing" not => regline("$(pattern)","$(edit.filename)");
     "wrong"   not => regline("$(rline)",  "$(edit.filename)"),
           comment => "rline : $(rline)";

  insert_lines:
     missing::
        "$(line)";

  replace_patterns:
     wrong.!missing::
     "$(pattern)"  replace_with => value("$(line)");
        "Fixed in $(edit.filename) : $(line)";
}

}

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

Reply via email to