6

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

7 Answers 7

5

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 }}"
1
  • 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
    – anquegi
    Oct 28, 2015 at 22:41
4

This worked for me (Ubuntu):

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

Using a regular (non-root) user.

2
  • that gist and blog post don't use this and don't use RVM. It says that's intended for a later post.
    – mahemoff
    Jan 9, 2014 at 0:34
  • 2
    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
    Jan 9, 2014 at 19:00
3

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

2

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
1
  • 2
    Nice :) Ansible is full of hidden gems. Pun intended...
    – dynex
    Jan 13, 2014 at 0:57
1

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

1
0

It is possibly because the response code from the script is non-zero?

"rc": 1

0

Because rvm does not play nice with non interactive shells if you still want to use rvm with ansible, you have to write your own scripts that call rvm but start with bash -l (a login shell):

- name: install ruby-1.9.3
    script: scripts/install-ruby-1.9.3.sh

Where install-ruby-1.9.3.sh contains something like

#!/bin/bash -l
rvm install 1.9.3

Try to keep such scripts small and focused to one task only (plus handling $? for the main command if there is an exit value other than 0 which you want to accept). For a second ruby use a second script that encapsulates the second command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .