![使用ansible安装ruby和gems后无法访问gem](https://linux22.com/image/130824/%E4%BD%BF%E7%94%A8ansible%E5%AE%89%E8%A3%85ruby%E5%92%8Cgems%E5%90%8E%E6%97%A0%E6%B3%95%E8%AE%BF%E9%97%AEgem.png)
我正在编写 Ansible 脚本来安装我的标准工作环境(Ruby on Rails)。
Ansible 在 sudo 下运行,并且已安装的 gem 也只能在 sudo 下访问。如果我gem env
作为普通用户这样做,我会得到以下信息:
$ gem env
/usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:10:in `require': no such file to load -- rubygems/defaults (LoadError)
from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:10
from /usr/local/bin/gem:8:in `require'
from /usr/local/bin/gem:8
当我打开有问题的文件时,我可以看到以下内容:
#/usr/local/lib/ruby/site_ruby/1.8/rubygems.rb
# -*- ruby -*-
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++
gem_disabled = !defined? Gem
require 'rubygems/defaults'
require 'rbconfig'
require 'thread' # HACK: remove me for 1.5 - this is here just for rails
10号线就是require 'rubygems/defaults'
。
这样做向sudo gem env
我展示了以下内容:
$ sudo gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.4.2
- RUBY VERSION: 1.8.6 (2010-09-02 patchlevel 420) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.8
- /home/max/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- "gem" => "--no-document --no-ri --no-rdoc"
- REMOTE SOURCES:
- http://rubygems.org/
谁能告诉我需要做什么才能让我的普通用户在非 sudo 模式下可以访问 gems?
编辑:更多信息。我rubygems/defaults
通过使用以下命令查找了应该从哪里加载locate
:
$ locate rubygems | grep default
/usr/lib/ruby/1.9.1/rubygems/defaults.rb
/usr/local/lib/ruby/gems/1.8/gems/rubygems-update-1.2.0/lib/rubygems/defaults.rb
/usr/local/src/rubygems-1.4.2/lib/rubygems/defaults.rb
猜测它应该正在加载/usr/local/lib/ruby/gems/1.8/gems/rubygems-update-1.2.0/lib/rubygems/defaults.rb
,我更改了 require 行以获得完整路径:
require '/usr/local/lib/ruby/gems/1.8/gems/rubygems-update-1.2.0/lib/rubygems/defaults'
这似乎有效 - 然后它在线上失败了require 'thread'
,我大概可以做同样的事情。
因此,在查找要包含的内容时,Ruby 似乎需要被告知从哪个文件夹开始。也许这就是我需要做的?但我不知道最好的方法......