LoginSignup
89
84

More than 5 years have passed since last update.

Ansibleでシェル設定の再読み込みが必要になった場合の対応

Last updated at Posted at 2014-10-25

Ansibleでシェルの設定ファイル(以下、bashを使用する前提で記述する)に設定の追加が必要なソフトウェア(rbenvやnodebrew, VirtualenvWrapperなど)をインストールする場合、以下のように書いても上手く動作しない。

    # ~/.bashrc(~/.bash_profile) にrbenvの設定を追記する
    - name: Set rbenv config
      copy: src=files/bashrc dest=~/.bashrc
    # ~/.bashrcに書き込んだ設定を読み込む
    - name: Load rbenv setting
      shell: source ~/.bashrc executable=/bin/bash
    - name: Install ruby 2.1.2
      shell: rbenv install 2.1.2 && rbenv rehash && rbenv global 2.1.2 executable=/bin/bash

上記の例ではrbenv installを実行する時点でsource ~/.bashrcの結果が反映されていない(別シェルで起動)ため、rbenvコマンドが見えずにエラーとなる。

*同様の内容の記事では、.bash_profileではなく.bashrc(bash起動時に毎回読み込み)に必要な設定を書き込むことで上手くいったとの事だが、自身の環境では.bashrcに書き込んだ際も上手くいかなかった。
http://qiita.com/volanja/items/3a263ab8df34af6870d5

bashの設定をコマンド実行時に反映させたい場合、lオプションによりログインシェルとして起動する必要がある。

    # ~/.bashrc(~/.bash_profile) にrbenvの設定を追記する
    - name: Set rbenv config
      copy: src=files/bashrc dest=~/.bashrc
    # lオプションによりログインシェルとしてbashを起動する
    - name: Install ruby 2.1.2
      shell: /bin/bash -lc "rbenv install 2.1.2 && rbenv rehash && rbenv global 2.1.2"

(ref.) https://github.com/zzet/ansible-rbenv-role/blob/master/tasks/main.yml#L116

89
84
1

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
89
84