IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

chefでhello world

今回も嵌りました。単純にログでHello Chef!を出力したかったのですが、出来ず。あとruby覚えるのが辛い。

参考にさせて頂いた資料

環境

AMI ID RHEL-6.5_GA-x86_64-7-Hourly2 (ami-a15e24a0)

chefインストール前の準備

treeコマンドが入ってなかったのでインストール

[user]# yum install tree

chefインストールをします。

オプションのLの意味は下記です。

 -L/--location      Follow Location: hints (H)
    --location-trusted Follow Location: and send auth to other hosts (H)
[user]# curl -L https://www.opscode.com/chef/install.sh |sudo bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15934  100 15934    0     0  17096      0 --:--:-- --:--:-- --:--:-- 42153
Downloading Chef  for el...
downloading https://www.opscode.com/chef/metadata?v=&prerelease=false&nightlies=false&p=el&pv=6&m=x86_64
  to file /tmp/install.sh.1453/metadata.txt
trying wget...
url https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-11.12.4-1.el6.x86_64.rpm
md5 959acd5df77c25f4f69d1f786f3c7360
sha256  d4eacc6b16c448a628367e7201922a4c58997f68808c5f698676e8a5eaf169b5
downloaded metadata file looks valid...
downloading https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-11.12.4-1.el6.x86_64.rpm
  to file /tmp/install.sh.1453/chef-11.12.4-1.el6.x86_64.rpm
trying wget...
Comparing checksum with sha256sum...
Installing Chef 
installing with rpm...
警告: /tmp/install.sh.1453/chef-11.12.4-1.el6.x86_64.rpm: ヘッダ V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
準備中...                ########################################### [100%]
   1:chef                   ########################################### [100%]
Thank you for installing Chef!

とくにrubyのインストールをしなくて良いようです。which rubyで情報が出てこないのですが、どこにインストールされているのでしょうか?

バージョンの確認をします。

[user]# chef-solo -v
Chef: 11.12.4

ヘルプの確認をします。

[user]# chef-solo -help
Usage: /usr/bin/chef-solo (options)
    -f, --[no-]fork                  Fork client
        --[no-]color                 Use colored output, defaults to enabled
    -c, --config CONFIG              The configuration file to use
    -d, --daemonize                  Daemonize the process
    -E, --environment ENVIRONMENT    Set the Chef Environment on the node
        --force-formatter            Use formatter output instead of logger output
        --force-logger               Use logger output instead of formatter output
    -F, --format FORMATTER           output format to use
    -g, --group GROUP                Group to set privilege to
    -i, --interval SECONDS           Run chef-client periodically, in seconds
    -j JSON_ATTRIBS,                 Load attributes from a JSON file or URL
        --json-attributes
    -l, --log_level LEVEL            Set the log level (debug, info, warn, error, fatal)
    -L, --logfile LOGLOCATION        Set the log file location, defaults to STDOUT
    -N, --node-name NODE_NAME        The node name for this client
    -o RunlistItem,RunlistItem...,   Replace current run list with specified items
        --override-runlist
    -r, --recipe-url RECIPE_URL      Pull down a remote gzipped tarball of recipes and untar it to the cookbook cache.
    -s, --splay SECONDS              The splay time for running at intervals, in seconds
    -u, --user USER                  User to set privilege to
    -v, --version                    Show chef version
    -W, --why-run                    Enable whyrun mode
    -h, --help                       Show this message
[user]# 

コマンドの場所の確認をします。

[user]# ls -al /usr/bin  | grep chef
lrwxrwxrwx.  1 root root         24  524 02:47 2014 chef-apply -> /opt/chef/bin/chef-apply
lrwxrwxrwx.  1 root root         25  524 02:47 2014 chef-client -> /opt/chef/bin/chef-client
lrwxrwxrwx.  1 root root         24  524 02:47 2014 chef-shell -> /opt/chef/bin/chef-shell
lrwxrwxrwx.  1 root root         23  524 02:47 2014 chef-solo -> /opt/chef/bin/chef-solo
lrwxrwxrwx.  1 root root         19  524 02:47 2014 knife -> /opt/chef/bin/knife
lrwxrwxrwx.  1 root root         18  524 02:47 2014 ohai -> /opt/chef/bin/ohai
lrwxrwxrwx.  1 root root         18  524 02:47 2014 shef -> /opt/chef/bin/shef

knifeコマンドで初期設定をします。

