示例Chef HTTP GET / POST Cookbook

时间:2015-03-04 14:15:56

标签: chef chef-recipe

我正在学习厨师,需要一个非常简单的准系统食谱/食谱示例,可以调用外部HTTP API(GET / POST)并将内容放入文件中。我已经浏览了社区食谱,但他们似乎都学习并试图理解发生的事情是过度的,当然大多数人似乎都专注于安装软件包。有没有人可以在这里使用:)

2 个答案:

答案 0 :(得分:3)

您可以根据需要使用remote_file资源,例如:

remote_file "/tmp/testfile" do
  source "http://www.example.com/tempfiles/testfile"
  mode '0644'
  checksum "3a7dac00b1" # A SHA256 (or portion thereof) of the file.
end

您可以在nginx community cookbook

中看到它的实际效果
nginx_url = node['nginx']['source']['url'] ||
        "http://nginx.org/download/nginx-#{node['nginx']['source']['version']}.tar.gz"

src_filepath  = "#{Chef::Config['file_cache_path'] || '/tmp'}/nginx-#{node['nginx']['source']['version']}.tar.gz"

....

remote_file nginx_url do
  source   nginx_url
  checksum node['nginx']['source']['checksum']
  path     src_filepath
  backup   false
end

有关此内置资源的更多信息,您可以find here

答案 1 :(得分:0)

一种简单的方法是使用ruby中的'net / http'模块发送请求并在ruby_block中处理响应。有关模式信息,请参阅:http://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html