我最近遇到了我的 Linux 远程主机使用 net-ssh gem 的问题。为了排除任何奇怪的环境serverspec-init
,使用选择的 Linux/SSH 选项和相应的远程主机执行清理。此外,我将 spec_helper.rb 调整为以下内容:
require 'serverspec'
require 'net/ssh'
set :backend, :ssh
host = ENV['TARGET_HOST']
options = Net::SSH::Config.for(host)
options[:user] ||= "MYUSER"
options[:password] ||= "MYPASSWORD"
set :sudo_password, "MYPASSWORD"
set :host, options[:host_name] || host
set :ssh_options, options
这导致了以下输出:
C:\Users\JumpStart\Documents\Repositories\connecttest>rake spec
C:/Ruby27-x64/bin/ruby.exe -I'C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rspec-support-3.9.2/lib';'C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rspec-core-3.9.1/lib' 'C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rspec-core-3.9.1/exe/rspec' --pattern 'spec/MYHOST/*_spec.rb'
An error occurred while loading ./spec/MYHOST/sample_spec.rb.
On host `MYHOST'
Failure/Error:
describe package('httpd'), :if => os[:family] == 'redhat' do
it { should be_installed }
end
NotImplementedError:
OS detection failed.
# ./spec/MYHOST/sample_spec.rb:3:in `<top (required)>'
No examples found.
Finished in 0.00006 seconds (files took 11.86 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
C:/Ruby27-x64/bin/ruby.exe -I'C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rspec-support-3.9.2/lib';'C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rspec-core-3.9.1/lib' 'C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rspec-core-3.9.1/exe/rspec' --pattern 'spec/MYHOST/*_spec.rb' failed
我通常可以使用用户名/密码手动 ssh 连接到主机。