Chefことはじめ

kidleで参考書を買ったので、本に従って進めていきます。

入門Chef Solo - Infrastructure as Code

入門Chef Solo - Infrastructure as Code

環境は、CentOS 6.6(最小インストール)です。

先に作業用ディレクトリだけ作っておく。

[root@chef ope]# cd /home/ope
[root@chef ope]# mkdir 20150308_chef
[root@chef ope]# chmod 755 20150308_chef/
[root@chef ope]# cd 20150308_chef/

次にRubyのインストール

[root@chef ope]# yum install ruby

そして、Chefのインストール。curlして、実行するだけなので超カンタンです。

[root@chef 20150308_chef]# curl 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 18373  100 18373    0     0  30185      0 --:--:-- --:--:-- --:--:-- 40919
Downloading Chef  for el...
downloading http://www.opscode.com/chef/metadata?v=&prerelease=false&nightlies=false&p=el&pv=6&m=x86_64
  to file /tmp/install.sh.1210/metadata.txt
trying curl...
url     http://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.1.0-1.el6.x86_64.rpm
md5     7169d310550cff994e23391de18c258e
sha256  26e891420622ed244fd8554d916a879b0df1621c37f459dca1100e8a7e2ca13e
downloaded metadata file looks valid...
downloading http://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.1.0-1.el6.x86_64.rpm
  to file /tmp/install.sh.1210/chef-12.1.0-1.el6.x86_64.rpm
trying curl...
Comparing checksum with sha256sum...
Installing Chef
installing with rpm...
警告: /tmp/install.sh.1210/chef-12.1.0-1.el6.x86_64.rpm: ヘッダ V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
準備中...                ########################################### [100%]
   1:chef                   ########################################### [100%]
Thank you for installing Chef!

次にレポジトリの作成。これもgitから落とすだけ。

[root@chef 20150308_chef]# yum install git

[root@chef 20150308_chef]# git clone git://github.com/opscode/chef-repo.git

Initialized empty Git repository in /home/ope/20150308_chef/chef-repo/.git/
remote: Counting objects: 240, done.
remote: Total 240 (delta 0), reused 0 (delta 0), pack-reused 240
Receiving objects: 100% (240/240), 48.06 KiB, done.
Resolving deltas: 100% (65/65), done.

続いて、クックブックの作成。
まずはknifeコマンド+サブコマンドconfigureで、
knifeの初期設定を実行。

[root@chef 20150308_chef]#  knife configure

Initialized empty Git repository in /home/ope/20150308_chef/chef-repo/.git/
remote: Counting objects: 240, done.
remote: Total 240 (delta 0), reused 0 (delta 0), pack-reused 240
Receiving objects: 100% (240/240), 48.06 KiB, done.
Resolving deltas: 100% (65/65), done.
[root@chef 20150308_chef]#
[root@chef 20150308_chef]# knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/root/.chef/knife.rb]
Please enter the chef server URL: [https://chef:443]
Please enter an existing username or clientname for the API: [ope]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem]
Please enter the path to a chef repository (or leave blank):
*****

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

*****

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

*****
Configuration file written to /root/.chef/knife.rb

その後、クックバックhelloの作成。

[root@chef chef-repo]# knife cookbook create hello -o /tmp/chef/chef-repo/coo
kbooks
** Creating cookbook hello in /home/ope/20150308_chef/chef-repo/cookbooks
** Creating README for cookbook: hello
** Creating CHANGELOG for cookbook: hello
** Creating metadata for cookbook: hello

おーなんか、できてる!

[root@chef chef-repo]# cd cookbooks/

[root@chef cookbooks]# ls -ltr
合計 8
-rw-r--r--  1 root root 3064  38 15:05 2015 README.md
drwxr-xr-x 10 root root 4096  38 15:09 2015 hello

そのまま、default.rbの作成。ここに書いた内容が実行されるらしい。

[root@chef recipes]# vi /home/ope/20150308_chef/chef-repo/cookbooks/hello/recipes/default.rb

赤字が追記箇所

#
# Cookbook Name:: hello
# Recipe:: default
#
# Copyright 2015, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

log "Hello,Chef!"

chef-repo配下に必要なファイル×2を作成。

1つ目はlocallhost.json

[root@chef chef-repo]# vi locallhost.json

こんな感じに作成

// localhost.json

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

2つ目はsolo.rb

[root@chef chef-repo]# vi solo.rb

こんな感じに作成

# solo.rb
file_cache_path "/tmp/chef-solo"
cookbook_path ["/home/ope/20150308_chef/chef-repo/cookbooks"]

最後に、レシピを実行

[root@chef chef-repo]# su - ope
[ope@chef chef-repo]$ sudo chef-solo -c solo.rb -j ./locallhost.json
Starting Chef Client, version 12.1.0
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * log[Hello,Chef!] action write


Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 1.067800253 seconds
[ope@chef chef-repo]$

成功!