Ankit Pandoh
Ankit Pandoh

Reputation: 577

Chef - Read external properties in chef recipe.rb files

I am new to Chef deployment tool and would like to use it for one of my use case. I am able to invoke a POST call to rest endpoint. But with the hardcoded url and json data as shown below:

recipe/default.rb

template '/etc/response.txt' do
        owner "root"
        group "root"
        mode "0644"
        source "response.erb"
        variables({
                response:Chef::HTTP.new('https://abc.om/').post('/api/v1/something',
                { "data1": "value1", "data2": "value2"},
                {'Accept'=> 'application/json', 'Content-Type'=>'application/json'})
        })
end

I would like to use dynamic value of url, json and other request parameters. I want it to be externalized such as in environments file (the way we do it in java web applications) so that before running the chef-client on nodes, user can provide values for request parameters. How can we achieve that in chef?

Upvotes: 0

Views: 310

Answers (1)

Mr.
Mr.

Reputation: 10102

chef syntax (recipe dsl) runs on top of ruby. thus you can leverage the power of ruby to dynamically -- in chef terms, you can do it in compilation phase or in convergence phase:

  • compilation phase: all recipes are loaded in the order specified by the expanded run-list.
  • convergence phase: Each resource is executed in the order identified by the run-list, and then by the order in which each resource is listed in each recipe. ...each action configures a specific part of the system.

for more information about chef-client about the phase, see chef-client overview.

best to do it in the convergence phase and you can achieve it using the ruby_block and\or http_request resources.

if you would like to have a static file within your cookbook, then place it within COOKBOOK_NAME/files/default and read it as you please. see cookbook_file resource.

Upvotes: 0

Related Questions