cfEngine3-类(如果已安装软件包) [英] cfEngine3 - class if package is installed

查看:93
本文介绍了cfEngine3-类(如果已安装软件包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果安装了软件包,如何设置类?

how can i set a class if a package is installed ?

背景:仅当安装了软件包(特定版本中为可选)时,我才想触发文件修改.

Background : i want to trigger a file modification only if a package is installed (optional in a specific version).

很不幸,我的(示例)代码无法正常工作:

My (example) code unfortunately doesn't work :

vars:
    "cfengine_rpm" data => packagesmatching("cfengine-nova", ".*", ".*", ".*");
    "cfengine_rpm_installed" slist => getindices(cfengine_rpm);

classes:
    "cfengine_installed" expression => some("cfengine", cfengine_rpm_installed);

reports:
    cfengine_installed::
        "cfEngine is installed ";
        # Bonus :-)
        "cfEngine Version : $(cfengine_rpm[$(cfengine_rpm_installed)][version])";

附录:此问题类似于 CFEngine-如果安装了特定的软件包版本,则设置变量,但我想询问编码的提示或解决方案:-)

Addendum : this question is similar to CFEngine - set variable if a specific package version is installed but I would like to ask for coded hints or solutions :-)

推荐答案

我对您的政策做了一些调整,并提供了相应的注释.我认为您的主要问题是您期望返回的packagesmatching()数据的索引将按包名称而不是数字ID进行索引.

I tweaked your policy a bit and provided comments in line. I think your main issue was that you were expecting the index of the returned packagesmatching() data to be indexed by package name, instead of a numeric id.

bundle agent main
{
vars:

    # Return data from cfengines internal cache about any packages matching the
    # name cfengine-nova
    "p"
      data => packagesmatching("cfengine.*", ".*", ".*", ".*");

    # Get the index (list of keys) from this data structure for iteration

    # Each value in the list is a number which is the position of the JSON
    # object in the data returned from packagesmatching(). For example, if
    # cfengine-nova-hub is the only package found to be installed that matches
    # then the data structure returend to p will look like the following
    # snippet. Note it's the 0th element inside the array ([]).
    #
    # [
    #   {
    #      "arch":"x86_64",
    #      "method":"dpkg",
    #      "name":"cfenigne-nova-hub",
    #      "version":"3.10.1-1"
    #   }
    # ]

    "i" slist => getindices(p);
    "c" slist => classesmatching( ".*", "defined_from=$(this.bundle)");

classes:

    # Iterate over the packages found, if one of their names matches
    # cfengine-nova.* then define the class cfengine_installed.

    "cfengine_installed"
      expression => regcmp( "cfengine.*", "$(p[$(i)][name])" ),
      meta => { "defined_from=$(this.bundle)" };

reports:

    # Emit the version of cfengine from the internal sys var
    "CFEngine $(sys.cf_version)";

    # Iterate over the index (i) of the data returned from packagesmatching
    # cfengine-nova (p) and print the name of each package.

    "CFEngine cached knowledge of $(p[$(i)][name]) $(p[$(i)][version])";

    "Found the class '$(c)' defined from $(this.bundle)";

    cfengine_installed::

        "CFEngine is installed ";

        # Bonus :-)

        # In case you had multiuple packages returned, you might want to make
        # this more strict, or it will emit the version of each package found
        # by packagesmatching.

        "CFEngine Package Version : $(p[$(i)][version])"
          if => strcmp( "cfengine-nova-hub", "$(p[$(i)][name])" );
}

输出结果:

R: CFEngine 3.10.1
R: CFEngine cached knowledge of cfengine-nova-hub 3.10.1-1
R: Found the class 'cfengine_installed' defined from main
R: CFEngine is installed 
R: CFEngine Package Version : 3.10.1-1

这能回答您的问题吗?

这篇关于cfEngine3-类(如果已安装软件包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