アーカイブ | chef RSS feed for this section

chef Roleの作成

25 10月

役割を登録するもの。

複数の Recipeを含むことがnodeと同じように Attributeを含むことも出来る。

Roleの作成
# EDITOR=vi knife role create sample-role
{
“chef_type”: “role”,
“name”: “sample-role”,
“override_attributes”: {
},
“json_class”: “Chef::Role”,
“run_list”: [
“recipe[sample]” ->ここ追加
],
“env_run_lists”: {
},
“default_attributes”: {
},
“description”: “”
}

作成した role を node に適用、
# knife node run_list add sl61dev “role[sample-role]”
run_list:
recipe[sample]
role[sample-role]

前の Recipe は run_list から外す。
# knife node run_list remove sl61dev recipe[sample]
run_list:
role[sample-role]

確認
# knife node show sl61dev

実行確認
# chef-clinet
今までの内容が出力されていればOK

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.

chef-serverのインストール

24 10月

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

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

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

3. firewallの停止c
# service iptables stop

4. セットアップスクリプトの実行
# /usr/sbin/setup-chef-server.sh

5 Chef-web-uiにログイン
# /etc/init.d/chef-server-webui restart
設定ファイルは
cat /etc/chef/webui.rb 参照

knifeコマンド用としてユーザを作成し、clientを登録
# mkdir ~tokyox2/.chef
# cp /etc/chef/validation.pem /etc/chef/webui.pem ~tokyox2/.chef/
# chown -R tokyox2:tokyox2 ~tokyox2/.chef/
# su – tokyox2
$ knife configure -i
WARNING: No knife configuration file found
Where should I put the config file? [~/.chef/knife.rb]
Please enter the chef server URL: [http://localhost:4000]
Please enter a clientname for the new client: [tokyox2]
Please enter the existing admin clientname: [chef-webui]
Please enter the location of the existing admin client’s private key: [/etc/chef/webui.pem] .chef/webui.pem
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef/validation.pem] .chef/validation.pem
Please enter the path to a chef repository (or leave blank):
Creating initial API user…
Created client[tokyox2]
Configuration file written to /home/tokyox2/.chef/knife.rb

登録に成功していれば knife client list を実行