从链接中提取 https://docs.chef.io/lwrp_custom_provider_ruby.html
why-run mode is already enabled for platform resources. When platform resources are used as part of the action block in a lightweight provider, only the whyrun_supported? is required to allow the chef-client to run in why-run mode.
一旦我将whyrun_supported设为true,是否可以针对特定资源禁用它(例如,资源cookbook_file将始终运行但与策略无关,因此在运行cookbook时不应将其作为操作选择在为什么运行模式)
实施例:
cookbook_file "setLastUpdateDate.ps1" do
path "#{ENV['TMP']}\\setLastUpdateDate.ps1"
action :create
end
execute "Set the last update date of signatures" do
cwd "#{ENV['TMP']}"
command "powershell.exe -executionpolicy unrestricted -command \"& #{ENV['TMP']}\\setLastUpdateDate.ps1\" "
action :run
end
if some_value > other_value then
converge_by("This is the corrective action") do
execute "RunSomething to modify signature update interval" do
cwd "#{ENV['TMP']}"
command "some_corrective_command"
end
end
为什么运行模式的策略应该仅在最后一个执行块运行时才响应(因为它是执行纠正操作且相关的块)。前两个块是更多的先决条件步骤,以便稍后我可以确定端点是否需要采取纠正措施。因此,我希望有一种方法,其中为什么运行忽略前两个资源块
部分解析
Why_run不会检测通过shell_out运行的命令。 例如在您的提供商中,添加
class Provider
class MseCheck < Chef::Provider
include Chef::Mixin::ShellOut
def whyrun_supported?
true
end
def action_fix
command1 = "ipconfig ; hostname ; mkdir C:\\tempFolder "
shell_out!("powershell -executionpolicy unrestricted -command \"#{command1}\"")
end
end
end
即使策略通过shell_out对系统进行了更改,why_run也不会对其进行更新。