Install RVM with ansible

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 }}"

This worked for me (Ubuntu):

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

Using a regular (non-root) user.


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


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

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