Install Bundler gem using Ansible

前端 未结 6 478
忘了有多久
忘了有多久 2020-12-14 00:46

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

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

If I SSH as root into the server and run

相关标签:
6条回答
  • 2020-12-14 01:05

    This Worked for me:

    - name: rubygems | install bundler
      shell: gem install bundler
    
    - name: rbenv | rehash
      shell: RBENV_ROOT={{ rbenv_root }} rbenv rehash
    

    Sometimes after installing bundler, with rbenv on the system, you need to update your $PATH by runing rbenv rehash. I just tried the same thing with ansible, and it worked. Bundler is available in my $PATH after rehash.

    0 讨论(0)
  • 2020-12-14 01:09

    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.

    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 
    

    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.

    0 讨论(0)
  • 2020-12-14 01:12

    The cleanest and quickest way to install bundler using Ansible is this:

    Simply install rbenv by using the role https://github.com/zzet/ansible-rbenv-role and by configuring its plugins like so (obviously, there are more parameters to configure than just the plugins):

    rbenv_plugins:
    - { name: 'ruby-build',
        repo: 'https://github.com/rbenv/ruby-build.git',
        version: master }
    - { name: 'rbenv-default-gems',
        repo: 'https://github.com/rbenv/rbenv-default-gems.git',
        version: master }
    

    The included plugin rbenv-default-gems will add bundler by default and into the right directory during the installation process of the version of ruby you will have spcecified.

    Then make sure bundler is in PATH.

    That's it.

    0 讨论(0)
  • 2020-12-14 01:14

    Since Ansible 1.3 following native solution is possible:

    - name: requirements for installing gems
      apt:
        name: {{ item }}
      with_items:
        - ruby
        - ruby-dev
        - make
    
    - name: install gem with proper $PATH
      gem:
        name: xyz
        user_install: no
    

    Mention the user_install parameter! Additionally some dependecies installed by the bundler could need following further package dependencies:

    • zlib1g-dev
    0 讨论(0)
  • 2020-12-14 01:21

    I've met the similar environment issue when I tried to run commands as another user. As mentioned in this feature request you have two options to execute your command in login shell (that will load user environment). For example i'ld like to install bundler as rails user:

    - name: Install Bundler
      shell: gem install bundler
      sudo_user: rails -i
    

    or

    - name: Install Bundler
      command: sudo -iu rails gem install bundler
    
    0 讨论(0)
  • 2020-12-14 01:22

    I got it working like this:

    - name: Install jekyll and bundler                                                                                                                      
      become_user: bob                                                                                                                    
      gem:                                                                                                                                                  
        name: "{{ item }}"                                                                                                                                  
      environment:                                                                                                                                          
        GEM_HOME: /home/bob/gems                                                                                                          
        PATH: $PATH:/bin/:/usr/bin/:/home/bob/gems/bin                                                                                    
      with_items:                                                                                                                                           
        - jekyll                                                                                                                                            
        - bundler 
    

    Replace bob with your local user.

    And then use the same principle with the bundler module

    - name: Install Gems                                                                                                                                    
      become_user: bob                                                                                                                     
      bundler:                                                                                                                                              
        gemfile: /home/bob/Gemfile                                                                                          
        state: present                                                                                                                                      
      environment:                                                                                                                                          
        GEM_HOME: /home/bob/gems                                                                                                           
        PATH: $PATH:/bin/:/usr/bin/:/home/bob/gems/bin
    
    0 讨论(0)
提交回复
热议问题