[英]Chef Ruby — are Loops within the variables for template possible?
I have spent already a long deal of time trying to figure out how to do this, I have also investigated but I have not found the right approach to it...? 我已经花了很多时间试图弄清楚如何做到这一点,我也调查了但是我还没找到合适的方法......?
basically I am trying to do something like the following: 基本上我正在尝试做类似以下的事情:
types = ['type1','type2']
classes = ['class1','class2']
classes.each do |class|
types.each do |type|
template "/files/filename.txt" do
source "source_file.erb"
owner "root"
group "root"
mode "0440"
variables({
:pri_areas => node['area']['#{type}']['#{class}'],
:rev_areas => node['area']['#{type}']['#{class}']
})
end
end
end
Obviously I got all the attributes already defined so everything looks all right from that front.. I still cannot manage to get a loop with arrays like that withing the variables? 显然我已经定义了所有已经定义的属性,所以从那个方面看起来一切都很好..我还是无法设法得到一个带有变量的数组的循环? Maybe another different approach? 也许另一种不同的做法
Any ideas/help? 任何想法/帮助?
Thanks very much. 非常感谢。
Your code has some issues which you need to fix before it will properly work. 您的代码有一些问题需要在正常工作之前修复。
At first, class
is a reserved keyword in Ruby and thus can't be used as a variable name. 起初, class
是Ruby中的保留关键字,因此不能用作变量名。 You should use another one, eg klass
. 你应该使用另一个,例如klass
。
Secondly, class
(or klass
) as well as type
are already strings inside your loop. 其次, class
(或klass
)以及type
都已经在你的循环中的字符串。 Thus you don't need to attempt string interpolation. 因此,您不需要尝试字符串插值。 You can directly use this: 你可以直接使用这个:
variables({
:pri_areas => node['area'][type][klass],
:rev_areas => node['area'][type][klass]
})
The reason why your string interpolation didn't work is that ruby knows two different kinds of String literals: ones with "
and ones with '
. The difference is that the ones delimited with '
do not allow string interpolation and generally do not interpret anything inside then as something else than the literal written string. Only in Strings delimited by "
, you can perform string interpolation like "#{foo}"
and use escape sequences like \\n
. 你的字符串插值不起作用的原因是ruby知道两种不同的字符串文字:带有"
和带有'
的字符串。不同之处在于用'
不允许字符串插值并且通常不解释任何内容然后作为文字书写字符串以外的其他东西。只有在字符串分隔的"
,你可以执行字符串插值,如"#{foo}"
并使用转义序列,如\\n
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.