Thursday, August 20, 2015

Knife Search Negative Query

Are you trying to look for Chef Nodes that don't include a particular result? Do you need to avoid pulling back nodes with specific values but need something more complex than NOT? Fear not, the solution exists! It is not very well documented that I can find so here goes.

I did find a couple of references to what I was looking for in negative queries, which I've linked below. It appears this request was fixed, but I couldn't find the merge with the details of what/how. You have to look very careful on the Chef.io page for knife search. If you look carefully under the "Fuzzy Matching" portion in the "About Operators" section you can find a quick reference to the use of AND and NOT in the same search. Their example is "knife search sample "id:foo AND -id:bar""

Anyway, on to the examples.

Search for any instance that is linux and not in the role [webserver]
knife search node -q "os:linux AND -roles:webserver" -i

If using psearch instead of search
knife psearch node "(NOT (os:linux OR roles:webserver)

Note: These are not the same logical results, it is only to show how to use the NOT syntax with the AND operator.


https://tickets.opscode.com/browse/CHEF-1821
https://docs.chef.io/knife_search.html#about-patterns