Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

おさらい

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

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

DQNEO
PHP/Go/Perl/C/Docker/Linux/Git/AWS
http://dqn.sakusakutto.jp
mercari
フリマアプリ「メルカリ」を、グローバルで開発しています。
https://tech.mercari.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした