iterating through word arrays is your friend in chef
Aug 28, 2015
200 words
1 minute read

Often times, when you’re installing a set of packages or Windows Features, for example, in Chef, you have a long list of items you want to install. Here’s one way to do it:

package 'nginx' do
  action :install
end

package 'mysql' do
  action :install
end

package 'git' do
  action :install
end

Pretty cumbersome, no? But with the magic of array iteration, we can knock this out much more simply:

%[nginx mysql git].each do |package|
  package package do
    action :install
  end
end

The %w portion says “split this array based on whitespace (or by ‘word’)”. Then for each of those items, shove the value into the variable package, which we can then loop through.

Magic!


comments powered by Disqus

Back to posts