fedora 16 にchefをinstall その4
[fedora][chef]
roleの作成を試す
前回recipeからのインストールができたので今回はroleを作ってインストールを試みてみる
roleを使うとそれぞれ役割ごとにrecipeを組み合わせることができる。
role内ででrecipeパラメータの変更も可能らしい。
server側
roleを作成する
roleを作成するにはRuby DSLやJSONファイルを直接作成することもできるのだが、
どちらもお作法をよくわかっていないので今回はknifeコマンドを使用して作成する。
recipeは公式のgitから落としてきたものを使用。
# knife role create test { "name": "test", "default_attributes": { }, "json_class": "Chef::Role", "env_run_lists": { }, "run_list": [ ], "description": "", "chef_type": "role", "override_attributes": { } }
空のテンプレートが表示されるのでこれを編集する
listen_portに8000番を追加し、apacheとmod_sslをインストールするように指定
{ "name": "test", "default_attributes": { "apache2": { "listen_ports": [ "8000" ] } }, "json_class": "Chef::Role", "env_run_lists": { }, "run_list": [ "recipe[apache2]", "recipe[apache2::mod_ssl]" ], "description": "", "chef_type": "role", "override_attributes": { } }
編集が終わったら保存して終了。
書式等に問題が無ければ以下の様な出力後roleが作成される。
Created role[test]
knife用にediterの環境変数がないと怒られるので設定しておくこと。
確認
# knife role list test
作成したroleをサーバに登録する
# knife node run_list add [nodename] "role[test]" run_list: role[test]
ノード名を指定しないといけないので注意
client側
jsonファイルの作成
今までと同じようにjsonファイルを修正
中身はrecipeからrun_listに変更している。
# vim /root/.chef/test-role.json { "run_list": [ "role[test]" ] }
実行
# chef-client -j /root/.chef/test-role.json [Tue, 13 Dec 2011 16:01:03 +0900] INFO: *** Chef 0.10.4 *** [Tue, 13 Dec 2011 16:01:04 +0900] INFO: Setting the run_list to ["role[test]"] from JSON [Tue, 13 Dec 2011 16:01:04 +0900] INFO: Run List is [role[test]] [Tue, 13 Dec 2011 16:01:04 +0900] INFO: Run List expands to [apache2, apache2::mod_ssl] [Tue, 13 Dec 2011 16:01:04 +0900] INFO: Starting Chef Run for SL6 省略 [Tue, 13 Dec 2011 16:01:32 +0900] INFO: Processing service[apache2] action restart (apache2::default line 210) [Tue, 13 Dec 2011 16:01:35 +0900] INFO: service[apache2] restarted [Tue, 13 Dec 2011 16:01:35 +0900] INFO: Chef Run complete in 31.103553 seconds [Tue, 13 Dec 2011 16:01:35 +0900] INFO: Running report handlers [Tue, 13 Dec 2011 16:01:35 +0900] INFO: Report handlers complete
確認
# netstat -anp |grep httpd tcp 0 0 :::80 :::* LISTEN 506/httpd tcp 0 0 :::443 :::* LISTEN 506/httpd tcp 0 0 :::8000 :::* LISTEN 506/httpd
8000番ポートが追加されている。
該当のファイルも見てみる
# vim /etc/httpd/ports.conf #This file generated via template by Chef. Listen 80 NameVirtualHost *:8000 Listen 443 NameVirtualHost *:443 Listen 8000 NameVirtualHost *:8000
SSLも入った。
# rpm -qa |grep mod_ssl mod_ssl-2.2.15-9.sl6.3.x86_64 # ls /etc/httpd/modules/ |grep ssl mod_ssl.so
値の変更を試す
今度はoverride_attributesに変更したいポートを指定した。
# knife role edit test 該当箇所抜粋 "override_attributes": { "apache": { "listen_ports": [ "8000", "443" ]
あとは同じようにuploadしてクライアントから実行
# netstat -anp |grep httpd tcp 0 0 :::443 :::* LISTEN 506/httpd tcp 0 0 :::8000 :::* LISTEN 506/httpd
# vim /etc/httpd/ports.conf #This file generated via template by Chef. Listen 8000 NameVirtualHost *:8000 Listen 443 NameVirtualHost *:443
"override_attributes"で指定した値に変更された。
同じrecipeを使いつつ役割に応じて微妙に構成を変えたい時などもroleを使えば対応できそうだ。
変更箇所の関係性
今回は
"default_attributes"と"override_attributes"で
"apache"recipeの"listen_ports"を変更した。
- /var/chef/cookbooks/apache2/attributes/default.rb
の中にある
- default[:apache][:listen_ports] = [ "80","443" ]
がこのroleの指定で一時的に変更され
- /var/chef/cookbooks/apache2/recipes/default.rb
のports.confを生成する部分で適用されている。
template "#{node[:apache][:dir]}/ports.conf" do source "ports.conf.erb" owner "root" group "root" variables :apache_listen_ports => node[:apache][:listen_ports].map{|p| p.to_i}.uniq mode 0644 notifies :restart, resources(:service => "apache2") end
"default_attributes"の[]で括っている部分は配列として格納されているようなので既存の値に追加されている模様。
参考:http://wiki.opscode.com/display/chef/Roles#Roles-runlist
http://wiki.opscode.com/display/chef/Attributes