I'm trying to set up a couple of servers using knife-solo, and I need to use a basic search in a recipe so I can know which servers are implementing a particular role. This seems like the most basic purpose for using search.
I am using chef-solo-search, however I'm not able to find nodes by role as (it seems to be) described in the docs; I am totally stumped. I can hack a solution, but it seems like I am doing something wrong. I built a simplified example to demonstrate my problem.
This does not work...
I created a dummy role roles/test_role.json
{
"name": "test_role",
"default_attributes": {},
"json_class": "Chef::Role",
"run_list": [],
"description": "",
"override_attributes": {}
}
My basic recipe to test searching for nodes with "test_role" role: site-cookbooks/nodesearch/recipes/default.rb
# required for search with knife-solo
include_recipe "chef-solo-search"
# this is now the test code for chef-solo-search shows to search for nodes that implement a role.
# see https://github.com/edelight/chef-solo-search/blob/master/tests/test_search.rb ~ line 208
nodes = search(:node, "role:test_role")
# This creates a line for each node found in the search...
search_content = nodes.map {|node| "id: #{node['id']} run_list: #{node['run_list']}\n"}.join
# And writes it to a file
file "/var/test_role_nodes.txt" do
content search_content
action :create
end
And I have created two nodes:
nodes/node1.json
{
"id": "node1",
"run_list": ["recipe[nodesearch]"]
}
nodes/node2.json
{
"id": "node2",
"run_list": ["role[test_role]"]
}
When I "cook" node1 (which runs the nodesearch recipe,) I am expecting the search(:node, "role:test_role") search to find node2, but it returns nothing. I have tried moving the node definitions to the data_bags directory because the docs (in places) seem to imply this is necessary, and I have tried all sorts of shenanigans with solo.rb settings, etc. I believe I have set up chef-solo-search per the documented instructions, and I don't get any errors to indicate otherwise. Out of ideas.
But this works...
The only thing I have done that does work is this: rewriting the search recipe from:
nodes = search(:node, "role:test_role")
to this:
nodes = search(:node, "run_list:*role\\[test_role\\]*")
The second form actually gets me the desired results. It's an acceptable workaround, but it seems like a bit of a hack, and it leaves me wondering... either I must be doing something awfully wrong, or all the docs for knife-solo and chef-solo-search are wrong (which seems quite unlikely!)
Can anyone help explain why I can't get any search results using search(:node, "role:test_role") ??
(We've explicitly chosen not to use a chef server solution for a number of carefully-considered reasons.)