Ubuntu 14.10
12.5.1-1
I've got the following pseudo-code in an attributes file:
node.default[:thing][:stuff] = []
boxes = search(:node, "role:boxrole AND chef_environment:#{node.chef_environment}")
boxes.each do | box |
node.default[:thing][:stuff] << "#{box.name}"
end
In an environment with nodes of role boxrole this works fine. Also, in environments which have previously (but no longer) had nodes of role boxrole it works, though the search returns no results as expected.
However, in environments which have never had a node with role boxrole, I'm seeing the following error being returned by the search method:
================================================================================
192.168.16.240 Recipe Compile Error in /var/chef/cache/cookbooks/cookbook/attributes/recipe.rb
192.168.16.240 ================================================================================
192.168.16.240
192.168.16.240 NoMethodError
192.168.16.240 -------------
192.168.16.240 Undefined node attribute or method `search' on `node'
Am I doing something wrong, and this is expected behavior somehow, or have I uncovered a bug?
For the future, this isn't the right place for asking support questions. Please use the mailing list or IRC for those. As to your specific question, the search() DSL method is not supported in attribute files, only recipes. You can use the Chef::Search::Query class directly though, see https://github.com/chef/chef/blob/master/lib/chef/dsl/data_query.rb#L39 for how search() is actually implemented.
I don't think this should be closed as OT.
This code was moved here from a recipe where it was also exhibiting this behavior. Please note that it works fine in the two cases mentioned above, and fails in one specific scenario.
I just ran into the same problem in a new cookbook I was working on (I was trying to build a complex test case using kitchen-nodes)
Following on @coderanger's comment:
the
search()DSL method is not supported in attribute files, only recipes. You can use theChef::Search::Queryclass directly though
I was able to get search working in an attributes file like so:
class AttributeSearch
extend Chef::DSL::DataQuery
end
boxes = AttributeSearch.search(:node, "role:boxrole AND chef_environment:#{node.chef_environment}")
boxes.each do | box |
node.default[:thing][:stuff] << "#{box.name}"
end
Most helpful comment
I just ran into the same problem in a new cookbook I was working on (I was trying to build a complex test case using kitchen-nodes)
Following on @coderanger's comment:
I was able to get search working in an attributes file like so: