简体   繁体   English

通过Chef Server API获取配方列表

[英]Getting recipe list through chef server api

I want to get the recipes that a cookbook contains, through chef-server-api. 我想通过Chef-server-api获取食谱中包含的食谱。 Following is the code I'm using for getting the cookbook list, individual cookbook details through the api : 以下是我用来获取菜谱列表的代码,以及通过api获得的菜谱详细信息:

require 'rubygems'
require 'chef/config'
require 'chef/log'
require 'chef/rest'
require 'chef/cookbook_version'

client_name = "admin"
signing_key_filename="c:/chef-repo/.chef/admin.pem"
server_url = "https://10.132.17.244:443"

rest = Chef::REST.new(server_url, client_name, signing_key_filename)
cookbooks = rest.get_rest("/cookbooks?all_versions")

cookbooks.keys.each do |name|
cookbook_versions = rest.get_rest("/cookbooks/#{name}")
print "#{name}\n"

cookbook_versions[name]["versions"].each do |cv|

version = cv["version"]
cookbook = rest.get_rest("/cookbooks/#{name}/#{version}")
 print "\t#{cookbook}\n"
 #parsed = JSON[cookbook]

end
end

The problem I'm facing is to get the recipe list from the 'cookbook' object. 我面临的问题是从“食谱”对象获取食谱列表。 I tried parsing it to ruby hash and then read, but of no use. 我尝试将其解析为ruby hash,然后读取,但没有用。 If I directly print the 'cookbook' variable, the output is something like the screenshot 如果我直接打印'cookbook'变量,则输出类似于屏幕截图 在此处输入图片说明

I'm not able to get how to interpret the output I am getting by hitting the '/cookbooks/NAMEW/VERSION' endpoint, and get the recipes present in an individual cookbooks. 我无法通过点击'/ cookbooks / NAMEW / VERSION'端点来解释我得到的输出,并无法获得食谱中的食谱。

When using the Chef gem it automatically decodes some responses into Ruby objects for you. 使用Chef gem时,它会自动为您将一些响应解码为Ruby对象。 You can either use the object directly (specifically you want #recipe_filenames and then parse those to the cookbook_name::recipe_name format) or you could use a better API client like Chef-API or PyChef . 您可以直接使用该对象 (特别是想要#recipe_filenames ,然后将其解析为cookbook_name::recipe_name格式),也可以使用更好的API客户端,例如Chef-APIPyChef

Need a ruby solution? 需要红宝石解决方案? The following example uses jq to filter the JSON resultset returned by knife: 下面的示例使用jq过滤刀返回的JSON结果集:

$ knife cookbook show apache2 2.0.0 recipes -Fj | jq '.[]|.name'
"mod_cgi.rb"
"mod_proxy_http.rb"
"mod_proxy_html.rb"
"mod_access_compat.rb"
"mod_authz_dbd.rb"
"mod_proxy_express.rb"
..
..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM