Include Rest Client in helper module in Chef


#1

Hi

I need to include res-client into my module in order to use RestClient::Resource.

I use the method in the module in a Chef recipe (ruby_block resource).

When my ruby_block resource tries to run the method in the module it output this error:

ERROR: cannot load such file – rest-client

The following is my module residing in libraries folder:

module SecretServer
def fetch_token
payload = { :username => “**", :password => "", :grant_type => "”}
uri = ***
request = RestClient::Resource.new(uri, :verify_ssl => false)
post_request = request.post payload, :content_type => ‘application/x-www-form-urlencoded’
token = JSON.parse(post_request)[“access_token”]
return token
end
end

require ‘rest-client’
Chef::Recipe.include(SecretServer)
Chef::Resource.include(SecretServer)

The following is the resource that calls the function in module:

ruby_block ‘parse data’ do
block do
res = fetch_token
puts res
end
end

This is just one of the several recipes in my cookbook. This recipe runs after the target node is almost ready and ‘bundle install’ has been run on the target node.

I also install rest-client in the target node. I tried each of following resource before my ruby_block resource:

chef_gem ‘rest-client’ do
action :install
end

gem_package ‘rest-client’ do
action :install
end

My question is how to include ‘rest-client’ and utilize it in Chef recipes?

Note: I need to disable ssl verification for the API call due to some reason, therefore I am not able to use Chef::HTTP since it does not offer that.


#2

Answered on StackOverflow https://stackoverflow.com/questions/51005697/require-rest-client-to-module-in-chef