简体   繁体   中英

Ruby operations on template variables - Chef

I have written following template in my chef cookbook recipe

template '/etc/app.conf' do
   variables({
   my_id: Chef::HTTP.new(https://example.com).get('/',{header})
})
end

And my erb file is

Output is : <%= @my_id %>

I actually want to perform some ruby operations(mainly filter out and count the components of my_id) and then pass those values(count of each component) back to the template and use it further. What should be erb configuration or anything thats need to be added in template block?

(Here, my_id actually has the subnets and I want to get those the count of those subnets and its values so that I can use it further to perform another http request and get the nodes in each of the subnet).

Don't know much about Chef Cookbooks but you can write some ruby within an ERB template. It is not the cleanest solution I believe though.

See here on how to embed code in your ERB

The thing you pasted from (hopefully you were summarizing since you missed a bunch of quotes in there) was only a quick tip. To get JSON data you want to use Chef::HTTP::SimpleJSON , the will do the parsing for you and whatnot.

variables data: Chef::HTTP::SimpleJSON.new('https://whatever.com/').get('/foo')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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