packagesmatching

Table of Contents

Prototype: packagesmatching(package_regex, version_regex, arch_regex, method_regex)

Return type: data

Description: Return a data container with the list of installed packages matching the parameters.

This function searches for the anchored regular expressions in the list of currently installed packages.

The return is a data container with a list of package descriptions, looking like this:

[
  {
    "name": "bfoobar",
    "version": "1",
    "arch": "besm6",
    "method": "printf"
  }
]

The following code extracts just the package names, then looks for some desired packages, and finally reports if they are installed.

body common control

{
      bundlesequence => { "missing_packages" };
}


bundle agent missing_packages
{
  vars:
    "desired" slist => { "mypackage1", "mypackage2" };

    "installed" data => packagesmatching(".*",".*",".*",".*");
    "installed_indices" slist => getindices(installed);
    "installed_name[$(installed_indices)]" string => "$(installed[$(installed_indices)][name])";
    "installed_names" slist => getvalues("installed_name");

    "missing_list" slist => difference(desired,installed_names);

  reports:
    "Missing packages = $(missing_list)";
    "Installed packages = $(installed_names)";
    "Desired packages = $(desired)";
}

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

Arguments:

  • package_regex: string, in the range: .*
  • version_regex: string, in the range: .*
  • arch_regex: string, in the range: .*
  • method_regex: string, in the range: .*

Example:

      "all_packages" data => packagesmatching(".*", ".*", ".*", ".*");

History: Introduced in CFEngine 3.6

See also: packageupdatesmatching().