Let's say I have one variable, directory_list
, which I define and set in a ruby_block named get_directory_list
. Can I use directory_list
later on in my recipe, or will the compile/converge processes prevent this?
Example:
ruby_block "get_file_list" do
block do
transferred_files = Dir['/some/dir/*']
end
end
transferred_files.each do |file|
file "#{file}" do
group "woohoo"
user "woohoo"
end
end
Option 1: You could also put your file resource inside the ruby_block.
ruby_block "get_file_list" do
block do
files = Dir['/some/dir/*']
files.each do |f|
t = Chef::Resource::File.new(f)
t.owner("woohoo")
t.group("woohoo")
t.mode("0600")
t.action(:create)
t.run_context=(rc)
t.run_action(:create)
end
end
end
Option 2: You could use node.run_state to pass data around.
ruby_block "get_file_list" do
block do
node.run_state['transferred_files'] = Dir['/some/dir/*']
end
end
node.run_state['transferred_files'].each do |file|
file "#{file}" do
group "woohoo"
user "woohoo"
end
end
Option 3: If this were just one file, you could declare a file resource with action :nothing
, look up the resource from within the ruby_block, and set the filename, and then notify the file resource when the ruby_block runs.
Option 4: If this is the example from IRC today, just place your rsync and the recursive chown inside a single bash resource. rsync and chown are already idempotent, so I don't think it's objectionable in this particular case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With