更新时间:2023-08-21 20:07:04
@ knocte是正确的,因为 capture
是有问题的,因为通常每个人都将部署定向到多个主机(并且捕获仅从第一个主机获取输出)。为了检查所有主机,您需要使用 invoke_command
代替(这是捕获
在内部使用的功能)。在以下示例中,我检查以确保所有匹配的服务器上都存在文件:
@knocte is correct that capture
is problematic because normally everyone targets deployments to more than one host (and capture only gets the output from the first one). In order to check across all hosts, you'll need to use invoke_command
instead (which is what capture
uses internally). Here is an example where I check to ensure a file exists across all matched servers:
def remote_file_exists?(path)
results = []
invoke_command("if [ -e '#{path}' ]; then echo -n 'true'; fi") do |ch, stream, out|
results << (out == 'true')
end
results.all?
end
请注意, invoke_command
默认情况下使用 run
-请查看您可以通过的选项以获得更多控制权。
Note that invoke_command
uses run
by default -- check out the options you can pass for more control.