[user]# knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/root/.chef/knife.rb] y
Please enter the chef server URL: [https://ip-172-31-18-171.ap-northeast-1.compute.internal:443] y
Please enter an existing username or clientname for the API: [ec2-user] y
Please enter the validation clientname: [chef-validator] y
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] y
Please enter the path to a chef repository (or leave blank): y
*****

You must place your client key in:
  /home/ec2-user/y.pem
Before running commands with Knife!

*****

You must place your validation key in:
  /home/ec2-user/y
Before generating instance data with Knife!

*****
Configuration file written to /home/ec2-user/y
[user]# ls /home/ec2-user/y
/home/ec2-user/y
[user]# less /home/ec2-user/y
[user]# cat /home/ec2-user/y
log_level                :info
log_location             STDOUT
node_name                'y'
client_key               '/home/ec2-user/y.pem'
validation_client_name   'y'
validation_key           '/home/ec2-user/y'
chef_server_url          'y'
syntax_check_cache_path  '/home/ec2-user/syntax_check_cache'
cookbook_path [ 'y/cookbooks' ]
[user]# 

クックプックのディレクトリを作成します。

[root@ip-172-31-18-171 cookbooks]# knife cookbook hoge01
FATAL: Cannot find sub command for: 'cookbook hoge01'
Available cookbook subcommands: (for details, knife SUB-COMMAND --help)

** COOKBOOK COMMANDS **
knife cookbook bulk delete REGEX (options)
knife cookbook create COOKBOOK (options)
knife cookbook delete COOKBOOK VERSION (options)
knife cookbook download COOKBOOK [VERSION] (options)
knife cookbook list (options)
knife cookbook metadata COOKBOOK (options)
knife cookbook metadata from FILE (options)
knife cookbook show COOKBOOK [VERSION] [PART] [FILENAME] (options)
knife cookbook test [COOKBOOKS...] (options)
knife cookbook upload [COOKBOOKS...] (options)

クックブックのディレクトリの確認をします。

[user]# tree /var/chef/cookbooks
/var/chef/cookbooks
└── hoge01
    ├── CHANGELOG.md
    ├── README.md
    ├── attributes
    ├── definitions
    ├── files
    │   └── default
    ├── libraries
    ├── metadata.rb
    ├── providers
    ├── recipes
    │   └── default.rb
    ├── resources
    └── templates
        └── default

11 directories, 4 files

レシピファイルの確認をします。

[user]# cat /var/chef/cookbooks/hoge01/recipes/default.rb 
#
# Cookbook Name:: hoge01
# Recipe:: default
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

chef-soloが利用するレシピの場所を設定する。

[user]# mkdir /etc/chef
[user]# vi /etc/chef/solo.rb
[user]# cat /etc/chef/solo.rb
cookbook_path ["/var/chef/cookbooks"]
[user]# 
[user]# 

レシピを記述します。

  1 #
  2 # Cookbook Name:: hoge01
  3 # Recipe:: default
  4 #
  5 # Copyright 2014, YOUR_COMPANY_NAME
  6 #
  7 # All rights reserved - Do Not Redistribute
  8 #
  9 log "hello" do
 10   message "hello chef!"
 11   level :debug
 12   action :write
 13 end
 14 #log "hello  hoge"
 15 bash "echo" do
 16   code <<-EOH
 17     echo "hello echo"
 18   EOH
 19 end
 20 directory "/etc/foo" do
 21   owner "root"
 22   group "root"
 23   mode 00644
 24   action :create
 25 end

レシピを実行します。

[user]# chef-solo -o hoge01
Starting Chef Client, version 11.12.4
Compiling Cookbooks...
Converging 3 resources
Recipe: hoge01::default
  * log[hello] action write

  * bash[echo] action run
    - execute "bash"  "/tmp/chef-script20140524-26557-149loyo"

  * directory[/etc/foo] action create
    - create new directory /etc/foo
    - change mode from '' to '0644'
    - change owner from '' to 'root'
    - change group from '' to 'root'
    - restore selinux security context


Running handlers:
Running handlers complete

Chef Client finished, 3/3 resources updated in 4.296873531 seconds
[user]# ls -la /etc/foo
合計 16
drw-r--r--.   2 root root  4096  524 04:33 2014 .
drwxr-xr-x. 102 root root 12288  524 04:33 2014 ..
[user]# 

logリソースの設定でmessageに設定した文字列がどこに出力されているのでしょうか??