Author

Author


Rajesh Kumar

Rajesh Kumar

DevOps@RajeshKumar.xyz

DevOps@RajeshKumar.xyz

Support my work @Patron! Support my work @Patron!

Looping in Chef Cookbooks

Exmaple Program 1


['vim', 'ntp', 'git'].each do |package|
 package package do
   action :install
 end
end

Exmaple Program 2


packages = ['vim', 'ntp', 'git']

packages.each do |package|
 package package do
   action :install
 end
end

Exmaple Program 3


default['createdir']['shared_dir'] = 'shared-lib'
default['createdir']['config_dir'] = 'shared-config'
default['createdir']['mode'] = 755
default['createdir']['owner'] = "ec2-user"
default['createdir']['group'] = "root"

[ "/opt/#{node['createdir']['shared_dir']}", "/opt/#{node['createdir']['config_dir']}" ].each do |path|
  directory path do
    mode node['createdir']['mode']
    owner node['createdir']['owner']
    group node['createdir']['group']
    recursive true
    action :create
  end
end

NOTES

#{node['createdir']['shared_dir']} with STRING
node['createdir']['mode'] without string

Avail Rajesh Kumar as trainer at 50% Discount
Puppet Online Training
Puppet Classroom TrainingEnroll Now