如果 windows 服务是 运行,我可以使用守卫来做饭吗?
Can I use guards to chef if a windows service is running?
我正在编写一份厨师食谱,仅当服务无法正常工作时,我才需要对此执行操作(运行 一批)。
我使用这个片段:
batch 'run commnad' do
cwd target_path + '/bin/win64'
code 'command to be executed'
not_if '::Win32::Service.exists?("Service name")'
end
不过好像不行。在看到 this 问题后,我使用 if 子句而不是 guard 更改了流程并且它工作正常:
if !::Win32::Service.exists?("Service name") then
batch 'Install zabbix agent' do
cwd target_path + '/bin/win64'
code 'command to be executed'
end
end
但据我所知,这不应该是管理它的正确方法,所以我想知道:为什么守卫不能正常工作?
谢谢,
米歇尔
您编写 not_if
语句的方式将命令作为 shell 脚本运行。
shell 不知道 Ruby 代码,因此整个命令将失败。
需要先:
require win32/service
为了将 not_if
与 Ruby 代码一起使用,您应该将其放在块中:
not_if { ::Win32::Service.exists?("Service name") }
在此处查看更多示例(在页面上搜索 not_if
):
https://docs.chef.io/resource_common.html
这是工作示例(Chef 13)
require 'win32/service'
windows_service "jenkins" do
action [:stop, :disable]
only_if { ::Win32::Service.exists?("jenkins")}
end
我正在编写一份厨师食谱,仅当服务无法正常工作时,我才需要对此执行操作(运行 一批)。 我使用这个片段:
batch 'run commnad' do
cwd target_path + '/bin/win64'
code 'command to be executed'
not_if '::Win32::Service.exists?("Service name")'
end
不过好像不行。在看到 this 问题后,我使用 if 子句而不是 guard 更改了流程并且它工作正常:
if !::Win32::Service.exists?("Service name") then
batch 'Install zabbix agent' do
cwd target_path + '/bin/win64'
code 'command to be executed'
end
end
但据我所知,这不应该是管理它的正确方法,所以我想知道:为什么守卫不能正常工作?
谢谢, 米歇尔
您编写 not_if
语句的方式将命令作为 shell 脚本运行。
shell 不知道 Ruby 代码,因此整个命令将失败。
需要先:
require win32/service
为了将 not_if
与 Ruby 代码一起使用,您应该将其放在块中:
not_if { ::Win32::Service.exists?("Service name") }
在此处查看更多示例(在页面上搜索 not_if
):
https://docs.chef.io/resource_common.html
这是工作示例(Chef 13)
require 'win32/service'
windows_service "jenkins" do
action [:stop, :disable]
only_if { ::Win32::Service.exists?("jenkins")}
end