Cfengine Goodies

To content | To menu | To search

Tag - regex

Entries feed - Comments feed

Monday 4 February 2013

CFEngine 3 :: delete non managed values

In etc_host sketch, a (regex) list of ips passed to delete_nonmanaged bundle to delete lines do not match ips or are not comments.

caller :

edit_line   => cfdc_etc_hosts:delete_nonmanaged("@(configure.ip)"),

delete_nonmanaged bundle:

bundle edit_line delete_nonmanaged(ips)
      "regex" string => join ("|", "ips"),
      comment => "Or the escaped ips together so we can delete all others.";

      # This is the negative look ahead, and what would need tweaked
      # if you don't like the behavior. Specifically it's identifying
      # our list of ips followed by a space, as well as # comment or
      # localhost and deleting everything else

      comment => "Delete lines do not match our ips or are not comments";

Friday 11 January 2013

CFEngine 3 :: regextract

How to define variables after command extraction :

A command :

"/sbin/ip -4 -o address list"

, a PCRE filter :

"^[^:]*: ([^\s]+).*inet (\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}).*$"

and cfengine (interface) variables definition.

  • CFEngine 3 test:
# cf-agent -I -f ./
R: eth0, 192_168_1_1
# cat ./
body common control
        bundlesequence  => { test };

bundle agent test {


     string => execresult("/sbin/ip -4 -o address list", "noshell");
     slist  => splitstring( ${ifaces}, "$(const.endl)","100");
     string => canonify("$(device[1])");
     string => canonify("$(device[2])");


      expression => regextract("^[^:]*: ([^\s]+).*inet (\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}).*$", "$(ifacelist)", "device");


      "$(interface), $(ip)";

Becareful, the name is misleading, because PCRE and Perl each have capabilities not shared by the other.