gsandie online notebook

Chef 11 - Libraries and LWRP testing in chef-shell

In chef 11 you can no longer load up the chef-shell, load a cookbook and access the libs and LWRPS that it provides.

You need a few extra steps. Credit to this goes to @btmspox who put this in a CHEF ticket which I can’t current locate.

Testing libs in chef shell, for chef 11 and greater

Start chef shell:

$ chef-shell -c /path/to/chef-config.rb -z

All of the following can be run from chef shell, you don’t need to be in recipe mode:

Chef::Config[:cookbook_path] = "/path/to/your/cookbooks"
run_list = "recipe[partial_search]"
cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path])
cl.load_cookbooks
cookbook_collection = Chef::CookbookCollection.new(cl)
@events = Chef::EventDispatch::Dispatcher.new()
node.run_context = Chef::RunContext.new(node, cookbook_collection, @events)
node.run_list = Chef::RunList.new(run_list)
node.run_context.load(node.expand!)

You can now use partial search from the shell:

partial_search(:node, "chef_environment:production", :keys => { 'fqdn' => ['fqdn'] })