如何使用 HWRP 中的 Chef::Resource::CookbookFile?

How to use Chef::Resource::CookbookFile from HWRP?

当我使用食谱中的 cookbook_file 资源时,它按预期工作

cookbook_file 'd:/temp/test.txt' do
   source 'text.txt'
end

当尝试使用以下代码从 HWRP 操作进行相同操作时

cbfile = Chef::Resource::CookbookFile.new('d:/temp/test.txt', run_context)
cbfile.source('text.txt')
cbfile.run_action(:create)

失败并出现错误:

Chef::Exceptions::CookbookNotFound: cookbook_file[text.txt] (dynamically defined) had an error: Chef::Exceptions::CookbookNotFound: Cookbook  not found.

怎么了?

DSL 比您的手动代码做更多的事情。与此最相关的是设置 @cookbook_name ,它用于查找食谱以查找文件。也就是说,您可以从 "HWRP" 中很好地使用 DSL,我会推荐它,因为我们不保证 DSL 的内部结构会随着时间的推移保持稳定。