Prototype: hostrange(prefix, range)

Return type: boolean

Description: Returns whether the unqualified name of the current host lies in the range of enumerated hostnames specified with prefix.

This is a pattern matching function for non-regular (enumerated) expressions. The range specification is in the format A-B (using a minus character -) where A and B are decimal integers, optionally prefixed with zeroes (e.g. 01). The unqualified name of the current host used in this function is the same as the contents of the sys.uqhost variable. The function is using integer comparison on range and the last numeric part of the unqualified host name and string comparison of prefix (lowercase) with the part of the unqualified host name until the last numeric part.

Arguments:

  • prefix: string - Hostname prefix - in the range: .*
  • range: string - Enumerated range - in the range: .*

Example:

code
bundle agent main
{
  vars:

      "range" string => "1-32";
      "hostname_f" string => execresult( "hostname -f", useshell);
      "hostname_s" string => execresult( "hostname -s", useshell);
      "hostname" string => execresult( "hostname", useshell);

  classes:

      "hostgroup_alpha_no_leading_zeros" expression => hostrange("host", $(range) );
      "hostgroup_alpha_leading_zeros"    expression => hostrange("host", "00$(range)" );
      "hostgroup_alpha_UPPERCASE_prefix" expression => hostrange("HOST", "0$(range)" );

  reports:
      "sys.fqhost = '$(sys.fqhost)'";
      "sys.uqhost = '$(sys.uqhost)'";
      "hostname -f = '$(hostname_f)'";
      "hostname -s = '$(hostname_s)'";
      "hostname = '$(hostname)'";

    hostgroup_alpha_no_leading_zeros::

      "This host is within the alpha host group range (host$(range))";

    hostgroup_alpha_leading_zeros::
      "This host is within the alpha host group range (host00$(range)) (NOTE: Leading zeros and prefix capitalization is insignificant)";

    hostgroup_alpha_UPPERCASE_prefix::
      "This host is within the alpha host group range (HOST0$(range)) (NOTE: Leading zeros and prefix capitalization is insignificant)";
}

This policy can be found in /var/cfengine/share/doc/examples/hostrange.cf and downloaded directly from github.

Example Output:

code
R: sys.fqhost = 'host001.example.com'
R: sys.uqhost = 'host001'
R: hostname -f = 'HOST001.example.com'
R: hostname -s = 'HOST001'
R: hostname = 'HOST001'
R: This host is within the alpha host group range (host1-32)
R: This host is within the alpha host group range (host001-32) (NOTE: Leading zeros and prefix capitalization is insignificant)
R: This host is within the alpha host group range (HOST01-32) (NOTE: Leading zeros and prefix capitalization is insignificant)

This policy can be found in /var/cfengine/share/doc/examples/hostrange.cf and downloaded directly from github.