LoginSignup
10
8

More than 5 years have passed since last update.

ChefでCentOS6とCentOS7を判定して条件分岐する方法

Last updated at Posted at 2015-05-14

おさらい

OSのタイプやバージョン番号を取得するには、

  • node[:platform_family]
  • node[:platform_version]

などの値を参照すればよいです。

これらの情報はohaiが収集してくれたものです。

まず適当なレシピを作ってこれを確かめてみるとよいでしょう。

バージョン情報を取得するレシピ

default.rb
log ":platform_family = " + node[:platform_family]
log ":platform_version = " + node[:platform_version]
log ":platform_version.to_i = " + node[:platform_version].to_i.to_s

実行結果

CentOS 6.6

  * log[:platform_family = rhel] action write

  * log[:platform_version = 6.6] action write

  * log[:platform_version.to_i = 6] action write

CentOS 7.1

  * log[:platform_family = rhel] action write

  * log[:platform_version = 7.1.1503] action write

  * log[:platform_version.to_i = 7] action write

CentOS7からバージョン番号にパラダイム変化が起きているのが要注意点です。(1503はUbuntu的な年月のアレか?)

メジャーバージョンで分岐させる方法

バージョン番号をto_iで整数に変換すると、分岐しやすくなります。

default.rb
if platform_family?('rhel') && node['platform_version'].to_i == 7 then
  log "this is rhel 7"
elsif platform_family?('rhel') && node['platform_version'].to_i == 6 then
  log "this is rhel 6"
end

これでうまく行きました!

10
8
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
10
8