replace_patterns

This promise refers to arbitrary text patterns in a file. The pattern is expressed as a PCRE regular expression.

       replace_patterns:

        "search pattern"

           replace_with => replace_body,
           ...;

In replace_patterns promises, the regular expression may match a line fragment, that is, it is unanchored.

    bundle edit_line upgrade_cfexecd
    {
      replace_patterns:

        "cfexecd" replace_with => value("cf-execd");
    }

    body replace_with value(x)  # defined in cfengine_stdlib.cf
    {
    replace_value => "$(x)";
    occurrences => "all";
    }

This is a straightforward search and replace function. Only the portion of the line that matches the pattern in the promise will be replaced; the remainder of the line will not be affected. You can also use PCRE look-behind and look-ahead patterns to restrict the lines upon which the pattern will match.


Attributes

replace_with

Type: body replace_with

occurrences

Description: Defines which occurrences should be replaced.

Using "first" is generally unwise, as it will change a different matching string each time the promise is executed, and may not "catch up" with whatever external action is altering the text the promise applies to.

Type: (menu option)

Allowed input range:

  • all

Replace all occurrence.

  • first

Replace only the first occurrence. Note: this is non-convergent.

Default value: all

Example:

     body replace_with example
     {
     occurrences => "first";        # Warning! Using "first" is non-convergent
     }

replace_value

Description: Value used to replace regular expression matches in search

Type: string

Allowed input range: .*

Example:

     body replace_with example(s)
     {
     replace_value => "$(s)";
     }