ようへいの日々精進XP

よかろうもん

Chef のレシピを書く為のマイチートシート(2)

追記

  • 以下の情報は古い、又は誤りがありますのでご注意下さい ...

はじめに

レシピを書く為だけではなくて、Chef を使うにあたって覚えておきたい色々となこと。


Berkshelf をサクッと使う

cookbookbundler 的な位置づけの Berkshelf で迷わない為のメモ。

Berkshelf のインストール

既に Version 3 が出ているようだけど、以下のように gem でインストールする場合には Version 2 系がインストールされる。

gem install berkshelf --no-ri --no-rdoc

簡単。

Berksfile を書く

早速、Berksfile を書いていくわけであるが、個人的に Berksfile の配置をどこにすれば良いのかが迷いどころであるが...

knife solo init chef-repo

を実行すると chef-repo 以下に Berksfile が作成されることから chef-repo 直下に置くのがイイのかなと思っていただけど、こちらGETTING STARTED を読むと Specify your dependencies in a Berksfile in your cookbook’s root とあるので任意の cookbook 以下に置くのが良さそう。

ということで Berksfile は以下のように記述する。

site :opscode

metadata
cookbook 'htpasswd', git: 'https://github.com/Youscribe/htpasswd-cookbook.git'

berks コマンド

berks コマンドのヘルプは以下の通り。

Commands:
  berks apply ENVIRONMENT     # Apply the cookbook version locks from Berksfile.lock to a Chef environment
  berks configure             # Create a new Berkshelf configuration file
  berks contingent COOKBOOK   # List all cookbooks that depend on the given cookbook
  berks cookbook NAME         # Create a skeleton for a new cookbook
  berks help [COMMAND]        # Describe available commands or one specific command
  berks init [PATH]           # Initialize Berkshelf in the given directory
  berks install               # Install the cookbooks specified in the Berksfile
  berks list                  # List all cookbooks (and dependencies) specified in the Berksfile
  berks outdated [COOKBOOKS]  # Show outdated cookbooks (from the community site)
  berks package [COOKBOOK]    # Package a cookbook (and dependencies) as a tarball
  berks shelf SUBCOMMAND      # Interact with the cookbook store
  berks show [COOKBOOK]       # Display name, author, copyright, and dependency information about a cookbook
  berks update [COOKBOOKS]    # Update the cookbooks (and dependencies) specified in the Berksfile
  berks upload [COOKBOOKS]    # Upload the cookbook specified in the Berksfile to the Chef Server
  berks version               # Display version and copyright information

Options:
  -c, [--config=PATH]    # Path to Berkshelf configuration to use.
  -F, [--format=FORMAT]  # Output format to use.
                         # Default: human
  -q, [--quiet]          # Silence all informational output.
  -d, [--debug]          # Output debug information

かなり色々と出来そうではあるが、今回は berks install のみを利用する。

berks install

Berksfile に記述した cookbook をインストールする為に berks install を利用する。

cd chef-repo/site-cookbooks/
berks install --path=../../cookbooks/

以下のように chef-repo 以下の cookbookshtpasswd という cookbook がインストールされた。

.
├── chef-repo
└── htpasswd

2 directories, 0 files

htpasswd を実現する

community cookbook を使う

  • こちらの community cookbook を使う
  • 上述の Berkshelf を使ってインストールするとイイ

レシピ例

例として nagios をセットアップする場合に... run_list を以下のようにして...

{
"run_list":[
  "recipe[htpasswd]",
  "recipe[nagios_cookbook::app_install]"
  ]
}

レシピは your_recipe.rb に以下のようにして書くと利用することが出来る。

htpasswd "/etc/httpd/htpasswd.users" do
  user "user"
  password "password"
end

とするだけ。また、以下のように ${your_cookbook}/attribute/default.rb に記述して...

default[:nagios][:users] = [{ user: "foo", password: "bar" }]
default[:nagios][:users] = [{ user: "admin", password: "bar" }]

レシピ側を以下のように書けば複数のアカウントを管理することも出来る。

node[:nagios][:users].each do |user|
  htpasswd "/etc/nagios3/htpasswd.users" do
    user user[:user]
    password user[:password]
  end
end

引き続き...

  • 書く