如何使用Ansible在Ubuntu框上安装Ruby 2和Ruby Gems

时间:2022-09-28 07:15:44

I want to provision Ubuntu Server machine with latest Ruby and Ruby Gems version using Ansible.

我想使用Ansible为Ubuntu Server机器配置最新的Ruby和Ruby Gems版本。

How do I do this?

我该怎么做呢?

2 个解决方案

#1


18  

Solution #1: Using APT and Symlinks

I recommend to use this solution if it's OK to install Ruby 2.0 and Ruby Gems globally (for all users). If you want to install another version or isolate it from other users - please see solution #2.

如果可以全局安装Ruby 2.0和Ruby Gems(对于所有用户),我建议使用此解决方案。如果您想安装其他版本或将其与其他用户隔离 - 请参阅解决方案#2。

Here's a simple Ansible playbook that will install latest Ruby 2.0 and Ruby Gems for you:

这是一个简单的Ansible playbook,它将为您安装最新的Ruby 2.0和Ruby Gems:

Ubuntu 14.04 (Trusty Tahr)

- name: Latest version of Ruby is installed
  apt: pkg={{ item }} state=latest
  with_items:
    - ruby2.0
    - ruby2.0-dev

- name: Symlink exists for Ruby 2.0
  file: src=/usr/bin/ruby2.0 dest=/usr/local/bin/ruby state=link

- name: Symlink exists for Ruby Gems 2.0
  file: src=/usr/bin/gem2.0 dest=/usr/local/bin/gem state=link

Ubuntu 13.10 (Saucy Salamander)

- name: Latest version of Ruby is installed
  apt: pkg={{ item }} state=latest
  with_items:
    - ruby2.0
    - ruby2.0-dev

- name: Making Ruby 2.0 the default one
  command: update-alternatives --set ruby /usr/bin/ruby2.0

- name: Making Gem 2.0 the default one
  command: update-alternatives --set gem /usr/bin/gem2.0

Provided playbooks must be executed with sudo: yes for obvious reasons.

提供的剧本必须用sudo执行:是的,原因很明显。

Solution #2: Using RVM

RVM is installing ruby and it's gems locally for current user. So it leads to a problems in multi-user environment. In my use-case it was not working correctly for some reason. So I would recommend to stick to the first solution if possible. Although, if you know what you are doing and understand the complications here goes the RVM solution.

RVM正在安装ruby,它是本地用户的宝石。因此,它会导致多用户环境中的问题。在我的用例中,由于某种原因它无法正常工作。所以我建议如果可能的话坚持第一个解决方案。虽然,如果您知道自己在做什么,并了解其中的复杂性,那就是RVM解决方案。

I suggest to create a simple shell script to install current version of Ruby and Ruby Gems with RVM and invoke it later on a provisioned machine.

我建议创建一个简单的shell脚本,用RVM安装当前版本的Ruby和Ruby Gems,稍后在配置的机器上调用它。

Here's the Bash script:

这是Bash脚本:

#!/usr/bin/env bash

# Checking if RVM is installed
if ! [ -d "~/.rvm" ]; then
    echo "Installing RVM..."
    \curl -sSL https://get.rvm.io | bash -s stable
    source ~/.rvm/scripts/rvm
    echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
else
    echo "Updating RVM..."
    rvm get stable
fi;

echo -n "RVM version is: "
rvm --version

echo "Installing Ruby..."
rvm install ruby

echo "Making installed Ruby the default one..."
rvm use ruby --default

echo "Installing latest version of Ruby Gems..."
rvm rubygems current

