Michael Potter <mega...@gmail.com> writes:

> I believe the problem is this expression:
>
> "^(smtp_host)\s+.*$" replace_with => value("$(match.1) $(smtp)"),
>
> Because you use a .* after the whitespace, the regex will continue to match
> after any replacement occurs.
>
> Try using a negative lookahead assertion:
>
> "^(smtp_host)\s+(?!$(smtp))$"
>
> This will ensure the pattern only matches when smtp_host is *not* followed
> by $(smtp), so after the first replacement occurs, the regex will no longer
> match and will be convergent.

Great, except I have a strange behaviour, when nothing should be change,
there is a change:

#v+
m...@home:~/ $ rm -f /tmp/main.cf
removed `/tmp/main.cf'

m...@home:~/ $ /usr/sbin/cf-agent -K
R: I add the 'smtp_host smtp.example.net' line

m...@home:~/ $ /usr/sbin/cf-agent -K
R: I do not replace any missconfigured smtp_host line
R: I replace some missconfigured smtp_host line

m...@home:~/ $ perl -pi -e 's/\.net/.org/' /tmp/main.cf
m...@home:~/ $ /usr/sbin/cf-agent -K
R: I replace some missconfigured smtp_host line

m...@home:~/ $ /usr/sbin/cf-agent -K
R: I do not replace any missconfigured smtp_host line
R: I replace some missconfigured smtp_host line
#v-

#v+
cf3       = = = = = = = = = = = = = = = = = = = = = = = = = = = =
cf3       replace_patterns in bundle fix_smtp_host
cf3       = = = = = = = = = = = = = = = = = = = = = = = = = = = =
cf3 
cf3 
cf3     .........................................................
cf3     Promise handle: 
cf3     Promise made by: ^(smtp_host)\s+(?!smtp.example.net$).*$
cf3 
cf3     Comment:  Replace missconfigured smtp_host
cf3     .........................................................
cf3 
cf3  -> Looking at pattern ^(smtp_host)\s+(?!smtp.example.net$).*$
cf3  -> Replaced pattern "^(smtp_host)\s+(?!smtp.example.net$).*$" in 
/tmp/main.cf
cf3  ?> defining promise result class SomeSMTPHostReplaced
cf3  -> << (1)"smtp_host smtp.example.net"
cf3  -> >> (1)"smtp_host smtp.example.net"
cf3  -> No pattern "^(smtp_host)\s+(?!smtp.example.net$).*$" in /tmp/main.cf
cf3  ?> defining promise result class NoSMTPHostReplaced

#v-

With the following configuration:

#v+
body common control
{

    bundlesequence => { "filetest" };
    version => "0.0.1";
}

bundle agent filetest
{

    vars:

        "smtp" string => "smtp.example.net";

    files:

        "/tmp/main.cf"
            create => "true",
            edit_line => fix_smtp_host2("$(smtp)"),
            comment => "smpt_host must be $(smtp)";


    reports:

        SMTPHostOK::
            "The smtp_host line is already configured";

        SMTPHostFixed::
            "I add the 'smtp_host $(smtp)' line";

        NoSMTPHostReplaced::
            "I do not replace any missconfigured smtp_host line";

        SomeSMTPHostReplaced::
            "I replace some missconfigured smtp_host line";
}

bundle edit_line fix_smtp_host2(smtp)
{
    classes:

        "HasSMTPhost" expression => regline("^smtp_host.*", "$(edit.filename)");

    insert_lines:

        !HasSMTPhost::
            "smtp_host $(smtp)",
                classes => if_ok("SMTPHostOK", "SMTPHostFixed"),
                comment => "Add smtp_host if none";

    replace_patterns:

        HasSMTPhost::
            "^(smtp_host)\s+(?!$(smtp)$).*$" replace_with => value("$(match.1) 
$(smtp)"),
                classes => if_ok("NoSMTPHostReplaced", "SomeSMTPHostReplaced"),
                comment => "Replace missconfigured smtp_host";

}

body replace_with value(v)
{
    replace_value => "$(v)";
    occurrences => "all";
}

body classes if_ok(notchanged, changed)
{
    promise_kept => { "$(notchanged)" };
    promise_repaired => { "$(changed)" };
}
#v-

Regards.
-- 
Daniel Dehennin
Récupérer ma clef GPG:
gpg --keyserver pgp.mit.edu --recv-keys 0x6A2540D1

Attachment: pgpYjfH4KlCZO.pgp
Description: PGP signature

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

Reply via email to