LoginSignup
12
12

More than 5 years have passed since last update.

Chefに入門(4) 〜knife-soloの導入〜

Posted at

入門Chef Soloのお勉強ポスト4回目にして最終回です。
まだほんの序盤ですので、続きは書籍をチェックしていただければと思います。
説明がとても分かりやすく、「自分でもレシピ書いてみようかな!」という気持ちにさせてもらえます。

さて、話は戻りますが、
knife-soloを入れれば、ローカルからレシピの編集、サーバへの転送、Chef-soloの実行が可能になるらしいです。
というわけで、前回のnginxのレシピをknife-solo経由でVagrant VMにアップロード、Chef-soloを実行する手順を試してみました。

インストール

以下のコマンド一発でインストールできます。

$ sudo gem install knife-solo

Vagrantで立ち上げたサーバへのSSH設定

knife-soloから操作できるようにするために、サーバに対するSSHを設定しておきます。

Vagrantなら以下のコマンドでSSH用の設定ファイルを書き出してくれますので、以下のように実行すればOKです。precise64の部分は任意のホスト名です。

$ vagrant ssh-config --host precise64 >> ~/.ssh/config
$ ls ~/.ssh
config      id_rsa      id_rsa.pub  known_hosts
$ ssh precise64
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-virtual x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Tue Oct 15 07:19:40 2013 from 172.16.215.1
vagrant@precise64:~$

入れました。

Chefリポジトリの作成、Gitにコミット

お好きな場所にchefリポジトリを作成します。

$ knife solo init chef-repo
WARNING: No knife configuration file found
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...

折角なのでリポジトリはGitで管理してみます。

$ cd chef-repo/
$ git init
$ git add .
$ git commit -m "first commit"
[master (root-commit) baed1ff] first commit
 2 files changed, 8 insertions(+)
 create mode 100644 .chef/knife.rb
 create mode 100644 .gitignore
 create mode 100644 data_bags/.gitkeep
 create mode 100644 nodes/.gitkeep
 create mode 100644 roles/.gitkeep
 create mode 100644 site-cookbooks/.gitkeep

Vagrant VMの準備

といってもprepareコマンド一発です。

$ knife solo prepare precise64
Bootstrapping Chef...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6790  100  6790    0     0   7237      0 --:--:-- --:--:-- --:--:--  9672
Downloading Chef 11.6.2 for ubuntu...
Installing Chef 11.6.2
(Reading database ... 35979 files and directories currently installed.)
Preparing to replace chef 11.6.2-1.ubuntu.12.04 (using .../chef_11.6.2_amd64.deb) ...
Unpacking replacement chef ...
Setting up chef (11.6.2-1.ubuntu.12.04) ...
Thank you for installing Chef!
Generating node config 'nodes/precise64.json'...

precise64に対するノード設定ファイル(JSON)が出来上がるので、こちらもGit管理に加えます。

$ git add nodes/precise64.json
$ git commit -m 'add node json file'
[master 8230017] add node json file
 1 file changed, 1 insertion(+)
 create mode 100644 nodes/precise64.json

クックブックおよび設定ファイル群の作成

nginx用のクックブックおよび設定ファイル群を作ります。
各設定ファイルの中身は前回コードを示した3ファイルと同じです。

$ knife cookbook create nginx -o site-cookbooks
** Creating cookbook nginx
** Creating README for cookbook: nginx
** Creating CHANGELOG for cookbook: nginx
** Creating metadata for cookbook: nginx
$ vi site-cookbooks/nginx/recipes/default.rb 
$ vi site-cookbooks/nginx/templates/default/nginx.conf.erb
$ vi nodes/precise64.json 

Knife soloの実行

コマンド一発で実行できます。

$ knife solo cook precise64

これでprecise64のサーバ上でnginxが立ち上がりました。
Git管理に加えます。

$ git add site-cookbooks/nginx
$ git commit -m 'Add nginx recipe'
[master 51a5039] Add nginx recipe
 5 files changed, 135 insertions(+)
 create mode 100644 site-cookbooks/nginx/CHANGELOG.md
 create mode 100644 site-cookbooks/nginx/README.md
 create mode 100644 site-cookbooks/nginx/metadata.rb
 create mode 100644 site-cookbooks/nginx/recipes/default.rb
 create mode 100644 site-cookbooks/nginx/templates/default/nginx.conf.erb

基本はこの流れでクックブック、レシピを作成していくみたいです。
なるほど。。。

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12