是否可以在ruby脚本中的Chef之外使用so=shellout(“linux cmd”)呢?
我很好奇,有没有可能在Chef之外的ruby脚本中使用shellout?如何设置此功能?是否可以在ruby脚本中的Chef之外使用so=shellout(“linux cmd”)呢?,shell,chef-infra,mixlib-shellout,Shell,Chef Infra,Mixlib Shellout,我很好奇,有没有可能在Chef之外的ruby脚本中使用shellout?如何设置此功能?gem安装mixlib shell输出 在ruby脚本中 require 'mixlib/shellout' cmd = Mixlib::ShellOut.new('linux cmd') cmd.run_command # And then optionally, to raise an exception if the command fails like shell_out!() cmd.error!
gem安装mixlib shell输出
在ruby脚本中
require 'mixlib/shellout'
cmd = Mixlib::ShellOut.new('linux cmd')
cmd.run_command
# And then optionally, to raise an exception if the command fails like shell_out!()
cmd.error!
预计到达时间:
如果您不想自己创建实例,我通常会在使用它的脚本中转储此包装器功能:
def shellout(cmd, ok_exits = [0])
run = Mixlib::ShellOut.new(cmd)
run.run_command
if run.error? || !ok_exits.include?(run.exitstatus)
puts "#{cmd} failed: #{run.stderr}"
exit 2
end
run.stdout
end
这只会提供基本API,
shell\u out()
helper实际上来自Chef。我将编辑您的示例:)以便总结Mixlib::ShellOut。应该使用新的?helpershell\u out
仅由Chef?Mixlib::Shellout
提供,它是一个类,因此需要初始化它的实例。Chef将其包装在一个模块中,该模块可用于获取类方法,但它依赖于Chef环境。不过,您可以包装自己的模块。就个人而言,我定义要求“mixlib/shellout”;def外壳(cmd);so=Mixlib::ShellOut.new(cmd,超时:60);so.run_命令;所以结束
。在stdout
、stderr
和exitstatus
方面,它与Chef组件完全相同。尝试a=shell\u out('ls')
并放置一个.stdout
。Mixlib必须与gem一起安装。