Forum: CFEngine Help
Subject: Re: set_config_values() does different things depending on original
value of config variable in file
Author: nickanderson
Link to topic: https://cfengine.com/forum/read.php?3,26379,26381#msg-26381
Sorry I don't think I did a good job explaining.
I went ahead and tested the fix. It appears to work. Here is a standalone file
that can be used to test
You will either need to define your own version of config_variable_values or
patch it in the stdlib.
Standalone Example
# Usage run agent with setup, reproduce and show_fix classes
# cf-agent -KIf ./test_config_values.cf -D setup
# cf-agent -KIf ./test_config_values.cf -D reproduce
# cf-agent -KIf ./test_config_values.cf -D show_fix
body common control {
bundlesequence => {
"main",
};
inputs => {
"cfengine_stdlib.cf",
};
}
bundle agent main {
methods:
setup::
"any" usebundle => create_file;
show_fix|reproduce::
"any" usebundle => fix_file;
}
bundle agent create_file {
vars:
"value" string => "2,1";
files:
"/tmp/testfile"
create => "true",
edit_line => myset_config_values("create_file.value"),
edit_defaults => empty;
}
bundle agent fix_file {
vars:
"value" string => "2";
files:
reproduce::
"/tmp/testfile"
edit_line => set_config_values("fix_file.value");
show_fix::
"/tmp/testfile"
edit_line => myset_config_values("fix_file.value");
}
bundle edit_line myset_config_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 first.
# Adds a new line if none exists.
# The argument is the fully-qualified name of an associative array containing
v="rhs"
{
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+(?!$($(v)[$(index)])$).*|# ?$(index)\s+.*)$"
replace_with => value("$(index) $($(v)[$(index)])"),
classes => always("replace_attempted_$(cindex[$(index)])");
insert_lines:
"$(index) $($(v)[$(index)])"
ifvarclass => "replace_attempted_$(cindex[$(index)])";
}
Example Run
cmdln@ThinkPad-W520:~/.cfagent/inputs $ cf-agent -KIf ./test_config_values.cf
-D setup^C
cmdln@ThinkPad-W520:~/.cfagent/inputs $ cat /tmp/testfile
Protocol 2,1
cmdln@ThinkPad-W520:~/.cfagent/inputs $ cf-agent -KIf ./test_config_values.cf
-D reproduce
-> Edited file /tmp/testfile
cmdln@ThinkPad-W520:~/.cfagent/inputs $ cat /tmp/testfile
Protocol 2,1
Protocol 2
cmdln@ThinkPad-W520:~/.cfagent/inputs $ cf-agent -KIf ./test_config_values.cf
-D setup
-> Edited file /tmp/testfile
cmdln@ThinkPad-W520:~/.cfagent/inputs $ cat /tmp/testfile
Protocol 2,1
cmdln@ThinkPad-W520:~/.cfagent/inputs $ cf-agent -KIf ./test_config_values.cf
-D show_fix
-> Edited file /tmp/testfile
cmdln@ThinkPad-W520:~/.cfagent/inputs $ cat /tmp/testfile
Protocol 2
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine