首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安装了cfEngine3类的if包

安装了cfEngine3类的if包
EN

Stack Overflow用户
提问于 2017-10-20 08:28:12
回答 1查看 217关注 0票数 0

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

背景:我只想在安装了一个包(在特定版本中是可选的)时才触发文件修改。

不幸的是,我的(示例)代码不起作用:

代码语言:javascript
复制
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 - set variable if a specific package version is installed,但我想问一下编码提示或解决方案:-)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-20 14:29:18

我对你的政策做了一点调整,并提供了相应的评论。我认为您的主要问题是,您期望返回的packagesmatching()数据的索引按包名索引,而不是数字id。

代码语言:javascript
复制
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])" );
}

这一产出的结果:

代码语言:javascript
复制
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

这能回答你的问题吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46845164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档