Chef script - read content of a file to use in http _request

1k views Asked by At

I have a chef script that want to read the value of a file.

http_request "create repo" do

  action :post

  url "http://chef.sparqlr.net:8080/openrdf-sesame/repositories/SYSTEM/statements?context=%3Curn:x-local:graph1%3E&baseURI=%3Curn:x-local:graph1%3E"

  headers "Content-Type" => "application/x-trig"

  message *<Read in content for a file>*

end

I would like the message of the post coming from a file, can anyone advise what the syntax should be? I have try a few option, with ruby block etc but no luck. Many thanks!

1

There are 1 answers

0
Tensibai On
http_request "create repo" do
  action :post
  url "http://chef.sparqlr.net:8080/openrdf-sesame/repositories/SYSTEM/statements?context=%3Curn:x-local:graph1%3E&baseURI=%3Curn:x-local:graph1%3E"
  headers "Content-Type" => "application/x-trig"

  message ::File.read("/path/to/file")
end

You can use standard ruby classes to read a file, but as Chef has it's own File class you hav to prefix it with :: to use the ruby class and not the Chef one.

In case the source file is deployed via chef you can wrap the File.read in a lazy block so it will be executed only when the provider is called.

Example:

template "/path/to/file" do
   source "file.erb"
end

http_request "create repo" do
  action :post
  url "http://chef.sparqlr.net:8080/openrdf-sesame/repositories/SYSTEM/statements?context=%3Curn:x-local:graph1%3E&baseURI=%3Curn:x-local:graph1%3E"
  headers "Content-Type" => "application/x-trig"

  message lazy { ::File.read("/path/to/file") }
end