Install RVM with ansible

8,369

Solution 1

RVM & Ruby Installation Playbook

Here's an idempotent playbook that will install RVM, a specific version of Ruby (set the version with the ruby_version var) and set that version of Ruby to be the default:

---

- hosts: all
  sudo: yes
  vars:
    ruby_version: "2.1.3"
    rvm_path: "/usr/local/rvm/gems/ruby-{{ ruby_version }}/bin:/usr/local/rvm/gems/ruby-{{ ruby_version }}@global/bin:/usr/local/rvm/rubies/ruby-{{ ruby_version }}/bin:/usr/local/rvm/bin"

  tasks:

  - name: append rvm path to environment
    lineinfile: dest=/etc/environment state=present backrefs=yes regexp='PATH=(["]*)((?!.*?{{rvm_path}}).*?)(["]*)$' line="PATH=\1\2:{{rvm_path}}\3"


  - name: ensure necessary packages are installed
    yum:
      name: "{{ item }}"
      state: present
    with_items:
      - curl
      - gnupg2

  - name: ensure that GPG key for RVM is installed
    command: gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
    args:
      creates: /root/.gnupg/secring.gpg

  - name: ensure that RVM is installed
    shell: curl -L get.rvm.io | bash -s stable
    args:
      creates: /usr/local/rvm

  - name: ensure that ruby is installed
    command: "rvm install {{ ruby_version }}"
    args:
      creates: "/usr/local/rvm/gems/ruby-{{ ruby_version }}"
    environment:
      PATH: "{{ rvm_path }}:{{ ansible_env.PATH }}"

  - name: set default version of ruby with rvm
    command: "rvm alias create default ruby-{{ ruby_version }}"
    args:
      creates: /usr/local/rvm/config/alias
    environment:
      PATH: "{{ rvm_path }}:{{ ansible_env.PATH }}"

Solution 2

This worked for me (Ubuntu):

    tasks:
      - name: Install RVM
        shell: "curl -sSL https://get.rvm.io | bash"

Using a regular (non-root) user.

Solution 3

These days, I believe the recommended way is with RVM's ansible role. There are instructions in that project's README.

Solution 4

Building on @dynex's answer, here's a way to do it a bit more idempotently, by checking for a folder it would normally create.

- stat: path=/etc/profile.d/rvm.sh
  register: rvm_folder

- name: install rvm
  shell: "curl -sSL https://get.rvm.io | bash"
  when: rvm_folder.stat.isdir is not defined

Solution 5

I also tried installing RVM with Ansible. Unfortunately RVM doesn't play nicely with non-interactive shells, because it is a shell script function. I ended up installing rbenv instead (https://github.com/sstephenson/rbenv).

Here is my gist:

https://gist.github.com/brendan-skyrkt/7699067

Share:
8,369

Related videos on Youtube

Toby Hede
Author by

Toby Hede

Updated on September 18, 2022

Comments

  • Toby Hede
    Toby Hede over 1 year

    I am attempting to install rvm with ansible on a centos-based vagrant box.

    The command I am running is:

    vars:
      user: "foo"
    
    - name: install rvm
      action: command sudo -u $user bash /home/$user/rvm-install.sh stable creates=$home/.rvm
    

    It pretty much works BUT Ansible thinks it has failed.

    Ansible output is:

    failed: [127.0.0.1] => {"changed": true, "cmd": ["sudo", "-u", "foo", "bash", "/home/foo/rvm-install.sh", "stable"], "delta": "0:00:21.102322", "end": "2012-10-09 12:33:19.917874", "rc": 1, "start": "2012-10-09 12:32:58.815552"}
    stderr: % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 1081k  100 1081k    0     0  54170      0  0:00:20  0:00:20 --:--:-- 89264
    stdout: Downloading RVM from wayneeseguin branch stable
    
    Installing RVM to /home/foo/.rvm/
        RVM PATH line found in /home/foo/.bashrc /home/foo/.zshenv.
        RVM sourcing line found in /home/foo/.bash_profile /home/foo/.zprofile.
    
    # RVM:  Shell scripts enabling management of multiple ruby environments.
    # RTFM: https://rvm.io/
    # HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)
    # Cheatsheet: http://cheat.errtheblog.com/s/rvm/
    # Screencast: http://screencasts.org/episodes/how-to-use-rvm
    
    # In case of any issues read output of 'rvm requirements' and/or 'rvm notes'
    
    Installation of RVM in /home/foo/.rvm/ is almost complete:
    
      * To start using RVM you need to run `source /home/foo/.rvm/scripts/rvm`
        in all your open shell windows, in rare cases you need to reopen all shell windows.
    
    # root,
    #
    #   Thank you for using RVM!
    #   I sincerely hope that RVM helps to make your life easier and more enjoyable!!!
    #
    # ~Wayne
    
  • dynex
    dynex over 10 years
    Yeah took me a while, but got it to work (non-global, single user). Here's the gist and the blog: gist.github.com/ihassin/7968406. Blog at ihassin.wordpress.com/2013/12/15/…
  • Tony
    Tony over 10 years
    that gist and blog post don't use this and don't use RVM. It says that's intended for a later post.
  • dynex
    dynex over 10 years
    I'm sorry, you'r right - I changed the blog not to use RVM and wanted to do a separate write up for RVM. When I was testing with RVM, the above worked, but not "system-wide" for which you need "shell: "curl -sSL get.rvm.io | sudo bash". Sorry for the confusion and I will remove the link to the gist.
  • dynex
    dynex over 10 years
    Nice :) Ansible is full of hidden gems. Pun intended...
  • anquegi
    anquegi over 8 years
    Thanks for this solution, it works for me, but I cannot install gems from the ansible playbook, it always give me an error. How can I install gems to a gemset from ansible playbook