This script will install RVM (or update it to a latest stable version if it's already installed) and it will install latest stable versions of Ruby and Ruby Gems.

这个脚本将安装RVM(或者如果它已经安装,则将其更新为最新的稳定版本),它将安装最新的稳定版本的Ruby和Ruby Gems。

And here's the playbook to copy provided script to the provisioning machine and invoke it:

这是将提供的脚本复制到配置机器并调用它的剧本:

- file: path=~/provision/ruby state=directory
- copy: src=../../files/ruby/install.sh dest=~/provision/ruby/install.sh mode=775

- name: Latest Ruby is installed
  shell: /usr/bin/env bash ~/provision/ruby/install.sh

Just place your script near the Ansible's playbook and update the paths.

只需将脚本放在Ansible的剧本附近并更新路径即可。

I hope it will help someone.

我希望它会帮助某人。

#2


1  

There's now an official Ruby/RVM playbook: https://github.com/rvm/rvm1-ansible

现在有一个官方的Ruby / RVM剧本:https://github.com/rvm/rvm1-ansible

To install: ansible-galaxy install rvm_io.rvm1-ruby -p ROLES_PATH

要安装:ansible-galaxy install rvm_io.rvm1-ruby -p ROLES_PATH

Example playbook:

示例剧本:

---

- name: Configure servers with ruby support
  hosts: all
  roles:
    - { role: rvm_io.rvm1-ruby, tags: ruby, become: True }

#1


18  

Solution #1: Using APT and Symlinks

I recommend to use this solution if it's OK to install Ruby 2.0 and Ruby Gems globally (for all users). If you want to install another version or isolate it from other users - please see solution #2.

如果可以全局安装Ruby 2.0和Ruby Gems(对于所有用户),我建议使用此解决方案。如果您想安装其他版本或将其与其他用户隔离 - 请参阅解决方案#2。

Here's a simple Ansible playbook that will install latest Ruby 2.0 and Ruby Gems for you:

这是一个简单的Ansible playbook,它将为您安装最新的Ruby 2.0和Ruby Gems:

Ubuntu 14.04 (Trusty Tahr)

- name: Latest version of Ruby is installed
  apt: pkg={{ item }} state=latest
  with_items:
    - ruby2.0
    - ruby2.0-dev

- name: Symlink exists for Ruby 2.0
  file: src=/usr/bin/ruby2.0 dest=/usr/local/bin/ruby state=link

- name: Symlink exists for Ruby Gems 2.0
  file: src=/usr/bin/gem2.0 dest=/usr/local/bin/gem state=link

Ubuntu 13.10 (Saucy Salamander)

- name: Latest version of Ruby is installed
  apt: pkg={{ item }} state=latest
  with_items:
    - ruby2.0
    - ruby2.0-dev

- name: Making Ruby 2.0 the default one
  command: update-alternatives --set ruby /usr/bin/ruby2.0

- name: Making Gem 2.0 the default one
  command: update-alternatives --set gem /usr/bin/gem2.0

Provided playbooks must be executed with sudo: yes for obvious reasons.

提供的剧本必须用sudo执行:是的,原因很明显。

Solution #2: Using RVM

RVM is installing ruby and it's gems locally for current user. So it leads to a problems in multi-user environment. In my use-case it was not working correctly for some reason. So I would recommend to stick to the first solution if possible. Although, if you know what you are doing and understand the complications here goes the RVM solution.

RVM正在安装ruby,它是本地用户的宝石。因此,它会导致多用户环境中的问题。在我的用例中,由于某种原因它无法正常工作。所以我建议如果可能的话坚持第一个解决方案。虽然,如果您知道自己在做什么,并了解其中的复杂性,那就是RVM解决方案。

I suggest to create a simple shell script to install current version of Ruby and Ruby Gems with RVM and invoke it later on a provisioned machine.

我建议创建一个简单的shell脚本,用RVM安装当前版本的Ruby和Ruby Gems,稍后在配置的机器上调用它。

Here's the Bash script:

这是Bash脚本:

#!/usr/bin/env bash

# Checking if RVM is installed
if ! [ -d "~/.rvm" ]; then
    echo "Installing RVM..."
    \curl -sSL https://get.rvm.io | bash -s stable
    source ~/.rvm/scripts/rvm
    echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
else
    echo "Updating RVM..."
    rvm get stable
fi;

echo -n "RVM version is: "
rvm --version

echo "Installing Ruby..."
rvm install ruby

echo "Making installed Ruby the default one..."
rvm use ruby --default

echo "Installing latest version of Ruby Gems..."
rvm rubygems current

This script will install RVM (or update it to a latest stable version if it's already installed) and it will install latest stable versions of Ruby and Ruby Gems.

这个脚本将安装RVM(或者如果它已经安装,则将其更新为最新的稳定版本),它将安装最新的稳定版本的Ruby和Ruby Gems。

And here's the playbook to copy provided script to the provisioning machine and invoke it:

这是将提供的脚本复制到配置机器并调用它的剧本:

- file: path=~/provision/ruby state=directory
- copy: src=../../files/ruby/install.sh dest=~/provision/ruby/install.sh mode=775

- name: Latest Ruby is installed
  shell: /usr/bin/env bash ~/provision/ruby/install.sh

Just place your script near the Ansible's playbook and update the paths.

只需将脚本放在Ansible的剧本附近并更新路径即可。

I hope it will help someone.

我希望它会帮助某人。

#2


1  

There's now an official Ruby/RVM playbook: https://github.com/rvm/rvm1-ansible

现在有一个官方的Ruby / RVM剧本:https://github.com/rvm/rvm1-ansible

To install: ansible-galaxy install rvm_io.rvm1-ruby -p ROLES_PATH

要安装:ansible-galaxy install rvm_io.rvm1-ruby -p ROLES_PATH

Example playbook:

示例剧本:

---

- name: Configure servers with ruby support
  hosts: all
  roles:
    - { role: rvm_io.rvm1-ruby, tags: ruby, become: True }