[chef] how could I use knife programmatically in a ruby script?


Chronological Thread 
  • From: Bryan Berry < >
  • To:
  • Subject: [chef] how could I use knife programmatically in a ruby script?
  • Date: Wed, 23 Nov 2011 18:14:11 +0100

https://gist.github.com/1389228


Instead of shelling out to knife each time, could I just require 'Chef', initialize a few values, and work from there?

I might be able to figure it out by looking thru the chef source code, but I am lazy, so I thought I would ask first ;)

#!/usr/bin/env ruby

servers = %x[ knife search node "name:*econ*" -i].split
servers = servers.slice(3, servers.length)

servers.each do |s|
  run_list_str = %[knife node show #{s} -r -f json]
  run_list = JSON.parse(s_run_list)
  if not run_list.include? "role[base]"
    %x[knife node run_list add #{s} "role[base]"]
  end
  if not run_list.include? "role[int0]" or not run_list.include? "role[ext0]"
    if s =~ /^ext.*$/
      %x[knife node run_list add #{s} "role[ext0]"]
    else
      %x[knife node run_list add #{s} "role[int0]"]
    end
  end
  if not run_list.include? "role[econ]"
    %x[knife node run_list add #{s} "role[econ]"]
  end
  %x[ knife ssh name:${s} "chef-client" -x root -i .chef/identity ]
end



Archive powered by MHonArc 2.6.16.

§