Chef 在数组中使用变量

Chef using variables in a array

如何在 Chef 中使用数组中的变量。我正在尝试使用数组创建多个文件夹。但是我的代码不起作用:

代码

variable1 = "/var/lib/temp"
variable2 = "/opt/chef/library"

%w{ #{variable1} #{variable2} }.each do |dir| 
    directory dir do
       owner 'root'
       group 'root'
       mode  '755'
       recursive true
       action :create
    end
end 

%w{ ... }语法用于声明一个单词数组,不进行任何插值。因为你想要一个预先存在的字符串数组,你可以通过声明一个普通的旧数组来做到这一点:

[ variable1, variable2 ].each do |dir|
  # ...
end

或者你可以切换到这个:

%w[ /var/lib/temp /opt/chef/library ].each |dir|
  # ...
end

第二种形式更有意义,因为这是您的意图。不需要中间变量。