简体   繁体   English

如何使用厨师食谱创建多个文件?

[英]How can I create multiple files using chef recipe?

I wanted to create the loop using 'each' but I couldn't.我想使用“每个”创建循环,但我做不到。 When I looked at the examples using 'each', array was always used.当我查看使用“每个”的示例时,总是使用数组。 Is there a solution using while or for?有没有使用while或for的解决方案?

while $i <=  do

  file "/home/user/files/file_#{i}" do
    owner 'root'
    group 'root'
    mode '0755'
    action :create
  end
  $i +=1
end

I want it to look like this file_1 file_2 file_3 ...我希望它看起来像这样file_1 file_2 file_3 ...

You can use times or range methods instead of while .您可以使用timesrange方法代替while

Example using range:使用范围的示例:

(1..5).each do |i|
  file "/home/user/files/file_#{i}" do
    owner 'root'
    group 'root'
    mode '0755'
    action :create
  end
end

Here I'm using range of (1..5) , which will create file_1 to file_5 .这里我使用(1..5)的范围,它将创建file_1file_5 You can change it to the numbers with which the files should be created.您可以将其更改为创建文件时应使用的数字。

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

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