Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nickanderson/25c7f6496e56828ad5fda0f8543a64ef to your computer and use it in GitHub Desktop.
Save nickanderson/25c7f6496e56828ad5fda0f8543a64ef to your computer and use it in GitHub Desktop.
Examples illustrating variablesmatching_as_data() in CFEEngine

variablesmatching_as_data() is useful for centralizing information from disparate sources. For example, deriving a list of package names by extracting the values from variables that match a specific name.

 bundle agent package_names_from_variablesmatching_as_data
 # @brief Example illustrating deriving a list of package names from variables named with a specific naming convention
{
  vars:
      "packages_baseline" slist => { "bash-completion", "emacs" };
      "packages_os" slist => { "httpd" };
      "packages_barf" slist => { "apache" };

      "_mypkgdata" data => variablesmatching_as_data( ".*\.packages_.*" );

      "_v" slist => getvalues( _mypkgdata );

  reports:
      '$(with)'
        with => storejson( _v );

}
bundle agent __main__
{
      methods: "finding_vars_by_name";
}
R: [
  "bash-completion",
  "emacs",
  "httpd",
  "apache"
]

Or, slightly better, a tag:

bundle agent package_names_from_variablesmatching_as_data
# @brief Example illustrating deriving a list of package names from variables having a specific tag
{
  vars:
      "packages_baseline"
        meta => { "desired_package_names=present" },
        slist => { "bash-completion", "emacs" };

      "packages_os"
        meta => { "desired_package_names=present" },
        slist => { "httpd" };

      "packages_barf"
        meta => { "desired_package_names=present" },
        slist => { "apache" };

      "_mypkgdata" data => variablesmatching_as_data( ".*", "desired_package_names=present" );

      "_v" slist => getvalues( _mypkgdata );

  reports:
      '$(with)'
        with => storejson( _v );

}
bundle agent __main__
{
      methods: "finding_vars_by_name";
}
R: [
  "bash-completion",
  "emacs",
  "httpd",
  "apache"
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment