Gabriel
Gabriel

Reputation: 109

How to use knife search using variables

I've created a jenkins job that lets a user choose a chef environment and a chef role and then it should run a knife search on that environment and that role and to run chef-client on the resulted nodes.

So far, i've had another job that was letting user choose an environment and run chef-client on the machines which result from that query. the code was

#!/bin/bash
echo env=$Environment
cd /chef-repo
machines=$(knife search 'chef_environment:'$Environment  -i)
echo "The machines are: $machines"
for i in $machines; do
    echo "Updating node $i"
    ssh -tt lcsa@$i "sudo chef-client"
done

So to solve my problem, I've tried a query like

machines=$(knife search 'chef_environment:'$Environment AND 'role:'$Role  -i)

with the error:

ERROR: knife search failed: invalid search query: ''chef_environment:'test-devops AND 'role:'base'

or I tried:

machines=$(knife search "'chef_environment:'$Environment AND 'role:'$Role"  -i)

but i get the error:

ERROR: Chef::Exceptions::InvalidSearchQuery: Invalid search object type nil (NilClass), must be a String or Symbol.Usage: search(:node, QUERY[, OPTIONAL_ARGS])        `knife search environment QUERY (options)`

Could you please enlighten me?

Thank you, Gabriel

Upvotes: 0

Views: 1007

Answers (1)

zuazo
zuazo

Reputation: 5758

Sounds like you are using shell quotes incorrectly. Try with this:

machines=$(knife search "chef_environment:$Environment AND role:$Role"  -i)

Or for your first example:

machines=$(knife search "chef_environment:$Environment"  -i)

Upvotes: 2

Related Questions