「windwos7にvagrantでUbuntu13.10 server+Puppet+Chefの実験環境を構築する(Ver.2)」|ただいまRubyの修行中
はじめに
「windwos7にvagrantでUbuntu13.10 server+Puppet+Chefの実験環境を構築する」|ただいまRubyの修行中 - StupidDog's blog
のアップ後に、tbpgrさんから有益な情報を貰ったので、Chefのインストールをもっと正確に早くできる方法で再構築します。
また、Chef-soloの実行確認で何処までリポジトリを簡素に作成できるかを試してみました。
書籍として以下のものを薦めて貰いました。
Chef-Soloでの使用方法が機能を絞って書かれているので、これから始めるにはバッチリです。
入門Chef Solo - Infrastructure as Code
- 作者: 伊藤直也
- 出版社/メーカー: 伊藤直也
- 発売日: 2013/03/11
- メディア: Kindle版
- 購入: 16人 クリック: 1,027回
- この商品を含むブログ (17件) を見る
また、以下のサイトも参照しています。
サーバー設定ツール「Chef」の概要と基礎的な使い方 - さくらのナレッジ
…実践Vagrantに載ってたのに、Chefを使う予定がなかったから意識から外れてたノДT)
概要
- Vagrant-omnibusプラグインのインストール
- Vagrant-omnibusを利用してChefをインストールするVagrantfileファイルの作成
- 仮想環境の構築(vagrant up)
- Chef-soloの実行確認
VirtualBoxやVagrantなどのインストールは前回と同じです。
Vagrant-omnibusプラグインのインストールから、それを利用したVagrantfileの作成と仮想環境の構築を中心にまとめています。
1.Vagrant-omnibusプラグインのインストール
Vagrantのプラグインなので、vagrant plugin installコマンドによりインストールできます。
F:\vm\z001>vagrant plugin list vagrant-login (1.0.1, system) vagrant-share (1.0.1, system) F:\vm\z001>vagrant plugin install vagrant-omnibus Installing the 'vagrant-omnibus' plugin. This can take a few minutes... Installed the plugin 'vagrant-omnibus (1.3.1)'! F:\vm\z001>vagrant plugin list vagrant-login (1.0.1, system) vagrant-omnibus (1.3.1) vagrant-share (1.0.1, system) F:\vm\z001>
2.Vagrant-omnibusを利用してChefをインストールするVagrantfileの作成
前回のVagrantfileの内容に、Chefのインストール指示として「config.omnibus.chef_version = :latest」の一行を追加します。
VAGRANT_API_VERSION = "2" Vagrant.configure(VAGRANT_API_VERSION) do |config| config.vm.box = "ubuntu1310" config.vm.box_url = "http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-1310-i386-virtualbox-puppet.box" config.omnibus.chef_version = :latest end
3.仮想環境の構築(vagrant up)
Vagrant upコマンドを実行し環境を構築します。
F:\vm\z001>vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'ubuntu1310'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: z001_default_1396697147599_75843 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 => 2222 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection timeout. Retrying... default: Warning: Connection timeout. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => F:/vm/z001 ==> default: Installing Chef 11.10.4 Omnibus package... ==> default: Downloading Chef 11.10.4 for ubuntu... ==> default: downloading https://www.getchef.com/chef/metadata?v=11.10.4&prerelease=false&p=ubuntu&pv=13.10&m=i686 ==> default: to file /tmp/install.sh.1231/metadata.txt ==> default: trying wget... ==> default: url https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/13.04/i686/chef_11.10.4-1.ubuntu.13.04_i386.deb ==> default: md5 86dc72fa5b37eee71bf239a4a39df14b ==> default: sha256 144d7d05aadcc0cdd084c9d4b46f08ae30485d6cc267c902ff5d31230a5b86a3 ==> default: downloaded metadata file looks valid... ==> default: downloading https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/13.04/i686/chef_11.10.4-1.ubuntu.13.04_i386.deb ==> default: to file /tmp/install.sh.1231/chef_11.10.4-1.ubuntu.13.04_i386.deb ==> default: trying wget... ==> default: Comparing checksum with sha256sum... ==> default: Installing Chef 11.10.4 ==> default: installing with dpkg... ==> default: Selecting previously unselected package chef. ==> default: (Reading database ... ==> default: 62328 files and directories currently installed.) ==> default: Unpacking chef (from .../chef_11.10.4-1.ubuntu.13.04_i386.deb) ... ==> default: Setting up chef (11.10.4-1.ubuntu.13.04) ... ==> default: Thank you for installing Chef!
末尾の数行でChef 11.10.4のインストールが行われ成功している事が分かります。
4.Chef-soloの実行確認
ここからは仮想環境へssh接続し、実際にChefを実行できるか確認します。
仮想環境へのssh接続
ssh接続には前回と同じく、gitに含まれるsshコマンドを利用しています。
F:\vm\z001>vagrant ssh Welcome to Ubuntu 13.10 (GNU/Linux 3.11.0-12-generic i686) * Documentation: https://help.ubuntu.com/ Last login: Thu Apr 10 00:28:04 2014 from 10.0.2.2
Chef-Soloのバージョンを確認
vagrant@ubuntu1310-i386:~$ chef-solo -v Chef: 11.10.4
プロビジョニングのためのファイルを準備
Chefは、Puppetと違いファイルとディレクトリの構成に決まりがあります。
Cookbookなどのリソースを格納するディレクトリを「リポジトリ」と呼びます。
まずはリポジトリ用のディレクトリを作成します。
サーバー設定ツール「Chef」の概要と基礎的な使い方 - さくらのナレッジの「リポジトリとCookbookの作成、Cookbookの構造」を参照して最低限必要なものだけを作成します。
vagrant@ubuntu1310-i386:~$ mkdir -p chef-repo vagrant@ubuntu1310-i386:~$ tree chef-repo chef-repo 0 directories, 0 files vagrant@ubuntu1310-i386:~$ knife cookbook create hello -o ~/chef-repo WARNING: No knife configuration file found ** Creating cookbook hello ** Creating README for cookbook: hello ** Creating CHANGELOG for cookbook: hello ** Creating metadata for cookbook: hello vagrant@ubuntu1310-i386:~$ tree ~/chef-repo /home/vagrant/chef-repo └── cookbooks └── hello ├── attributes ├── CHANGELOG.md ├── definitions ├── files │ └── default ├── libraries ├── metadata.rb ├── providers ├── README.md ├── recipes │ └── default.rb ├── resources └── templates └── default 12 directories, 4 files
レシピを編集
「~/chef-repo/cookbooks」ディレクトリ以下にknife cookbook createコマンドで作成したcookbookのひな形が作成されています。
各cookbookは「recipes」ディレクトリ以下に複数のレシピをファイルとして持ちます。
cookbookでレシピを指定しなかった場合に実行されるレシピは「default.rb」ファイルに記述します。
コメントだけが記述されたひな形が作成されているので、下記のように一行追加します。
vagrant@ubuntu1310-i386:~$ cat ~/chef-repo/cookbooks/hello/recipes/default.rb # # Cookbook Name:: hello # Recipe:: default # # Copyright 2014, YOUR_COMPANY_NAME # # All rights reserved - Do Not Redistribute # log "Welcome to a kitchen!"
設定ファイルの作成
Chef-soloへ渡す設定としてファイルを作成します。
下記の内容でファイル名を「solo.rb」としリポジトリ直下に作成します。
vagrant@ubuntu1310-i386:~$ cat ~/chef-repo/solo.rb file_cache_path "/tmp/chef-solo" cookbook_path ["/home/vagrant/chef-repo/cookbooks"]
実行するレシピを決めるファイルの作成
Chef-soloでは実行するレシピのファイルを直接指定しません。
JSON書式のファイルに記述して実行します。
下記の内容でファイル名を「localhost.json」としリポジトリ直下に作成します。
vagrant@ubuntu1310-i386:~$ cat ~/chef-repo/localhost.json { "run_list" : [ "recipe[hello]" ] }
レシピの適用
chef-soloコマンドは、管理対象のサーバーで、管理者権限を必要とする部分も変更の対象とするので、実行にsudoが必要になります。
vagrant@ubuntu1310-i386:~$ chef-solo -c ~/chef-repo/solo.rb -j ~/chef-repo/localhost.json [2014-04-10T04:58:01-07:00] ERROR: Permission denied - /tmp/chef-solo/chef-client-running.pid [2014-04-10T04:58:01-07:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1) vagrant@ubuntu1310-i386:~$ sudo chef-solo -c ~/chef-repo/solo.rb -j ~/chef-repo/localhost.json Starting Chef Client, version 11.10.4 Compiling Cookbooks... Converging 1 resources Recipe: hello::default * log[Welcome to a kitchen!] action write Running handlers: Running handlers complete Chef Client finished, 1/1 resources updated in 1.355948408 seconds
ログ出力として、"Welcome to a kitchen!"の出力に成功しました。
まとめ
Chef-soloのインストールにVagrant-omnibusプラグインを使用して仮想環境構築と合わせてChef-solo環境を入手する方法を確認しました。
折角作成したのでChef-soloでは、どこまでリポジトリを省略して作成できるかを試してみました。
結果として動作確認だけなら、リポジトリ直下にcookbooksがあれば動きそうです。
最終的なリポジトリの内容
vagrant@ubuntu1310-i386:~$ tree -F ~/chef-repo /home/vagrant/chef-repo ├── cookbooks/ │ └── hello/ │ ├── attributes/ │ ├── CHANGELOG.md │ ├── definitions/ │ ├── files/ │ │ └── default/ │ ├── libraries/ │ ├── metadata.rb │ ├── providers/ │ ├── README.md │ ├── recipes/ │ │ └── default.rb │ ├── resources/ │ └── templates/ │ └── default/ ├── localhost.json └── solo.rb 12 directories, 6 files
また、参照する先々でknife configureコマンドを実行するように説明されていますが、意図的に実行しないで検証を行いました。今回の内容では、knife configureコマンドで作成する「~/.chef/knife.rb」ファイルは不要だったようです。
前回インストールしたknife-soloはgemなのでVagrant-ominibusプラグインではインストールされません。
次は、構築した環境をベースにChefの使用する機能を増やして、どこで何が必要になるか検証していきます。
あらぁ…puppetやrubyから離れていくノД≦)ノ