KnifeSoloを使ってサーバー構築

ChefServerChefSoloに続いて、KnifeSoloを使ってみました。

サーバーの準備

例によって、Vagrantで環境を作ります。

Vagrantfile作成

$ vim Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define :centos2 do |dev|
    dev.vm.box               = "chef/centos-6.5"
    dev.vm.hostname          = "192.168.1.111"
    dev.vm.network :public_network, ip: "192.168.1.111"
  end
  config.vm.define :centos3 do |dev|
    dev.vm.box               = "chef/centos-6.5"
    dev.vm.hostname          = "192.168.1.110"
    dev.vm.network :public_network, ip: "192.168.1.110"
  end
end

構築

$ vagrant up centos2 centos3

ログイン

$ vagrant ssh centos2

KinfeSoloのインストール

rubyのインストール

$ curl -sSL https://get.rvm.io | bash -s stable
$ source .profile
$ sudo yum install libyaml-devel
$ rvm install ruby-2.1.1
$ source .rvm/scripts/rvm
$ rvm use ruby-2.1.1
$ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
$ gem -v
2.2.2

KnifeSoloをインストー/ル

$ gem install knife-solo

レポジトリの作成

$ knife solo init chef-repo
$ cd chef-repo
$ knife cookbook create sample -o cookbooks

クライアントにchef-soloをインストール

$ knife solo prepare vagrant@192.168.1.110 --omnibus-options insecure

※ エラーになったのでrsync入れました。
sudo yum install rsync

レシピの作成

$ vi cookbooks/sample/recipes/default.rb
package "httpd" do
 action :install
end
service "httpd" do
 action [ :enable, :start ]
end
cookbook_file "/var/www/html/index.html" do
 source "index.html"
 mode "0644"
end
 
$ vi cookbooks/sample/files/default/index.html
<html>
<body>
 <h1>Hello, world!</h1>
</body>
</html>
 
# nodes/ホスト名.json
$ vi nodes/192.168.1.110.json
{"run_list":["recipe[sample]"]}

クライアントにレシピをインストール

[vagrant@192 chef-repo]$ knife solo cook vagrant@192.168.1.110
Running Chef on 192.168.1.110...
Checking Chef version...
Enter the password for vagrant@192.168.1.110:
Uploading the kitchen...
vagrant@192.168.1.110's password:
vagrant@192.168.1.110's password:
vagrant@192.168.1.110's password:
vagrant@192.168.1.110's password:
vagrant@192.168.1.110's password:
vagrant@192.168.1.110's password:
vagrant@192.168.1.110's password:
vagrant@192.168.1.110's password:
Generating solo config...
vagrant@192.168.1.110's password:
Running Chef...
[2014-05-17T00:53:49+00:00] WARN:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
SSL validation of HTTPS requests is disabled. HTTPS connections are still
encrypted, but chef is not able to detect forged replies or man in the middle
attacks.
To fix this issue add an entry like this to your configuration file:
```
  # Verify all HTTPS connections (recommended)
  ssl_verify_mode :verify_peer
  # OR, Verify only connections to chef-server
  verify_api_cert true
  verify_api_cert true
```
To check your SSL configuration, or troubleshoot errors, you can use the
`knife ssl check` command like so:
```
  knife ssl check -c /home/vagrant/chef-solo/solo.rb
```
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Starting Chef Client, version 11.12.4
Compiling Cookbooks...
Converging 3 resources
Recipe: sample::default
  package[httpd] action install
    - install version 2.2.15-30.el6.centos of package httpd
  * service[httpd] action enable
    - enable service service[httpd]
  * service[httpd] action start
    - start service service[httpd]
  * cookbook_file[/var/www/html/index.html] action create
    - create new file /var/www/html/index.html
    - update content in file /var/www/html/index.html from none to 69c55a
        --- /var/www/html/index.html    2014-05-17 00:54:00.052014612 +0000
        +++ /tmp/.index.html20140517-7453-saxrdh        2014-05-17 00:54:00.079014612 +0000
        @@ -1 +1,6 @@
        +<html>
        +<body>
        + <h1>Hello, world!</h1>
        +</body>
        +</html>
    - change mode from '' to '0644'
    - restore selinux security context
 
Running handlers:
Running handlers complete
Chef Client finished, 4/4 resources updated in 10.961148419 seconds

パスワードきかれすぎ。。、nopass以外でいい方法が知りたいです。。 

ブラウザから確認

http://192.168.1.110

 

f:id:katashiyo515:20140517103909j:plain

参考URL

http://matschaffer.github.io/knife-solo/

 

トラブルシューティング

■エラー1
[vagrant@192 chef-repo]$ knife solo cook vagrant@192.168.1.110
Running Chef on 192.168.1.110...
Checking Chef version...
Enter the password for vagrant@192.168.1.110:
Uploading the kitchen...
ERROR: RuntimeError: Failed to launch command ["rsync""-rL""--rsh=ssh vagrant@192.168.1.110""--delete""--exclude=revision-deploys""--exclude=tmp""--exclude=.git""--exclude=.hg""--exclude=.svn""--exclude=.bzr""/home/vagrant/.rvm/gems/ruby-2.1.1/gems/knife-solo-0.4.1/lib/knife-solo/resources/patch_cookbooks/"":~/chef-solo/cookbooks-1"]
 
→ rsyncコマンドがなかったのでいれる
→ $ sudo yum install rsync
 
 
■エラー2
$ knife solo prepare vagrant@192.168.1.110
trying wget...
ERROR 404
Unable to retrieve a valid package!
→とりあえず最新版にしてみる
→$ knife solo prepare vagrant@192.168.1.110 --omnibus-options insecure
→なぜかうまくいった