Reputation: 6563
I would like to use the template content as the body for a POST request.
body = IO::Read('templatefile') # ???
response = Chef::REST::RESTRequest.new(:POST, 'http://localhost:8080/createItem', body, nil).call
What is the proper way to do this? Most usage patterns for template involve creating files, not using the template for another method.
Note: Ideally, The solution would not even create a temporary file.
Upvotes: 1
Views: 1556
Reputation: 436
I had once this issue in a cookbook. Instead of putting such files into template subdir, I recommend you to put it into a file subdir (same level than recipes, tempaltes, ... subdirs). The reason is simple: as you said yourself, template subdir is not designed to store such files, hence it is considered a bad practice.
For instance, you can put your file myfile
into your_cookbook_dir/files/default/myfile
, expecting your cookbook directiry on your filesystem is named your_cookbook_dir
.
Then, in you recipe, you can affect to a string the absolute path on the filesystem at runtime:
myfilepath = "#{run_context.cookbook_collection[cookbook_name].root_dir}/files/default/myfile"
At this point it becomes trivial to use the variable in another ruby instruction to read it!
I think this a better way (chef-design compliant) to adress your problem.
HTH.
Upvotes: 1
Reputation: 2457
Chef templates weren't really designed to be used this way, but you might be able to do what you want by finding the path of the template file and then rendering the template by hand. You can use the rendered template text in your POST request.
Upvotes: 1