Calling knife inside a chef recipe ? or a better solution


#1

Hi,

Here is my scenario, i have list of servers in my clusterA , i want to
provide that list of servers to a script ,

the script will then read this list( of servers) and will do some action
on a file, basically add/remove entires of those hosts in the file.

How can i achieve this, i can create this list dynamically using the knife
search query from a recipe and then put the output in a file , but i have
to have knife installed on all nodes then correct ?

Or i can run this knife query from outside on the chef server put the
output in the cookbook file folder , that also is a process breaker as i
have to run it on the server first then do the other stuff .

Is there a better way to handle this scenario , Please advise.

Thanks
Manoj


#2

knife search is just a wrapper around the search API endpoint, which you can use directly within recipe code via http://docs.opscode.com/essentials_search.html#nodes

–Noah

On Feb 10, 2014, at 3:03 PM, Manoj Thakkar manoj.thakkar@gmail.com wrote:

Hi,

Here is my scenario, i have list of servers in my clusterA , i want to provide that list of servers to a script ,

the script will then read this list( of servers) and will do some action on a file, basically add/remove entires of those hosts in the file.

How can i achieve this, i can create this list dynamically using the knife search query from a recipe and then put the output in a file , but i have to have knife installed on all nodes then correct ?

Or i can run this knife query from outside on the chef server put the output in the cookbook file folder , that also is a process breaker as i have to run it on the server first then do the other stuff .

Is there a better way to handle this scenario , Please advise.

Thanks
Manoj


#3

you can use the search method in recipe to get the list of hosts (and save
it in a file if you want), and pass it to the consumer script. also any
script with read access to to the chef client can search chef server and
obtain the list independently .

following is sample ruby code

require 'chef’
Chef::Config.from_file '/etc/chef/client.rb’
q = Chef::Search::Query.new
q.search(:node, ‘:’)

cheers
ranjib

On Mon, Feb 10, 2014 at 3:03 PM, Manoj Thakkar manoj.thakkar@gmail.comwrote:

Hi,

Here is my scenario, i have list of servers in my clusterA , i want to
provide that list of servers to a script ,

the script will then read this list( of servers) and will do some action
on a file, basically add/remove entires of those hosts in the file.

How can i achieve this, i can create this list dynamically using the knife
search query from a recipe and then put the output in a file , but i have
to have knife installed on all nodes then correct ?

Or i can run this knife query from outside on the chef server put the
output in the cookbook file folder , that also is a process breaker as i
have to run it on the server first then do the other stuff .

Is there a better way to handle this scenario , Please advise.

Thanks
Manoj