Chef 自定义资源守卫

Chef Custom Resource Guards

我有一个自定义资源(在我的食谱的 /resources 文件夹中一个名为 dsc_class.rb 的文件中)。基本上,这是一种无需安装和使用完整 DSC 引擎即可轻松调用 DSC class 资源的方法。

resource_name :dsc_class

default_action :create

property :file_name, kind_of: String, required: true, name_property: true

action :create do  
  powershell_script file_name do
    cwd ::File.dirname(file_name)
    code <<-EOH
    gc "#{file_name}" -Raw | iex
    $o = New-Object #{file_name.split('/').last.split('.').first}
    $o.Set()
    EOH
    not_if <<-EOH
    gc "#{file_name}" -Raw | iex
    $o = New-Object #{file_name.split('/').last.split('.').first}
    o.Test()
    EOH
  end
end

效果很好 - 但动作总是 "executes" 所以当资源守卫和 powershell 脚本不执行时,我在 log/output 中得到 "x resources updated" 到 chef-client 运行.

如何在我的自定义资源中适当地保护?

更新:食谱包含

 dsc_class "/install/dsc/MyDscResource.ps1"

powershell_script 与所有其他 execute 样式的资源一样,它始终运行 command/script/whatever 并依赖于外层 not_ifonly_if 子句.在这种情况下,您可以将它们放在配方代码中的 dsc_class 资源实例上。