How to lazily evaluate an arbitrary variable with Chef

混江龙づ霸主 提交于 2019-11-30 08:47:00

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

For ruby_block, any variables within the block will need to be global as anything defined within the block is local to it.

You can't use a lambda for delayed execution in a library, so ruby_block works well in this case.

@thoughtcroft answer not working for me at chef-client 12.8.1

In this cases I place needed code into custom resource and call it with lazy attributes.

mycookbook_myresource "name" do
  attribute lazy { myvar }
end

Not elegant solution but it works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!