Forum: Cfengine Help
Subject: replace_patterns bug?
Author: babudro
Link to topic: https://cfengine.com/forum/read.php?3,19347,19347#msg-19347
Hello Cfengineers.
Like Michael Potter's post on 9 November, I am also running into some strange
behaviour with replace_patterns. I wonder if someone can spot if I am doing
something wrong so I don't file an erroneous bug report.
All I am trying to do is replace a parameter line in a file. I created this
rule to toggle the setting:
bundle edit_line yum_plugins(x) {
replace_patterns:
"^enabled\s*=\s*[^$(x)]" replace_with => value("enabled = $(x)");
}
The idea is that if I pass x=0 then it searches for "enabled" not equal to zero
and, if it finds a match, changes it to be zero.
When I run this promise, I get this:
-> Promised replacement "enabled = 00000000000000000000" on line "enabled = 1"
for pattern "^enabled\s*=\s*[^0]" is not convergent while editing
/etc/yum/pluginconf.d/rhnplugin.conf
Because the regular expression "^enabled\s*=\s*[^0]" still matches the
replacement string "enabled = 00000000000000000000"
Promise (version 8) belongs to bundle 'yum_plugins' in file './yum.cf' near
line 215
-> Promised replacement "enabled = 00000000000000000000" on line "enabled = 1"
for pattern "^enabled\s*=\s*[^0]" is not convergent while editing
/etc/yum/pluginconf.d/rhnplugin.conf
How weird is that? I understand how "enabled = 00" does not match the pattern
"^enabled\s*=\s*[^0]" since I only have a single zero, but how did it ever get
that second (and third and fourth &c.) zero? It should have made the first
replacement and then stopped since the match is no longer true.
If I put a dollar-sign at the end of the match string, it works. But I
shouldn't have to do that.
Am I missing something?
Here is a self-contained policy file to demonstrate this apparent bug:
body common control {
bundlesequence => { "yum" };
}
bundle agent yum {
files:
redhat::
"/tmp/test.conf"
comment => "Demonstrate pattern bug",
create => "false",
edit_line => yum_plugins("0");
reports:
plugins_toggled::
"Toggled 'enabled' flag in test.conf.";
}
bundle edit_line yum_plugins(x) {
replace_patterns:
"^enabled\s*=\s*[^$(x)]" replace_with => value("enabled = $(x)");
}
body replace_with value(x) {
replace_value => "$(x)";
occurrences => "all";
}
# vim:ts=2
Create the file /tmp/test.conf with just this one line:
enabled = 1
Now watch it explode:
$ cf-agent -f ./pattern_bug.cf
-> Promised replacement "enabled = 00000000000000000000" on line "enabled = 1"
for pattern "^enabled\s*=\s*[^0]" is not convergent while editing /tmp/test.conf
I: Made in version 'not specified' of './pattern_bug.cf' near line 18
Because the regular expression "^enabled\s*=\s*[^0]" still matches the
replacement string "enabled = 00000000000000000000"
Promise (version not specified) belongs to bundle 'yum_plugins' in file
'./pattern_bug.cf' near line 18
-> Promised replacement "enabled = 00000000000000000000" on line "enabled = 1"
for pattern "^enabled\s*=\s*[^0]" is not convergent while editing /tmp/test.conf
I: Made in version 'not specified' of './pattern_bug.cf' near line 18
Because the regular expression "^enabled\s*=\s*[^0]" still matches the
replacement string "enabled = 00000000000000000000"
Promise (version not specified) belongs to bundle 'yum_plugins' in file
'./pattern_bug.cf' near line 18
-> Promised replacement "enabled = 00000000000000000000" on line "enabled = 1"
for pattern "^enabled\s*=\s*[^0]" is not convergent while editing /tmp/test.conf
I: Made in version 'not specified' of './pattern_bug.cf' near line 18
Because the regular expression "^enabled\s*=\s*[^0]" still matches the
replacement string "enabled = 00000000000000000000"
Promise (version not specified) belongs to bundle 'yum_plugins' in file
'./pattern_bug.cf' near line 18
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine