突然ですがChefの勉強を始めました。
ネットで調べてみようと思ったのですがQiitaみたく断片的なTipsであればたくさん記事があるのですが、なにせ右も左も分からないので体系的に書かれているサイトがなくて困りました。
そこで調べてみたところ猛烈にプッシュされていたのがこの本でした。2年前の本なので躊躇しましたが他に適切なものがなさそうだったので購入しました。

入門Chef Solo - Infrastructure as Code
- 作者: 伊藤直也
- 出版社/メーカー: 伊藤直也
- 発売日: 2013/03/11
- メディア: Kindle版
- 購入: 16人 クリック: 1,027回
- この商品を含むブログ (19件) を見る
……ということで、入門Chef Soloを片手に読み進めていきます。本記事は僕のためのメモなので雑に書かれています。
あと出版が2013年なので仕方がないのですが情報が古くてそのまま使えないところがあります。どうやって解決したかについてもメモしています。
目次
メモ
読み進めていって詰まった順にメモを書いています。
Vagrantのコマンド
Vagrantを作成する
$ cd hoge $ vagrant init
Vagrantを起動する
Vagrantfile
を編集してから、Vagrantを起動します。
$ vagrant up
Vagrantに接続するために設定する
ssh用のconfigを作成する
$ vagrant ssh-config --host melody
面倒臭い場合はこれ。
$ vagrant ssh-config --host melody >> ~/.ssh/config
Knifeのコマンド
Chefリポジトリを作成する
chef-repo
という名前のChefリポジトリを作成する。
$ knife solo init <chef-repo>
レシピを作成する
Chefリポジトリを作成したら実行したいレシピを作成します。
$ cd chef-repo $ knife cookbook create hello -o site-cookbooks
このコマンドはテンプレートファイルを作成するだけのようなので、最初にディレクトリを移動していないと変な場所にディレクトリが作成されてしまうようです。
cookbookディレクトリはサードパーティ製、site-cookbooskディレクトリには自作のレシピを入れるのが推奨されているようです。
レシピを書く
上で作成したsite-cookbooks/hello/recipes/default.rb
にレシピを書きます。下記はサンプルにあったログの出し方です。
# # Cookbook Name:: hello # Recipe:: default # # Copyright 2016, YOUR_COMPANY_NAME # # All rights reserved - Do Not Redistribute # log "Hello, chef!!"
リモート側でChefを使えるようにする
serverに対してクックブックが実行可能なように準備をするのが下記のコマンドです。
$ knife solo prepare melody
サーバーmelody
でChefが使えるように設定しています。
$ knife solo prepare melody Bootstrapping Chef... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 20022 100 20022 0 0 20788 0 --:--:-- --:--:-- --:--:-- 68102 Getting information for chef stable 12.7.2 for el... downloading https://omnitruck-direct.chef.io/stable/chef/metadata?v=12.7.2&p=el&pv=6&m=x86_64 to file /tmp/install.sh.2588/metadata.txt trying wget... url https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.7.2-1.el6.x86_64.rpm md5 8c3ba2e797fc852fc557b0e7157556cc sha256 6af0eb1c7706fc6a36f74ae9f590135e37e6206f2fe7d5a1760c1e2da1b36068 version 12.7.2downloaded metadata file looks valid... downloading https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.7.2-1.el6.x86_64.rpm to file /tmp/install.sh.2588/chef-12.7.2-1.el6.x86_64.rpm trying wget... Comparing checksum with sha256sum... Installing chef 12.7.2 installing with rpm... 警告: /tmp/install.sh.2588/chef-12.7.2-1.el6.x86_64.rpm: ヘッダ V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY 準備中... ########################################### [100%] 1:chef ########################################### [100%] Thank you for installing Chef! Generating node config 'nodes/melody.json'...
ローカルからリモートに向けてレシピを実行する
serverに対してクックブックを実行していくのが下記のコマンドです。
$ knife solo cook <server>
サーバーmelody
に対してクックブックを実行しています。
$ knife solo cook melody Running Chef on melody... Checking Chef version... Uploading the kitchen... Generating solo config... Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json Starting Chef Client, version 12.7.2 Compiling Cookbooks... Converging 0 resources Running handlers: Running handlers complete
node.jsをインストールするレシピの書き方
$ knife solo init <chef-repo> $ knife cookbook create hello -o site-cookbooks
Berksfile
はNuGetやCocoaPodsみたいなものという認識。
source "https://api.berkshelf.com" site :opscode cookbook 'nodejs'
$ berks vendor cookbooks
cookbooksにインストールされる。
$ knife solo prepare melody
nodes/melody.json
ができる。
{ "run_list": [ "recipe[nodejs]" ], "automatic": { "ipaddress": "melody" } }
$ knife solo cook melody
トラブルシューティング
いくつかトラブルが発生している。
gemでVagrantがインストールできない
Vagrantのインストール自体はできるけれど実際には使えず、boxを追加しようとすると下記のようなエラーが発生します。
$ vagrant box add base http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.3-x86_64-v20130101.box Thanks for wanting to use Vagrant! Unfortunately, this is not the way to install Vagrant anymore. We now make installers for the various operating systems Vagrant supports. Vagrant is no longer distributed as a RubyGem. Please download the latest version for your operating system from the URL below. If you still wish to use the RubyGem version, you can manually install version 1.0.7. Note that the RubyGem version hasn't been updated in over a year and will no longer receive any updates. Prior to installing Vagrant using the installer, make sure you uninstall all your Vagrant gems, since they sometimes conflict. http://www.vagrantup.com If you want to learn more about why we don't distribute using RubyGems anymore, please read this: http://mitchellh.com/abandoning-rubygems
この対策としては、以下のVagrant公式ページから.dmgファイルをダウンロードしてインストールします。
既にgemからインストールしてしまっている場合にはgem uninstall vagrant
でアンインストールしてからインストールするのが良いと思います。
VagrantfileのConfigの書き方が変わっている
Vagrantfile
を編集します。本書では下記のように書いていました。configの書き方が変わったのかテンプレートに合わせて書き直しました。
Vagrant::config.run do |config| config.vm.box = "base" config.vm.network :hostonly, "192.168.50.12" ...
↓
Vagrant.configure(2) do |config| ... config.vm.network "private_network", ip: "192.168.50.12"
EC2 Amazon Linuxでknife solo cook
を実行しようとするとrsyncで失敗する
EC2のAmazon Linuxのインスタンスを作成して、rootで直接ログイン可能な状態にした上で、作成したクックブックを実行しようとしました。
$ knife solo prepare ec2root $ knife solo cook ec2root
下記のようなエラーが出て止まってしまいました。
Uploading the kitchen... rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-47/rsync/io.c(453) [sender=2.6.9] ERROR: RuntimeError: Failed to launch command ["rsync", "-rL", "--rsh=ssh root@ec2root -o ControlMaster=auto -o ControlPath=/Users/ch3cooh/.chef/knife-solo-sockets/%h -o ControlPersist=3600", "--delete-after", "-zt", "--exclude=revision-deploys", "--exclude=.git", "--exclude=.hg", "--exclude=.svn", "--exclude=.bzr", "/usr/local/lib/ruby/gems/2.2.0/gems/knife-solo-0.5.1/lib/knife-solo/resources/patch_cookbooks/", ":~/chef-solo/cookbooks-1"]
長くなりましたので解決編は別の記事に書きました。
ユーザーとして実行できない
Chefはrootから操作することが前提になっているようで、cookbookの実行時にユーザーでログインしてもsuに昇格するためにパスワード入力が求められます。
どうしたら良いものかと悩んでいたのですが簡単な解決策がありました。「http://kkurahar.github.io/blog/2013/04/30/chef-script-not-user/」を参考にして書きました。
execute 'git_clone' do command <<-EOF cd /home/ec2-user su ec2-user -l -c 'git clone git@github.com:ch3cooh/ikatomo.git' EOF action :run not_if {Dir.exist?("/home/ec2-user/ikatomo")} end