Forum: CFEngine Help
Subject: Reuse edit_line
Author: sandro...@gmail.com
Link to topic: https://cfengine.com/forum/read.php?3,25568,25568#msg-25568

Hi, it's my first question in this forum, I've been searching around the web 
and in this forum for a similar question, but I couldn't find any related 
questions.

I'm using a implementation of edit_line set_variable_values, found this one 
more recently: 
https://github.com/zzamboni/cfengine-copbl/blob/master/cfengine_stdlib.cf#L170
So I have two promises that uses the same bundle edit_line (i renamed 
set_values), the problem is that i have the same values in one file that I have 
in another, when i remove those repeated values from the second promises to be 
executed, nothing happens, only repair the file if I remove a value that its 
distinct from the another one, e.g.:

file01.conf
user=root
password=default

file02.conf
user=root
password=default
engine=MyISAM

In the example above, if I remove engine from file02.conf, it will be repaired, 
if I remove user=root from file02.conf, promise is kept.
Can I reuse edit_line set_values multiple times from different promises in this 
case?

Here's a sample code:

bundle agent config_mybkp {
    vars:       
        "mybackup_file" string => "/path/mybackup.conf";
        "mycatalog_file" string => "/path/mycatalog.conf";

        # mybackup.conf config values
        "mybackup"            string => "default_schema";
        "mybackup"              string => "localhost";
        "mybackup"             string => "mysql";
        "mybackup"         string => "1000";
        "mybackup"          string => "20";
        "mybackup"              string => "root";
        "mybackup"          string => "default";
        
        # mycatalog.conf config values
        "mycatalog"     string => "default_schema";
        "mycatalog"       string => "localhost";
        "mycatalog"     string => "MyISAM";
        "mycatalog"  string => "30";
        "mycatalog"       string => "root";
        "mycatalog"   string => "default";
    
    files:
        "$(config_mybkp.mycatalog_file)"
            perms => change_owner("0600","root","root"),
            create => "true",
            edit_line => set_values("config_mybkp.mycatalog");


        "$(config_mybkp.mybackup_file)"
            perms => change_owner("0600","root","root"),
            create => "true",
            edit_line => set_values("config_mybkp.mybackup");


}

bundle edit_line set_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)])";

}

In the real scenario, when i remove "host=localhost" from file mybackup.conf, 
the promise is kept, because it's already on the mycatalog.conf file, but if i 
remove "retention=30" from the same file, promise is repaired, going a 
furthermore, if a remove any values from mycatalog.conf, it works.

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

Reply via email to