我想将shell命令的输出放入变量中,以便以后在Chef配方中使用。
在bash中,我可以执行类似output=`tail -1 file.txt`
的操作,然后我可以echo $output
'执行资源'可以执行此操作,以便我可以在配方中稍后使用结果吗?
答案 0 :(得分:21)
ruby_block "check_curl_command_output" do
block do
#tricky way to load this Chef::Mixin::ShellOut utilities
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
curl_command = 'curl --write-out %{http_code} --silent --output /dev/null '+node['url']
curl_command_out = shell_out(curl_command)
if curl_command_out.stdout == "302"
...
else
...
end
end
action :create
end
Chef :: Mixin:如果您需要以特定用户身份运行命令,则ShellOut特别有用(参见http://www.slideshare.net/opscode/chef-conf-windowsdougireton):
ruby_block "run_command_as" do
block do
Chef::Resource::RubyBlock.send(:include,Chef::Mixin::ShellOut)
add_group = shell_out("your command",
{
:user => "my_user",
:password => "my_password",
:domain => "mycorp.com"
}
)
end
end
答案 1 :(得分:2)
为我工作
require 'chef/mixin/shell_out'
passenger_root = shell_out("passenger-config --root").stdout