我最近尝试执行一个简单的任务:如果软件包不存在,则通过从 Web 位置(内部存储库)中拉出分发并在不再需要时将其删除来安装它。
了解 :before 通知我想出了以下优雅的代码(在此示例中,变量“pkg”保留分发镜像的名称,“pkg_src_location”是我的网络存储库的 URL,“name_of_package”被命名为已安装的包):
local_image = "#{Chef::Config['file_cache_path']}/#{pkg}"
remote_file 'package_image' do
path local_image
source "#{pkg_src_location}/#{pkg}"
action :nothing
end
package name_of_package do
source local_image
notifies :create, 'remote_file[package_image]', :before
notifies :delete, 'remote_file[package_image]', :delayed
end
我很惊讶它不起作用......实际上'包'资源正在融合而没有创建'remote_file' - 由于源local_image没有到位而失败......
我做了一个简单的测试:
log 'before' do
action :nothing
end
log 'after' do
action :nothing
end
log 'at-the-end' do
action :nothing
end
log 'main' do
notifies :write, 'log[before]', :before
notifies :write, 'log[at-the-end]', :delayed
notifies :write, 'log[after]', :immediately
end
我学到的是'main'实际上收敛了两次!一次是第一次遇到,一次是在“之前”资源收敛之后......
Recipe: notify_test::default
* log[before] action nothing (skipped due to action :nothing)
* log[after] action nothing (skipped due to action :nothing)
* log[at-the-end] action nothing (skipped due to action :nothing)
* log[main] action write
* log[before] action write
* log[main] action write
* log[after] action write
* log[at-the-end] action write
这是错误还是功能?如果这是“功能”,那真是太糟糕了,Chef 根本不应该拥有它。它的工作方式根本没用,只会浪费人们的时间......
任何对厨师有更深入了解的人都可以评论吗?有什么办法可以让 ':before' 工作吗?也许我只是在这里做错了什么?
最佳答案
更具体地说,before
计时使用“为什么运行”系统来猜测资源是否需要更新。在这种情况下,包资源一开始就无效,所以whyrun 无法判断需要更新。
关于notifications - Chef 'notifies :action, ' 资源', :before' work at all as it should?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40806714/