Querying a Chef server through it's API


#1

Hello everyone,

I’m new to Chef so be gentle!

I’m trying to query a Chef server through it’s API and have developed the following code:-

require 'chef/config’
require 'chef/log’
require 'chef/rest’
chef_server_url = "http//chef.prod.practor.service"
client_name = "ace67"
signing_key_filename=File.dirname(FILE) + “/.chef/ace67.pem”

rest = Chef::REST.new(chef_server_url, client_name, signing_key_filename)
nodes = rest.get_rest("/nodes")

nodes.keys.each do |node_name|
puts "Node name: #{node_name}"
end

It’s a simple Ruby script to query the Chef server. through it’s API, for a list of nodes. However when I run the script, I get an error of TypeError: can’t dup NilClass. It’s occurring on the nodes = rest.get_rest("/nodes") line.

I’m running Chef version 11.16.2

I’d appreciate any assistance with this.

Many thanks

Angela Ebirim

Sent from iCloud


#2

Hi Angela,

It sounds like you might be getting a nil or empty response back from the
chef server. I would try running the same thing in a pry/irb session and
see what the return value looks like from rest.get_rest(’/nodes’).

For what it’s worth, you might also want to give ridley
https://github.com/RiotGames/ridley a try.

Thanks,
Brandon

On Fri, Jan 23, 2015 at 10:48 AM, ANGELA EBIRIM aebirim@icloud.com wrote:

Hello everyone,

I’m new to Chef so be gentle!

I’m trying to query a Chef server through it’s API and have developed the
following code:-

require 'chef/config’
require 'chef/log’
require 'chef/rest’
chef_server_url = "http//chef.prod.practor.service"
client_name = "ace67"
signing_key_filename=File.dirname(FILE) + “/.chef/ace67.pem”

rest = Chef::REST.new(chef_server_url, client_name, signing_key_filename)
nodes = rest.get_rest("/nodes")

nodes.keys.each do |node_name|
puts "Node name: #{node_name}"
end

It’s a simple Ruby script to query the Chef server. through it’s API, for
a list of nodes. However when I run the script, I get an error of
TypeError: can’t dup NilClass. It’s occurring on the nodes =
rest.get_rest("/nodes") line.

I’m running Chef version 11.16.2

I’d appreciate any assistance with this.

Many thanks

Angela Ebirim

Sent from iCloud


#3

Couple things:

  • You don’t have a colon after ‘http’
  • If you’re running Chef Server 12 or connecting to Hosted Enterprise
    Chef, the chef_server_url is going to be something like
    https://your-chef-server/organizations/yourorg – there is no endpoint
    ’/nodes’ at the root of the system.
  • Julian

On Fri, Jan 23, 2015 at 12:48 PM, ANGELA EBIRIM aebirim@icloud.com wrote:

Hello everyone,

I’m new to Chef so be gentle!

I’m trying to query a Chef server through it’s API and have developed the
following code:-

require 'chef/config’
require 'chef/log’
require 'chef/rest’
chef_server_url = "http//chef.prod.practor.service"
client_name = "ace67"
signing_key_filename=File.dirname(FILE) + “/.chef/ace67.pem”

rest = Chef::REST.new(chef_server_url, client_name, signing_key_filename)
nodes = rest.get_rest("/nodes")

nodes.keys.each do |node_name|
puts "Node name: #{node_name}"
end

It’s a simple Ruby script to query the Chef server. through it’s API, for a
list of nodes. However when I run the script, I get an error of TypeError:
can’t dup NilClass. It’s occurring on the nodes = rest.get_rest("/nodes")
line.

I’m running Chef version 11.16.2

I’d appreciate any assistance with this.

Many thanks

Angela Ebirim

Sent from iCloud


[ Julian C. Dunn jdunn@aquezada.com * Sorry, I’m ]
[ WWW: http://www.aquezada.com/staff/julian * only Web 1.0 ]
[ gopher://sdf.org/1/users/keymaker/ * compliant! ]
[ PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9 ]


#4

Hi Brandon,

I’ve been using Ridley to actually query a test Chef server and it worked fine (always the way!). However when I tried it with a dev Chef server received the following error https://github.com/RiotGames/ridley/issues/301. Apparently I discovered a bug that they have now flagged up as a potential feature. If you try to find a databag on a Chef server, using Ridley, you may receive a 301 error. Unfortunately you don’t get any information on what the new location for the databag should be!

Once I find a solution, I’ll post it here just in case someone else has the same problem.

Thanks for your help!

Regards
Sent from iCloud

On Jan 23, 2015, at 01:09 PM, Brandon Raabe brandocorp@gmail.com wrote:

Hi Angela,

It sounds like you might be getting a nil or empty response back from the chef server. I would try running the same thing in a pry/irb session and see what the return value looks like from rest.get_rest(’/nodes’).

For what it’s worth, you might also want to give ridley a try.

Thanks,
Brandon

On Fri, Jan 23, 2015 at 10:48 AM, ANGELA EBIRIM aebirim@icloud.com wrote:
Hello everyone,

I’m new to Chef so be gentle!

I’m trying to query a Chef server through it’s API and have developed the following code:-

require 'chef/config’
require 'chef/log’
require 'chef/rest’
chef_server_url = "http//chef.prod.practor.service"
client_name = "ace67"
signing_key_filename=File.dirname(FILE) + “/.chef/ace67.pem”

rest = Chef::REST.new(chef_server_url, client_name, signing_key_filename)
nodes = rest.get_rest("/nodes")

nodes.keys.each do |node_name|
puts "Node name: #{node_name}"
end

It’s a simple Ruby script to query the Chef server. through it’s API, for a list of nodes. However when I run the script, I get an error of TypeError: can’t dup NilClass. It’s occurring on the nodes = rest.get_rest("/nodes") line.

I’m running Chef version 11.16.2

I’d appreciate any assistance with this.

Many thanks

Angela Ebirim

Sent from iCloud