低レベルインフラエンジニアの忘備録

Linux系の各種作業を自分用の忘備録として残しています

【chef】knife-soloでchef-soloをリモート実行(Apache+MySQL)

【chef】knife-soloでchef-soloをリモート実行saosao-0706.hatenablog.com

先日こちらの記事を書かせていただきましたが、こちらの環境で今度はApacheMysqlをセットアップしてみたいと思います。

環境

上記の記事をご参照ください。

apacheMySQL用のcookbookを作成

# cd /root/vagrant/centos67
# knife cookbook create apache -o site-cookbooks
** Creating cookbook apache in /root/vagrant/centos67/site-cookbooks
** Creating README for cookbook: apache
** Creating CHANGELOG for cookbook: apache
** Creating metadata for cookbook: apache
# knife cookbook create mysql -o site-cookbooks
** Creating cookbook mysql in /root/vagrant/centos67/site-cookbooks
** Creating README for cookbook: mysql
** Creating CHANGELOG for cookbook: mysql
** Creating metadata for cookbook: mysql
# cd /root/vagrant/centos67/site-cookbooks/
# ls -l
合計 12
drwxr-xr-x 10 root root 4096  4月 26 16:50 2016 apache
drwxr-xr-x 10 root root 4096  4月 26 09:48 2016 dovecot
drwxr-xr-x 10 root root 4096  4月 26 16:50 2016 mysql

「site-cookbooks」ディレクトリ内にcookbookが生成されます。
「/root/vagrant/centos67」はsite-cookbooksがあるディレクトリです。

Nodeオブジェクト(ランリスト)の編集

# vi /root/vagrant/centos67/nodes/192.168.33.10.json
{
  "run_list": [
    "recipe[dovecot]",
    "recipe[apache]",
    "recipe[mysql]"
  ],
  "automatic": {
    "ipaddress": "192.168.33.10"
  }
}

こちらを追加することで、knife-solo実行時にApacheMySQL用のレシピが実行されます。

apacheのレシピを書く

# vi /root/vagrant/centos67/site-cookbooks/apache/recipes/default.rb
#
# Cookbook Name:: apache
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
package "httpd" do
    action :install
end

service "httpd" do
    action [ :enable, :start]
end

赤字の行を追加します。
上記のレシピの意味は以下の通りです。
◆action :install
apacheのインストール
◆action [ :enable, :start]
enableはOS起動時のサービス有効化(chkconfig httpd on)、startはサービスの起動(service httpd start)

mysqlのレシピを書く

# vi /root/vagrant/centos67/site-cookbooks/mysql/recipes/default.rb
#
# Cookbook Name:: mysql
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
package "mysql-server" do
    action :install
end

service "mysqld" do
    action [ :enable, :start]
end

赤字の行を追加します。
上記のレシピの意味は以下の通りです。

knife-solo実行

# cd /root/vagrant/centos67
# knife solo cook 192.168.33.10
Running Chef on 192.168.33.10...
Checking Chef version...
/root/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/net-ssh-2.9.4/lib/net/ssh/transport/session.rb:67:in `initialize': Object#timeout is deprecated, use Timeout.timeout instead.
/root/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/net-ssh-2.9.4/lib/net/ssh/transport/session.rb:84:in `initialize': Object#timeout is deprecated, use Timeout.timeout instead.
/root/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/net-ssh-2.9.4/lib/net/ssh/transport/session.rb:67:in `initialize': Object#timeout is deprecated, use Timeout.timeout instead.
/root/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/net-ssh-2.9.4/lib/net/ssh/transport/session.rb:84:in `initialize': Object#timeout is deprecated, use Timeout.timeout instead.
Installing Berkshelf cookbooks to 'cookbooks'...
Resolving cookbook dependencies...
Uploading the kitchen...
WARNING: Local cookbook_path '/root/vagrant/centos67/cookbooks' does not exist
Generating solo config...
Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
Starting Chef Client, version 12.9.38
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 5 resources
Recipe: dovecot::default
  * yum_package[dovecot] action install (up to date)
Recipe: apache::default
  * yum_package[httpd] action install
    - install version 2.2.15-47.el6.centos.4 of package httpd
  * service[httpd] action enable
    - enable service service[httpd]
  * service[httpd] action start
    - start service service[httpd]
Recipe: mysql::default
  * yum_package[mysql-server] action install
    - install version 5.1.73-5.el6_7.1 of package mysql-server
  * service[mysqld] action enable
    - enable service service[mysqld]
  * service[mysqld] action start
    - start service service[mysqld]

Running handlers:
Running handlers complete
Chef Client finished, 6/7 resources updated in 24 seconds

ApacheMySQLの作業を行った旨のログが出力されます。

ゲストOSにログインして確認

 
# ssh 192.168.33.10
Last login: Thu Apr 28 05:36:54 2016 from 10.0.2.2
$ ps -ef | grep httpd
root      4747     1  0 05:37 ?        00:00:00 /usr/sbin/httpd
apache    4749  4747  0 05:37 ?        00:00:00 /usr/sbin/httpd
apache    4750  4747  0 05:37 ?        00:00:00 /usr/sbin/httpd
apache    4751  4747  0 05:37 ?        00:00:00 /usr/sbin/httpd
apache    4752  4747  0 05:37 ?        00:00:00 /usr/sbin/httpd
apache    4753  4747  0 05:37 ?        00:00:00 /usr/sbin/httpd
apache    4754  4747  0 05:37 ?        00:00:00 /usr/sbin/httpd
apache    4755  4747  0 05:37 ?        00:00:00 /usr/sbin/httpd
apache    4756  4747  0 05:37 ?        00:00:00 /usr/sbin/httpd
vagrant   5093  5077  0 05:38 pts/0    00:00:00 grep httpd
$ ps -ef | grep mysql
root      4955     1  0 05:37 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql     5057  4955  0 05:37 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
vagrant   5095  5077  0 05:38 pts/0    00:00:00 grep mysql
$ chkconfig --list | grep httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
$ chkconfig --list | grep mysql
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

ApacheMySQLがインストールされ、サービスが起動しています。
また、OS起動時にサービスが起動する設定となっています。

ちなみに、knife-solo実行時に文法エラーがあった場合は以下のようなエラーメッセージが出力されます。

# knife solo cook 192.168.33.10
Running Chef on 192.168.33.10...
Checking Chef version...
/root/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/net-ssh-2.9.4/lib/net/ssh/transport/session.rb:67:in `initialize': Object#timeout is deprecated, use Timeout.timeout instead.
/root/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/net-ssh-2.9.4/lib/net/ssh/transport/session.rb:84:in `initialize': Object#timeout is deprecated, use Timeout.timeout instead.
/root/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/net-ssh-2.9.4/lib/net/ssh/transport/session.rb:67:in `initialize': Object#timeout is deprecated, use Timeout.timeout instead.
/root/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/net-ssh-2.9.4/lib/net/ssh/transport/session.rb:84:in `initialize': Object#timeout is deprecated, use Timeout.timeout instead.
ERROR: JSON::ParserError: 419: unexpected token at '"recipe[apache]"
        "recipe[mysql]"
  ],
  "automatic": {
    "ipaddress": "192.168.33.10"
  }
}