読者です 読者をやめる 読者になる 読者になる

ぶるーすくりーん

ぽんこつプログラマ日記

VagrantとChefをためしてみた

vagrant chef

やっとvagrantとChefをためしてみたのでそのメモ。

参考サイト:http://qiita.com/taiki45/items/b46a2f32248720ec2bae

 

マシン

Linux Mint 16 Petra (Cinnamon 64-bit)

 

Vagrantインストール

 

~ % mkdir -p ~/vagrant/centos65; cd ~/vagrant/centos65

centos65 % vagrant box add centos65 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
centos65 % vagrant init centos65
centos65 % vagrant up

以下のエラーが発生して起動できない (TへT)

centos65 % vagrant up
         ~~~~ 省略 ~~~~
    default: SSH auth method: private key
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
    default: Error: Connection timeout. Retrying...
### default: Error: Connection timeout. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

理由はBIOS設定デフォルトのままでVirtualization Technologyの設定がDisableになっていたため。。 BIOS設定しなおして再リトライで無事起動。

centos65 % vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> 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: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /home/mid/vagrant/centos65
centos65 % vagrant ssh
[vagrant@vagrant-centos65 ~]$ 

vagrant-omnibusインストール

VM環境に応じたChefさんをインストールしてくれるvagrantプラグインhttps://github.com/schisamo/vagrant-omnibus

$ vagrant plugin install vagrant-omnibus

Vagrantfileに以下を追加

Vagrantfile

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # ...
  config.omnibus.chef_version = :latest
  # ...
end

vagrant-berkshelfインストールとレシピ作成

  • インストール
centos65 % vagrant plugin install vagrant-berkshelf --plugin-version '>= 2.0.1'
Installing the 'vagrant-berkshelf --version '>= 2.0.1'' plugin. This can take a few minutes...
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

Net::HTTPNotFound: No gems found matching "vagrant-vmware-fusion" "2.3.5" nil

謎エラー。 このへん参考にvagrant-hostmanagerいれてみて再実行。

centos65 % vagrant plugin install vagrant-hostmanager
centos65 % vagrant plugin install vagrant-berkshelf --plugin-version '>= 2.0.1'
centos65 % sudo gem i berkshelf
  • berkshelfの設定 Berksfile
site :opscode
  • レシピ作成
centos65 % mkdir site-cookbooks
centos65 % cd !$
cd site-cookbooks
site-cookbooks % berks cookbook initEnvironment
      create  initEnvironment/files/default
      create  initEnvironment/templates/default
      create  initEnvironment/attributes
      create  initEnvironment/libraries
      create  initEnvironment/providers
      create  initEnvironment/recipes
      create  initEnvironment/resources
      create  initEnvironment/recipes/default.rb
      create  initEnvironment/metadata.rb
      create  initEnvironment/LICENSE
      create  initEnvironment/README.md
      create  initEnvironment/CHANGELOG.md
      create  initEnvironment/Berksfile
      create  initEnvironment/Thorfile
      create  initEnvironment/chefignore
      create  initEnvironment/.gitignore
         run  git init from "./initEnvironment"
      create  initEnvironment/Gemfile
      create  initEnvironment/Vagrantfile
site-cookbooks % cd -
/home/mid/vagrant/centos65
  • レシピ作成

とりあえずはろーわーるど

site-cookbooks/initEnvironment/recipes/default.rb

log "Hello World!!!!!!!!!!!!!" 

Vagrantとchef連携!

Vagrantfileにbarkshelf用の設定, chef solo用の設定を追加 Vagrantfile

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # ...

  # vagrant-omnibus
  config.omnibus.chef_version = :latest

  # barkshelf
  config.berkshelf.enabled = true

  # chef solo
  config.vm.provision :chef_solo do |chef|
    chef.run_list = ["initEnvironment"]
  end

end

Berksfileに作成したレシピを読み込むための設定を追加 Berksfile

site :opscode
cookbook "initEnvironment", path: "site-cookbooks/initEnvironment"

いったんさっき作成したVMを捨てて再度vagrant up

centos65 % vagrant halt
centos65 % vagrant destroy
centos65 % vagrant up

まつ。まつ。まつ。。。。

無事ログにはろーわーるど表示

==> default: [2014-06-03T12:08:31+00:00] INFO: Hello World!!!!!!!!!!!!!

ほんとはChefでMW環境構築までしてみたかったのですが、今日はこのへんで。