如何使用Chef Windows重新启动资源仅重新启动一次
我当前正在尝试在chef资源中使用重新启动资源:如何使用Chef Windows重新启动资源仅重新启动一次,windows,chef-infra,reboot,Windows,Chef Infra,Reboot,我当前正在尝试在chef资源中使用重新启动资源: reboot 'ADS Install Complete' do action :nothing reason 'Cannot continue Chef run without a reboot.' only_if {reboot_pending?} end ... execute 'Initialize ADS Configuration INI' do command "\"#{node["ads-tfs-ini"]["
reboot 'ADS Install Complete' do
action :nothing
reason 'Cannot continue Chef run without a reboot.'
only_if {reboot_pending?}
end
...
execute 'Initialize ADS Configuration INI' do
command "\"#{node["ads-tfs-ini"]["tfsconfig_path"]}\" unattend \/create \/type:#{node["ads-tfs-ini"]["Scenario"]} \/unattendfile:\"#{node["ads-tfs-ini"]["unattend_file_path"]}\""
only_if { ! "#{ENV['JAVA_HOME']}".to_s.empty? }
notifies :request_reboot, 'reboot[ADS Install Complete]', :delayed
end
我得到了一个无休止的重新启动循环(客户端重新启动-->厨师客户端运行-->厨师客户端重新运行运行列表--客户端重新启动-->…)。如何只重新启动一次?您可以添加一些验证来检查计算机是否已重新启动一次
ruby_block "reboot" do
unless File.exist?("C:\reboot") do
block do
Chef::Util::FileEdit.new('C:\reboot').write_file
Chef::ShellOut.new("shutdown /r").run_command
end
end
end
这个解决方案不是很优雅,但它应该可以工作。重新启动在ruby块中,该块仅在C:\reboot不存在时运行。如果该文件不存在,块将创建该文件,然后调用重新启动。在第二次运行chef时,该文件将存在,因此不会触发重新启动
来自厨师资源:
reboot 'ADS Install Complete' do
action :nothing
reason 'Cannot continue Chef run without a reboot.'
only_if {reboot_pending?}
end
...
execute 'Initialize ADS Configuration INI' do
command "\"#{node["ads-tfs-ini"]["tfsconfig_path"]}\" unattend \/create \/type:#{node["ads-tfs-ini"]["Scenario"]} \/unattendfile:\"#{node["ads-tfs-ini"]["unattend_file_path"]}\""
only_if { ! "#{ENV['JAVA_HOME']}".to_s.empty? }
notifies :request_reboot, 'reboot[ADS Install Complete]', :delayed
end
使用“重新启动”资源重新启动节点,这是在某些平台上进行某些安装的必要步骤。Microsoft Windows、macOS和Linux平台支持使用此资源
只有当
execute
资源中的guard使execute资源运行时,如果ENV['JAVA\u HOME']
不是空的,您的。很可能设置了此环境变量,这就是为什么每次运行Chef时都会运行execute
资源,并触发重新启动
我的猜测是,您实际上只需要一个相反的,仅当变量为空时才运行资源。为此,您只需删除代码>从行开始
only_if { ENV['JAVA_HOME'].to_s.empty? }
如果我之前的猜测是错误的,那么您只需要在
guard更健壮的情况下更改。通过命令行,我了解到您创建了一些配置文件,因此当配置文件已经存在时,您不需要运行execute
resource:
not_if { ::File.exist?('/path/to/file/created/by/command') }