LoginSignup
1
0

More than 5 years have passed since last update.

AnsibleでサーバにRubyをインストールする

Last updated at Posted at 2018-09-13

Ansibleでサーバ(Amazon Linux2)の環境構築をしているのですが、rbenvを使わずにソースからrubyを入れたので、その手順をまとめてみました

インストールするためにやったこと

手順は大まかに次の4つになります

1. Ansible GalaxyからRubyをインストールするロールを探す
2. ansible-galaxy install コマンドでプロジェクトのroleに追加
3. インストールしたいRubyのバージョンなど設定ファイルを書き換え
4. Playbookを実行し、WebサーバのRubyのバージョンを確認する

※ Ansible Galaxyとは

ロールを共有するためのサービスで、Docker HubやChefのSupermarketのようなイメージです

1. Ansible GalaxyからRubyをインストールするロールを探す

Ansible GalaxyでkeywordにRubyと入力し、star数などを確認しながら目的のものを探します
今回はこちら( geerlingguy.ruby)を使ってみることにします

2. ansible-galaxy install コマンドでプロジェクトのroleに追加

プロジェクトのrole配下にインストールしたかったため、こちらのコマンドを実行しました

$ ansible-galaxy install geerlingguy.ruby -p roles/servers/common

するとroles/servers/common/geerlingguy.rubyに作成されました

ansible2.png

3. インストールしたいRubyのバージョンなど設定ファイルを書き換え

Ruby 2.4.2をインストールしたかったため、geerlingguy.ruby/defaults/main.yml ファイルを書き換えます

main.yml
---
workspace: /root

# Whether this role should install Bundler.
ruby_install_bundler: True

# A list of Ruby gems to install.
ruby_install_gems: []

# The user account under which Ruby gems will be installed.
ruby_install_gems_user: "{{ ansible_user }}"

# If set to True, ruby will be installed from source, using the version set with
# the 'ruby_version' variable instead of using a package.
ruby_install_from_source: True
ruby_download_url: http://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.gz
ruby_version: 2.4.2

# Default should usually work, but this will be overridden on Ubuntu 14.04.
ruby_rubygems_package_name: rubygems
※注意点

Playbookのgather_factsをtrueにしておかないと、 "{{ ansible_user }}" が取得できないです
falseにする場合は、"{{ ansible_user }}" を書き換える必要があります

main.yml

省略

# The user account under which Ruby gems will be installed.
ruby_install_gems_user: "{{ ansible_user }}"

省略

Playbookの一例でこんなファイルだとすると

webserver.yml
- name: webserver
  hosts: web
  gather_facts: true
  remote_user: ec2-user
  become: yes
  roles:
    - servers/common/geerlingguy.ruby

4. Playbookを実行し、WebサーバのRubyのバージョンを確認する

サーバにsshし、Rubyのバージョンを確認してみます
ansible1.png
無事Ruby 2.4.2がインストールされました

参考サイト

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0