如何在Chef Search API中过滤结果

时间:2016-03-30 07:52:26

标签: rest search filter chef

Chef Node API与/ nodes / nodename一样,将返回完整的节点对象,如:

{
  "name": "node_name",
  "chef_environment": "_default",
  "run_list": [
    "recipe[recipe_name]"
  ]
  "json_class": "Chef::Node",
  "chef_type": "node",
  "automatic": { ... },
  "normal": { "tags": [ ] },
  "default": { },
  "override": { "cookbook1_name"=>{
                    "attribute1"=>"overridevalue"
              }}
}

我正在尝试使用Chef :: Search :: Query.search REST API来返回一些必需的列而不是完整的节点对象。例如:

Chef::Search::Query.new("server url").search(:node, '*:*', :filter_result => {'name' => ['name'] } )

它只能返回节点名称,但是当我尝试添加覆盖数据时,它总是返回null ...

Chef::Search::Query.new("server url").search(:node, '*:*', :filter_result => {'name' => ['name'], 'override' => ['override'] } )

似乎filter_result可以用于像string或array这样的类型,但是override是一个json对象,如何在结果中包含它?

谢谢!

1 个答案:

答案 0 :(得分:1)

et filter_result中的关键路径是针对合并的属性表单运行的。你会想要'attribute1' => ['cookbook1_name', 'attribute1']

之类的东西

编辑:

直接举例:

$ knife node show foo -z -l -Fj
{
  "name": "foo",
  "chef_environment": "_default",
  "run_list": [

]
,
  "normal": {

  },
  "default": {

  },
  "override": {
    "key1": {
      "key2": "value"
    }
  },
  "automatic": {

  }
}
$ knife exec -z -E 'puts search(:node, "*:*").inspect'
[#<Chef::Node:0x007f89e3ee97f0 @chef_server_rest=nil, @name="foo", @chef_environment="_default", @primary_runlist=#<Chef::RunList:0x007f89e3ee9728 @run_list_items=[]>, @override_runlist=#<Chef::RunList:0x007f89e3ee9660 @run_list_items=[]>, @policy_name=nil, @policy_group=nil, @attributes=#<Chef::Node::Attribute @default={}, @env_default={}, @role_default={}, @force_default={}, @normal={}, @override={"key1"=>{"key2"=>"value"}}, @role_override={}, @env_override={}, @force_override={}, @automatic={}, @merged_attributes=nil, @properties=nil>, @run_state={}>]
$ knife exec -z -E 'puts search(:node, "*:*", filter_result: {myval: %w{key1 key2}}).inspect'
[{"myval"=>"value"}]