0 votes
1 view
in Devops and Agile by (29.9k 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 (51.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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...