chef-client インストールなど

24 10月

Chef-client
1. RBEL repoのインストール

# wget http://rbel.frameos.org/rbel6
# rpm -Uvh http://rbel.frameos.org/rbel6

2. Chef clientと依存モジュールのインストール
# yum install rubygem-chef

3. firewallの停止
# service iptables stop

4 カギファイル同期
# scp /etc/chef/validation.pem サーバ:/etc/chef/
# vi /etc/chef/client.rb
chef_server_url ‘http://サーバ:4000’
node_name ‘sl61dev’
# chef-client

knife(コマンドライン操作ツール)
knifeの初期化
# knife configure
WARNING: No knife configuration file found
Where should I put the config file? [~/.chef/knife.rb]
Please enter the chef server URL: [http://servername:4000] http://servername:4000
Please enter an existing username or clientname for the API: [root] sl61dev
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef/validation.pem]
Please enter the path to a chef repository (or leave blank):
# ln -s /etc/chef/client.pem /root/.chef/sl61dev.pem

設定の確認
# knife node list
sl61dev

Chefのディレクトリ一式を作成する

Opscodeのgithubからディレクトリのひな形をダウンロードする。
# mkdir chef
# cd chef
# wget –no-check-certificate -O chef-repo.tar.gz http://github.com/opscode/chef-repo/tarball/master
# tar zxf chef-repo.tar.gz
# mv opscode-chef-repo-a3bec38 chef-repo

Cookbookを作る

knifeコマンドで、sampleという名前のcookbookを作成する。

# knife cookbook create sample -o ~/chef/chef-repo/cookbooks
** Creating cookbook sample
** Creating README for cookbook: sample
** Creating metadata for cookbook: sample
# ls chef-repo/cookbooks/sample
README.md attributes/ definitions/ files/ libraries/ metadata.rb providers/ recipes/ resources/ templates/

Recipeの作成~サーバへの登録

動作を確かめるために、実際にRecipeを書いてみる。

# vi chef-repo/cookbooks/sample/recipes/default.rb
template “/tmp/sl61.txt” do
source “sl61.txt.erb”
mode 0644
end
# vi chef-repo/cookbooks/sample/templates/default/sl61.txt.erb
Welcome to Chef!

CPU :
Memory:
OS :

Chef-serverへ登録してみる。
cookbookのuploadをする際に、クライアントがadmin権限を持っていないとauthorizeされないので、事前にadmin権限を付与しておく。

# knife cookbook upload -a -o ~/chef/chef-repo/cookbooks/

Cookbookをnodeに紐付ける。

# knife node run_list add sl61 ‘recipe[sample]’

クライアントに反映する

先ほど登録して紐付けたcookbookを実際にクライアントに反映をする。

# chef-client
# cat /tmp/sl61.txt
Welcome to Chef!

CPU :QEMU Virtual CPU version 0.9.1
Memory:1019852kB
OS :scientific 6.1

エラーが何も出なければOK。

Attributeを試す

Attributeはデフォルト値や共通値、ノード固有の値を定義するためのもの。

# vi chef-repo/cookbooks/sample/templates/default/sl61.txt.erb
Welcome to Chef!

CPU :
Memory:
OS :
Memo :

# cat chef-repo/cookbooks/sample/attributes/default.rb
default[:memo] = “None.”
# knife cookbook upload -a -o ~/chef/chef-repo/cookbooks/
# chef-client
# cat /tmp/sl61.txt
Welcome to Chef!

CPU :QEMU Virtual CPU version 0.9.1
Memory:1019852kB
OS :scientific 6.1
Memo :None.

このノードにはmemoというattributeは設定されていないので、
defaultで設定した「None.」が表示される。
そこで、このノードのattributeを設定して見る。

# EDITOR=vi knife node edit sl61
{
“normal”: {
“tags”: [
],
“memo”: “This is a sl61 for chef test.”
},
“name”: “sl61”,
“chef_environment”: “_default”,
“run_list”: [
“recipe[sample]”
]
}

# chef-client
# cat /tmp/sl61.txt
Welcome to Chef!

CPU :QEMU Virtual CPU version 0.9.1
Memory:1019852kB
OS :scientific 6.1
Memo :None.
Memo :This is a sl61 for chef test.

コメントを残す