How to Lazily Evaluate an Arbitrary Variable with Chef

How to lazily evaluate an arbitrary variable with Chef

I haven't tested this particular code but I have done something similar in cookbooks and used a lambda to delay evaluation as follows:

home = lambda {node['etc']['passwd'][node['nodejs']['user']]['dir']}

execute "npm install" do
command "npm install #{prefix}#{app} --prefix #{home.call}"
end

Removing packages in Chef, from a generated list

How about this:

ruby_block "somehow get the list of packages to remove" do
block do
node.run_state['remove_packages'] = %w( foo bar baz )
end
end

package "remove the list of packages" do
package_name lazy { node.run_state['remove_packages'] }
action :remove
only_if { node.run_state['remove_packages'] }
end

(tested with chef-apply 13.5.3)



Related Topics



Leave a reply



Submit