使用 Ansible 安装 Bundler gem [英] Install Bundler gem using Ansible

查看:29
本文介绍了使用 Ansible 安装 Bundler gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ansible 在我的 VPS 上安装 Bundler.

I am trying to install Bundler on my VPS using Ansible.

我已经设置了 rbenv,全局 ruby​​ 是 2.1.0.

I already have rbenv set up and the global ruby is 2.1.0.

如果我以 root 身份通过 SSH 进入服务器并运行 gem install bundler,它会完美安装.

If I SSH as root into the server and run gem install bundler, it installs perfectly.

我尝试了以下三种使用 Ansible 安装 Bundler gem 的方法,所有三种方法都没有产生错误,但是当我通过 SSH 进入并运行 gem list 时,Bundler 无处可见.

I have tried the following three ways of using Ansible to install the Bundler gem and all three produce no errors, but when I SSH in and run gem list, Bundler is nowhere to be seen.

尝试 1:

---
- name: Install Bundler
  shell: gem install bundler

尝试 2:

---
- name: Install Bundler
  shell: gem install bundler

尝试 3:

---
- name: Install Bundler
  gem: name=bundler
       state=latest

我还尝试了最后一次尝试 user_install=yesuser_install=no,但都没有任何区别.

I have also tried the last attempt with user_install=yes and also with user_install=no and neither make any difference.

有什么想法可以让我通过 Ansible 正确安装 Bundler?

Any ideas how I can get it to install Bundler correctly via Ansible?

我已经为此工作了一段时间,我安装了 1 个 ruby​​ 版本:2.1.0 并且发现 rbenv 的 shims 目录不包含 bundle 的 shim.

I've been working on this for a little while now and I have 1 ruby version installed: 2.1.0 and ahve found that the shims directory for rbenv does not contain a shim for bundle.

bundle 的垫片应该在那里吗?我只是对为什么 capistrano 找不到 bundle 命令感到困惑,因为它在我运行 sudo gem list 时列出,但在我运行 gem list?

Should a shim for bundle be in there? I'm just getting confused as to why capistrano cannot find the bundle command as it's listed when I run sudo gem list but NOT when I run gem list?

root@weepingangel:/usr/local/rbenv/shims# echo $PATH
/usr/local/rbenv/shims:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
root@weepingangel:/usr/local/rbenv/shims# gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 2.2.0
  - RUBY VERSION: 2.1.0 (2013-12-25 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0
  - RUBY EXECUTABLE: /usr/local/rbenv/versions/2.1.0/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/rbenv/versions/2.1.0/bin
  - SPEC CACHE DIRECTORY: /root/.gem/specs
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/rbenv/versions/2.1.0/lib/ruby/gems/2.1.0
     - /root/.gem/ruby/2.1.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["http://gems.rubyforge.org", "http://gems.github.com"]
     - "gem" => "--no-ri --no-rdoc"
  - REMOTE SOURCES:
     - http://gems.rubyforge.org
     - http://gems.github.com
  - SHELL PATH:
     - /usr/local/rbenv/versions/2.1.0/bin
     - /usr/local/rbenv/libexec
     - /usr/local/rbenv/shims
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games

有什么想法吗?

所以,我认为我有两个主要问题:

So, I think the two main problems I have:

  1. 为什么 bundler 只在我运行 sudo gem list 时可见?

我的部署说:

INFO [18d5838c] Running /usr/bin/env bundle install --binstubs  
/var/rails_apps/neiltonge/shared/bin --path  
/var/rails_apps/neiltonge/shared/bundle --without development test
--deployment --quiet on 188.226.159.96 DEBUG [18d5838c] Command: cd /var/rails_apps/neiltonge/releases/20140301205432 && ( PATH=$PATH
/usr/bin/env bundle install --binstubs
/var/rails_apps/neiltonge/shared/bin --path
/var/rails_apps/neiltonge/shared/bundle --without development test
--deployment --quiet ) DEBUG [18d5838c]     /usr/bin/env: bundle: No such file or directory

这是我的$PATH:

/usr/local/rbenv/shims:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

为什么无法定位bundle?

Why can't bundle be located?

推荐答案

问题在于,当通过 ansible 运行 gem install bundler 时,您没有正确初始化 rbenv,因为 rbenvinit.bashrc.bash_profile 中运行.所以使用的 gem 命令是系统命令,而不是作为 rbenv shim 安装的命令.因此,无论何时安装 gem,它都会在系统范围内安装,而不是在您的 rbenv 环境中.

The problem is that, when running gem install bundler via ansible, you're not initializing rbenv properly, since rbenv init is run in .bashrc or .bash_profile. So the gem command used is the system one, not the one installed as a rbenv shim. So whenever you install a gem, it is installed system-wide, not in your rbenv environment.

要正确初始化 rbenv,您必须执行 bash 本身并明确声明它是一个登录 shell,因此它读取它的初始化文件:

To have rbenv initialized properly, you must execute bash itself and explicitely state that it's a login shell, so it reads it's initialization files :

ansible your_host -m command -a 'bash -lc "gem install bundler"' -u your_rbenv_user 

如果您真的想以 root 身份执行此操作,请保留 -u your_rbenv_user 部分.

Leave the -u your_rbenv_user part if you really want to do this as root.

如果上面的命令有效,你可以很容易地把它变成一个剧本操作:

If the above command works, you can easily turn it into a playbook action :

- name: Install Bundler
  become_user: your_rbenv_user
  command: bash -lc "gem install bundler"

这很麻烦,但这是我目前找到的唯一方法.

It's cumbersome, but it's the only way I found so far.

这篇关于使用 Ansible 安装 Bundler gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