“cannot load such file — chef/rest” when “require

2019-08-18 22:17发布

问题:

Trying to write first program to access chef server via API. Following example program on https://docs.chef.io/api_chef_server.html#examples there supposed to be two lines of code to include chef::rest library.

require 'chef'
require 'chef/rest'

but the code failed with error

Traceback (most recent call last):
        2: from ./rest.rb:4:in `<main>'
        1: from /opt/chef/embedded/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/opt/chef/embedded/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- chef/rest (LoadError)

Chef client version: 14.12.9-1 OS Version: Ubuntu 16.04 LTS

Any missing steps?

回答1:

you did not share whether you use chef libraries as a part of your cookbooks or as a part of another program that depended on the chef libraries.

if you are at the second case, i.e. outside of a cookbook context, then make sure that you are using Gemfile to manage your chef libraries requirement, then execute your program using bundler.

as you can see, Chef::Rest is deprecated

The Chef::REST class will be removed. Chef::REST was deprecated in 12.7.2, and will be removed in Chef 13.

If writing code designed to be run internally to Chef, for example in a cookbook or a knife plugin, transition to using Chef::ServerAPI. In most cases this is as simple as creating a Chef::ServerAPI instance rather than a Chef::REST one.

If writing code to interact with a Chef Server from other code, move to the chef-api gem.

so i assume that you are using an old chef version, so for the remedy i will be using chef version 12.6:

$ cat Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

gem 'chef', '12.6.0'

$ cat foo.rb
require 'chef'
require 'chef/rest'

$ bundle install
...
Bundle complete! 1 Gemfile dependency, 46 gems now installed.

$ bundle exec ruby foo.rb


标签: ruby rest chef