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 ./test_regextract.cf
R: eth0, 192_168_1_1
# cat ./test_regextract.cf
body common control
{
    any::
        bundlesequence  => { test };
}

bundle agent test {

  vars:

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

  classes:

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

  reports:

    cfengine_3::
      "$(interface), $(ip)";
}

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