0

在我的 capistrano 任务中,我正在调用该superfunction方法。不幸的是,它抛出了一个Unexpected Return错误。我需要从superfunction方法中获取输出,以便在我的任务中进一步解析它。

def superfunction(cmd_type, command, client)
    run "#{command}" do |channel, stream, data|
        hostname = "#{channel[:host]}".tr('"','')
        result = "#{data}".to_s.strip
        return hostname, result
    end

end


task :gather, :roles => :hosts do
...
   servername, redhat_version = superfunction("redhat_version", "cat /etc/redhat-release", client)
end
4

1 回答 1

0

正在生成错误,因为您的块在方法返回后被调用(可能 capistrano 将其存储在内部)。作为一种简单的解决方法,您可以使用块获取所需的变量:

def superfunction(cmd_type, command, client)
  run "#{command}" do |channel, stream, data|
    hostname = "#{channel[:host]}".tr('"','')
    result = "#{data}".to_s.strip
    yield(hostname, result)
  end
end

task :gather, :roles => :hosts do
  superfunction("redhat_version", "cat /etc/redhat-release", client) do |servername, redhat_version|
    # Use servername and redhat_version here
  end
end
于 2012-10-02T20:29:31.423 回答