LoginSignup
0
0

More than 5 years have passed since last update.

Chef ohai の SoftLayerプラグインを利用したい

Last updated at Posted at 2015-10-16

モチベーション

Chefクライアントをインストールすると Ohai も導入されてくる。このOhaiで SoftLayer の パブリックIPとプライベートIPを取得したい。しかし、現在使っているKnife のバージョンでは、SoftLayerのプラグインが無い Ohai となっている。

Chef solo で利用されるバージョン

現在利用しているChefのバージョンでは、SoftLayerのプラグインは利用できるかを確認する。 Chefクライアントを導入したサーバーで、以下のコマンドを実行する。

[root@server1 ~]# ohai |less

ohaiコマンドの結果は、このバージョン以下の8.5.1と,少し古い

"chef_packages": {
  "ohai": {
    "version": "8.5.1",
    "ohai_root": "/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.1/lib/ohai"
  },
  "chef": {
    "version": "12.4.3",
    "chef_root": "/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/chef-12.4.3/lib"
  }
},

このバージョンのOhai にプラグインが存在するかを調べてみるが、上記の結果が指すディレクトリに、softlayer.rb は残念ながら無い(泣)

[root@server1 plugins]# pwd
/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.1/lib/ohai/plugins
[root@server1 plugins]# ls -R |grep softlayer
[root@server1 plugins]# 

find で 広範囲に探してみるが、結果は以下のとおり

[root@server1 ~]# find / -name softlayer.rb -print
[root@server1 ~]# 

Chef/Ohai の GitHub バージョン

このURLにあるとおり、SoftLayerのプラグインは、Ohai 8.7 では既に存在している。
https://github.com/chef/ohai/blob/master/lib/ohai/plugins/softlayer.rb

それから、クラウドを判定するコードはどうなっているか調べると、以下の様に追加されている。
https://github.com/chef/ohai/blob/master/lib/ohai/plugins/cloud.rb

cloud.rb抜粋
Ohai.plugin(:Cloud) do
  provides "cloud"

  depends "ec2"
  depends "gce"
  depends "rackspace"
  depends "eucalyptus"
  depends "linode"
  depends "openstack"
  depends "azure"
  depends "digital_ocean"
  depends "softlayer"

このプラグインによって、プライベートのIPとパブリックのIPが簡単に取れる様になる

cloud.rb続き
  # ----------------------------------------
  # softlayer
  # ----------------------------------------

  # Is current cloud softlayer?
  #
  # === Return
  # true:: If softlayer Hash is defined
  # false:: Otherwise
  def on_softlayer?
    softlayer != nil
  end

  # Fill cloud hash with softlayer values
  def get_softlayer_values
    cloud[:public_ipv4] = softlayer['public_ipv4']
    cloud[:local_ipv4] = softlayer['local_ipv4']
    cloud[:public_ips] << softlayer['public_ipv4'] if softlayer['public_ipv4']
    cloud[:private_ips] << softlayer['local_ipv4'] if softlayer['local_ipv4']
    cloud[:public_hostname] = softlayer['public_fqdn']
    cloud[:provider] = 'softlayer'
  end

2015/10/19日現在で Chefをインストールすると

以下の様に、Ohaiのバージョンは、SoftLayerのプラグインが入っている ohai 8.7.0 が導入されます。

chef@chefserver:~/chef-repo$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies..................................................
Using rake 10.4.2
Installing addressable 2.3.8
中略
Installing ohai 8.7.0   <--  softlayer.rbの入ったバージョン
中略
Installing knife-windows 1.1.0
Installing knife-softlayer 0.4.0
Installing knife-solo 0.5.1
Installing knife-zero 1.9.0
Using bundler 1.10.6
Bundle complete! 5 Gemfile dependencies, 95 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
Post-install message from knife-solo:

検索した結果、確かに入っている事が確認できました。

chef@chefserver:~$ find . -name softlayer.rb -print
./.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/fog-softlayer-0.3.30/lib/fog/softlayer.rb
./.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/knife-softlayer-0.4.0/lib/chef/knife/softlayer.rb
./.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/ohai-8.7.0/lib/ohai/plugins/softlayer.rb

Ohai が SoftLayer を判定するための metaデータ

ohai 8.7が利用できる様になったとして、以下の設定を追加する必要がありそうだ。
Enabling EC2 Metadata in ohai

他のコードも無いか調べてみると、
https://github.com/coderanger/ohai-softlayer もあった。

Installs Ohai plugins for SoftLayer cloud metadata.

    softlayer – Ingests metadata from the SoftLayer API.
    cloud_hack – Add SoftLayer data to the cloud and cloud_v2 hashes.

参考資料

http://www.intellilink.co.jp/article/column/devops04.html
https://github.com/chef/ohai
https://docs.chef.io/ohai.html
https://docs.chef.io/ohai_custom.html
http://heartbeats.jp/hbblog/2013/06/use-ohai.html

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