Skip to content

Instantly share code, notes, and snippets.

@leucos
Last active December 11, 2015 10:28
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leucos/4586964 to your computer and use it in GitHub Desktop.
Save leucos/4586964 to your computer and use it in GitHub Desktop.
Ruby playbook example

Deploying Ruby with Ansible

Define these variables somewhere and let it go :

  • ruby_current: the ruby version you want to deploy (e.g. "1.9.3-p374")
  • ruby_user: Under which account we want ruby to be installed (e.g. "myappuser"; "ruby" in this playbook)

Note : this playbook only works with '-i' in the sudo line since it requires the target user environment to be fully loaded

---
- hosts: ubuntu
name: Setting up environment
tasks:
- include: tasks/setup.yml
- hosts: ubuntu
name: Install for ruby user
sudo: True
sudo_user: ruby
# should be $ruby_user, but can't for now because of #1665
tasks:
- include: tasks/install.yml
---
- name: Installs rbenv
action: git repo=git://github.com/sstephenson/rbenv.git dest=/home/$ruby_user/.rbenv
tags:
- ruby
- name: Creates plugin directory
action: file path=~$ruby_user/.rbenv/plugins/ owner=$ruby_user group=$ruby_user mode=0755 state=directory
tags:
- ruby
- name: Installs ruby-build
action: git repo=git://github.com/sstephenson/ruby-build.git dest=~$ruby_user/.rbenv/plugins/ruby-build
tags:
- ruby
# This implies you use ~/.bash.d/ construct (see bash playbook)
# Change accordingly
- name: Adds bash rbenv stuff for user $item
action: template src=templates/rbenv.bash dest=~$ruby_user/.bash.d/
tags:
- bash
- ruby
- name: Gets current ruby version
action: shell rbenv version | cut -f1 -d" "
register: ruby_current_version
tags:
- ruby
- name: Displays global rbenv version
action: shell echo ${ruby_current_version.stdout}
- name: Installs ruby
action: shell rbenv install $ruby_current
only_if: "'${ruby_current_version.stdout}' != '$ruby_current'"
# Takes no more than 600 secs on a small VM
async: 600
poll: 30
tags:
- ruby
- name: Displays global rbenv version again
action: shell echo ${ruby_current_version.stdout}
tags:
- ruby
- name: Sets global rbenv version
action: shell rbenv global $ruby_current
only_if: "'${ruby_current_version.stdout}' != '$ruby_current'"
tags:
- ruby
---
- name: Installs ruby building dependencies
action: apt name=$item state=installed update-cache=yes
with_items: $ruby_deps
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
@bct
Copy link

bct commented Jan 22, 2013

Where is $ruby_deps supposed to be set?

@leucos
Copy link
Author

leucos commented Jan 23, 2013

In config vars somewhere. I have this in my group_vars for ubuntu machines :

ruby_deps:
- autoconf
- automake
- bison
- build-essential
- curl
- exuberant-ctags
- git-core
- libreadline6
- libreadline6-dev
- libreadline-dev
- libsqlite3-0
- libsqlite3-dev
- libssl-dev
- libncurses5-dev
- libtool
- libxml2-dev
- libxslt1-dev
- openssl
- sqlite3
- subversion
- zlib1g
- zlib1g-dev

YMMV dependng on target OS.

@billwanjohi
Copy link

This is great, but how do you actually invoke this with sudo -i?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment