Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k points)

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

1 Answer

0 votes
by (50.2k points)

For this, you have 2 ways 

Option1: 

File resource can also put inside the ruby block for that you can use like this to your example mentioned in the question

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:

In this type you can use node.run_state to pass data around. For this let’s see how to pass data with respect to example

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

Related questions

0 votes
1 answer
asked Jan 17, 2020 in DevOps and Agile by Abhishek_31 (12.7k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...