I am trying to run a loop over array in chef (The loop contains a bash command). Anyone has any idea on this ?
symlink_db = data_bag_item(“my”_db,”my”_db)
source = symlink_db[“sourceFile”]
instances = symlink_db["Instances"].split(',') <---Instances is a comma separated string in Databag
bash "create_link" do
puts "1: #{instances}" <-------Puts all instances correctly
instances.each do |instance|
puts "2: #{instance}" <------ This prints each instance in loop correctly
code <<-EOH
echo "ln -fs #{source} #{instance}"; <----- This is printed only for last instance in the loop
EOH
end
end
Appreciate if anyone can help soon.....Thanks
You need the bash resource inside your loop:
instances.each do |instance|
bash "create_link-#{instance}" do
code <<-EOH
echo "ln -fs #{source} #{instance}"
EOH
end
end
BTW, this is not idiomatic chef. You should simply use the link
resource like this:
instances.each do |instance|
link instance do
to source
end
end
An advantage of this approach is that it makes your recipe cross-platform. It's also a lot more readable.