Reputation:
How can you use variables in an array in Chef. I am trying to create multiple folder with an array. However my code does not work:
Code
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
Upvotes: 2
Views: 3316
Reputation: 211540
The %w{ ... }
syntax is for declaring an array of words, and no interpolation is done. Since you want an array of pre-existing strings, you can do it this way by declaring a plain-old array:
[ variable1, variable2 ].each do |dir|
# ...
end
Or you can switch to this:
%w[ /var/lib/temp /opt/chef/library ].each |dir|
# ...
end
The second form makes a lot more sense since that's your intent. No need for intermediate variables.
Upvotes: 2