简体   繁体   English

Ruby对模板变量的操作-Chef

[英]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 我的erb文件是

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. 我实际上想执行一些ruby操作(主要是过滤出并计数my_id的组件),然后将这些值(每个组件的数量)传递回模板并进一步使用它。 What should be erb configuration or anything thats need to be added in template block? erb配置应该是什么,或者需要在模板块中添加什么?

(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). (在这里,my_id实际上具有子网,我想获取那些子网的数量及其值,以便可以进一步使用它执行另一个http请求并获取每个子网中的节点)。

Don't know much about Chef Cookbooks but you can write some ruby within an ERB template. 对Chef Cookbook不太了解,但是您可以在ERB模板中编写一些红宝石。 It is not the cleanest solution I believe though. 我相信这不是最干净的解决方案。

See here on how to embed code in your ERB 请参阅此处 ,了解如何将代码嵌入到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. 要获取JSON数据,您想使用Chef::HTTP::SimpleJSON ,它将为您进行解析, Chef::HTTP::SimpleJSON进行其他操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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