【发布时间】:2019-04-13 02:11:46
【问题描述】:
在厨师库中无法访问os.windows?,而在食谱中可以访问。我认为可以通过在我的库中添加 require 'os' 行以使其可用来获得它。
require 'os'
module Project
module Helper
# ...
def serviceExists?(service_name)
if os.windows?
puts 'Windows detected'
# ... etc ...
else
raise 'Unimplemented..'
end
end
# ...
end
end
这不起作用。相反,我收到了一个错误:
LoadError
---------
cannot load such file -- os
是否可以在不将os 作为参数传递给方法的情况下访问os 变量或确定库中的操作系统?我希望处理库中的操作系统复杂性,以保持配方更清洁。
我通过以下not_if 语句调用代码。
batch "Install #{service_name} service" do
extend Project::Helper
cwd install_home
code <<-EOH
@echo off
call \"installSvc.cmd\"
EOH
not_if { serviceExists?(service_name) }
end
【问题讨论】:
-
我想过做stackoverflow.com/a/20341462/1247302之类的东西,但感觉很脏。
-
尝试通过
Chef::Recipe.send(:include, Project::Helper)stackoverflow.com/a/22081109/1247302 将模块混合到Recipe DSL 中,但我收到os不作为方法存在的错误。undefined methodos' for Chef::Resource::Batch`(我从配方中的not_if调用)
标签: chef-infra