20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Chefに入門 (1)

Last updated at Posted at 2013-10-15

このたびChefとはどんなものが知りたくて、入門Chef SoloをKindleで購入いたしました(初期型Nexus 7で閲覧)。こんなお手頃な値段で入門書が手に入るとは、いい時代になりました。しかも電子書籍って本文検索やマーキングとかもできて便利ですね。かさばらないし。
以下お勉強メモです。当然といえば当然ですが、内容はかなり端折ってるので詳細は上記書籍をご一読ください。今回は初回のいわゆるHello, World実行まで。

何はともあれやってみよ

折角なのでVagrant+VMware fusionで仮想マシン環境を準備。

$ mkdir chef-test
$ cd chef-test 
$ vagrant box list
precise64 (vmware_fusion)
$ vagrant init precise64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

仮想マシンイメージをVMware Fusionに立ち上げます。

$ vagrant up --provider=vmware_fusion
Bringing machine 'default' up with 'vmware_fusion' provider...
[default] Cloning VMware VM: 'precise64'. This can take some time...
[default] Verifying vmnet devices are healthy...
[default] Preparing network adapters...
[default] Starting the VMware VM...
[default] Waiting for the VM to finish booting...
[default] The machine is booted and ready!
[default] Forwarding ports...
[default] -- 22 => 2222
[default] Configuring network adapters within the VM...
[default] Enabling and configuring shared folders...
[default] -- /your.path.to.rootdir/chef-test: /vagrant

SSHで入ってみます。

$ vagrant ssh
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-virtual x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Thu Jan 31 13:48:53 2013
vagrant@precise64:~$ 

git入れます。

vagrant@precise64:~$ sudo apt-get install git-core

Chef入れます。

vagrant@precise64:~$ curl -L http://www.opscode.com/chef/install.sh |  sudo bash

curlがないと言われました。

-bash: curl: command not found

aptでインストールします。

vagrant@precise64:~$ sudo apt-get install curl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libcurl3
The following NEW packages will be installed:
  curl libcurl3
0 upgraded, 2 newly installed, 0 to remove and 41 not upgraded.
Need to get 374 kB of archives.
After this operation, 910 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://us.archive.ubuntu.com/ubuntu/ precise/main libcurl3 amd64 7.22.0-3ubuntu4 [237 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ precise/main curl amd64 7.22.0-3ubuntu4 [138 kB]
Fetched 374 kB in 1s (214 kB/s)
Selecting previously unselected package libcurl3.
(Reading database ... 23766 files and directories currently installed.)
Unpacking libcurl3 (from .../libcurl3_7.22.0-3ubuntu4_amd64.deb) ...
Selecting previously unselected package curl.
Unpacking curl (from .../curl_7.22.0-3ubuntu4_amd64.deb) ...
Setting up libcurl3 (7.22.0-3ubuntu4) ...
Setting up curl (7.22.0-3ubuntu4) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

再実行。

vagrant@precise64:~$ curl -L http://www.opscode.com/chef/install.sh | sudo bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6789  100  6789    0     0  11818      0 --:--:-- --:--:-- --:--:-- 20085
Downloading Chef  for ubuntu...
Installing Chef 
Selecting previously unselected package chef.
(Reading database ... 23798 files and directories currently installed.)
Unpacking chef (from .../tmp.5nxyDg68/chef__amd64.deb) ...
Setting up chef (11.6.2-1.ubuntu.12.04) ...
Thank you for installing Chef!

入りました。
続いて、Chefリポジトリのひな形をOpscodeのGithubから持ってきます。

$ git clone git://github.com/opscode/chef-repo.git

クックブックの作成。knifeコマンドを使って作成します。
まずはknifeの初期化。

$ knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/home/vagrant/.chef/knife.rb] 
Please enter the chef server URL: [http://precise64:4000] 
Please enter an existing username or clientname for the API: [vagrant] 
Please enter the validation clientname: [chef-validator] 
Please enter the location of the validation key: [/etc/chef/validation.pem] 
Please enter the path to a chef repository (or leave blank): 
*****

You must place your client key in:
  /home/vagrant/.chef/vagrant.pem
Before running commands with Knife!

*****

You must place your validation key in:
  /etc/chef/validation.pem
Before generating instance data with Knife!

*****
Configuration file written to /home/vagrant/.chef/knife.rb

chef-repo/cookbooksディレクトリ内にhelloというクックブックを作ります。

$ cd chef-repo
$ knife cookbook create hello -o cookbooks

レシピを編集します。
今回は"Hello,Chef!"というログをコンソールに出力するのみです。

$ vi cookbooks/hello/recipes/default.rb
default.rb
#
# Cookbook Name:: hello
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

log "Hello, Chef!"

Chef Solo実行時に実行するレシピを記述するJSONファイルを用意。
今回はchef-repo/test.jsonとしました。

test.json
//test.json

{
        "run_list":[
                "recipe[hello]"
        ]
}

Chefが利用するテンポラリディレクトリやクックブックのパスを指定する設定ファイルをchef-repo/config.rbとして記述します。

config.rb
# config.rb
file_cache_path "/tmp/chef-solo"
cookbook_path ["/home/vagrant/chef-repo/cookbooks"]

あまりハマる方はいないと思われますが、cookbook_pathのあとに半角スペース入れないと以下の様なエラーが出ます。。

[2013-10-15T07:47:10-07:00] FATAL: Unknown error processing config file solo.rb with error can't convert String into Integer

いざ実行。

$ sudo chef-solo -c config.rb -j ./test.json 
Starting Chef Client, version 11.6.2
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * log[Hello, Chef!] action write

Chef Client finished, 1 resources updated

ちゃんとログが出ていることが確認できました。
本日はここまで。。

20
20
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
20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?