How to get only the results of 'knife search' commands

4k views Asked by At

When we run knife search command, it is listing the number of items found. I don't want the number of items, instead i need only the results. How to get it. Example to say, I would like to get some return code or nil value for the below command.

$ knife search node "name:MyLinux AND role:java"
0 items found
2

There are 2 answers

1
Tensibai On BEST ANSWER

You may use knife exec for this and use the exitmethod of ruby.

Example (even if it's try to undertand what you wish to accomplish with so few details/context):

knife exec -E "exit nodes.find('name:MyLinux AND role:java').count > 0"; echo $?

Which will return 0 if nothing is found and 1 if any number of nodes are found.

If you wish to do something to the nodes (like adding a recipe to the run_list):

knife exec -E "nodes.find('name:MyLinux AND role:java') { |n| n.run_list << 'cookbook::recipe'; n.save }"
0
ilvidel On

The "X items found" is written to stderr, so it might be easier to just redirect it:

$ knife search node "name:MyLinux AND role:java" 2> /dev/null