regline

Table of Contents

Prototype: regline(regex, filename)

Return type: boolean

Description: Returns whether the anchored regular expression regex matches a line in file filename.

Note that regex must match an entire line of the file in order to give a true result.

Arguments:

  • regex: regular expression, in the range: .*
  • filename: string, in the range: .*

Examples:

This example shows a way to determine if IPV4 forwarding is enabled or not.

bundle agent main
{
  vars:

    linux::

      "file" string => "/proc/sys/net/ipv4/ip_forward";
      "reg_enabled" string => "^1$";
      "reg_disabled" string => "^0$";

  classes:

    linux::

      "ipv4_forwarding_enabled" -> { "SecOps" }
        expression => regline( $(reg_enabled) , $(file) ),
        comment => "We want to know if ip forwarding is enabled because it is a
                    potential security issue.";

      "ipv4_forwarding_disabled" -> { "SecOps" }
        expression => regline( $(reg_disabled) , $(file) );

  reports:

    ipv4_forwarding_enabled::
      "I found that IPv4 forwarding is enabled!";

    ipv4_forwarding_disabled::
      "I found that IPv4 forwarding is disabled.";
}
R: I found that IPv4 forwarding is disabled.

For edit_line applications it may be useful to set a class for detecting the presence of a string that does not exactly match one being inserted. For example:

bundle edit_line upgrade_cfexecd
{
  classes:

    # Check there is not already a crontab line, not identical to
    # the one proposed below...

    "exec_fix"
      not => regline(".*cf-execd.*","$(edit.filename)"),
      scope => "bundle"; # Unless you need the class outside of the bundle you
                         # should always scope it to the bundle. This can
                         # prevent issues when the bundle is used multiple
                         # times, and the classes promise is expected to be
                         # re-evaluated. If the class is namespace scoped the
                         # class will be available to other bundles and persist
                         # until it is explicitly canceled or until the end of
                         # the agent run.

  insert_lines:

    exec_fix::

     "0,5,10,15,20,25,30,35,40,45,50,55 * * * * /var/cfengine/bin/cf-execd -F";

  reports:

    exec_fix::

     "Added a 5 minute schedule to crontabs";
}

See also: regcmp(), strcmp()