rubyは2.2.0を使用。
Gemfileで必要なgemをインストールする
gem "pry"
gem "json" ,'1.8.2'
gem "chef" , '12.0.3'
gem "knife-zero"
gem "berkshelf"
インストールが完了したら、実際にクックブックを作成していく。
下記を想定する
前提
・CentOS6.6サーバを用意。IPアドレスとネームサーバのみ設定済み
目標
・SELinuxはOFFにする
・iptablesは、80番ポートと22番ポート、およびICMPのみ受け付ける
・nginxをインストールする
・serverspecでプロセス、リッスンポートの確認をする
対象サーバにChef Clientのインストール
bundle exec knife zero bootstrap 10.3.2.201
2つファイル(サーバ情報を記載したjson)ができる
[rails@zackey chef-zero]$ ll nodes/rp.local.json
-rw-rw-r-- 1 rails rails 81810 2月 19 20:52 2015 nodes/rp.local.json
[rails@zackey chef-zero]$ ll clients/rp.local.json
-rw-rw-r-- 1 rails rails 514 2月 19 20:52 2015 clients/rp.local.json
適応したいレシピを追加する(run_listへの追加)
bundle exec knife node run_list add rp.local iptables
bundle exec knife node run_list add rp.local nginx
bundle exec knife node run_list add rp.local selinux::disabled #SELINUXをOFFにしたい
レシピの編集
iptables/recipes/default.rb
task:テンプレートの順番は関係あるのか??
if node["iptables"]["install_rules"]
iptables_rule "prefix"
iptables_rule "all_established"
iptables_rule "all_icmp"
iptables_rule "ssh" #追加
iptables_rule "http" #追加
iptables_rule "postfix"
end
追加したテンプレートは
cookbooks/ptables/templates/default/ssh.erb
-A FWR -p tcp -m tcp --dport 22 -j ACCEPT
cookbooks/ptables/templates/default/http.erb
-A FWR -p tcp -m tcp --dport 80 -j ACCEPT
レシピの適用
bundle exec knife zero chef_client 'name:rp.local' --attribute ipaddress
serverspecによる確認
#setup serverspec env
gem "serverspec"
bundle install後、初期設定
[rails@zackey serverspec]$ bundle exec serverspec-init
Select OS type:
1) UN*X
2) Windows
Select number: 1
Select a backend type:
1) SSH
2) Exec (local)
Select number: 1
Vagrant instance y/n: n
Input target host name: rp.local
+ spec/
+ spec/rp.com/
+ spec/rp.com/sample_spec.rb
+ spec/spec_helper.rb
+ Rakefile
+ .rspec
rp.localはhostsで名前解決できるようにしておく。
また.sshの設定で下記を追加
task: rootユーザでよいのか。(ssh時何ユーザでアクセスするのか)
[rails@zackey serverspec]$ cat ~/.ssh/config
Host rp.local
HostName rp.local
Port 22
User root
今回は公開鍵認証じゃないため、export ASK_SUDO_PASSWORD=1をいれてパスワードを対話形式でいれる
あとは実行
bundle exec rake spec
Package "nginx"
should be installed
Service "nginx"
should be enabled
should be running
Port "80"
should be listening
Finished in 0.23661 seconds (files took 5.12 seconds to load)
4 examples, 0 failures