LoginSignup
2
2

More than 5 years have passed since last update.

Chefのrecipeのlibraryで他のgemを使用したいとき

Last updated at Posted at 2016-10-17

標準ではないgemを使用したいときはchef_gemリソースでgemをインストールすれば各レシピで使用できます。各レシピで共通で使用するメソッドはlibraryディレクトリにrubyスクリプトファイルを置けば使用できます。標準ではないgemをlibrary配下のrubyスクリプトで使おうとすると。。。

前提条件

Chefバージョン: 12

うまく動かない例

recipe/default.rb
chef_gem 'activesupport' do
  version "4.2.5.1"
end
libraries/helper.rb
require 'active_support/core_ext/string/inflections'

module Helper
  def humanize_name(name)
    name.humanize
  end
end

エラーが発生します

LoadError
---------
cannot load such file -- active_support/core_ext/string/inflections

うまく動く例

Chefはrecipeを実行する前にライブラリをロードしようとするので、methodの中でrequireしてやると遅延評価されてうまくいきます。

recipe/default.rb
chef_gem 'activesupport' do
  version "4.2.5.1"
end
libraries/helper.rb
module Helper
  def name(name)
    require 'active_support/core_ext/string/inflections'
    name.humanize
  end
end

参考元

ここまでお読みくださってありがとうございました。
ご指摘お待ちしております。

2
2
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
2
2