page_adsence

2015年5月27日水曜日

vagrantからVirtualBoxが起動出来なくなる問題の解決方法

Windows + Cygwin + Vagrant + VirtualBoxという環境で開発をしているのですが、
たまに、VirtualBoxを立ち上げていない状態で、Cygwinから「vagrant resume」してしまう事がある。
そうすると下記の様なメッセージが出てきて、VMが立ち上がらない状態になってしまう。(人によってはVirtualBoxが立ち上がってくれる?)

$ vagrant resume
==> default: VM not created. Moving on...

VirtualBoxを後から起動させてみると、VirtualBox上はsuspend状態のまま。
しかしvagrantコマンドでステータスを確認してみると

$ vagrant status
Current machine states:

default                   not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.

VMは作成されていない状態になっている。
ちなみにこの状態で「vagrant up」してもすでに同名のVirtualBox名(Vagrantfile内のvb.name)が存在しているためエラーになる。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos6.4'...
==> default: Matching MAC address for NAT networking...
A VirtualBox machine with the name 'centos' already exists.
Please use another name or delete the machine with the existing
name, and try again.

なぜこんな状態になっているかというと、vagrantとVirtualBoxの関連付けをしているファイルが無い、もしくは中身が空の状態になっている事が原因らしい。
下記のファイルがあるか、中身が書かれているかを確認してみる。(Vagrantfileが置いてある所がvagrant_home)

/path/to/vagrant_home/.vagrant/machines/default/virtualbox/id

試しに確認してみたら、

$ ls -la .vagrant/machines/default/virtualbox/
合計 4
drwxr-xr-x+ 1 kusagaya-naoki Domain Users 0 5月  26 15:31 .
drwxr-xr-x+ 1 kusagaya-naoki Domain Users 0 5月  18 13:11 ..

案の定ファイルは存在していなかった。
なので、ここにファイルを作成してやればいいのですが、VirtualBox側のVMのUUIDを調べる必要がある。
UUIDの調べ方は以下の通り。

VirtualBoxのVMのUUIDの調べ方


1.コマンドプロンプトを立ち上げて、VirtualBoxのインストールディレクトリへ移動

cd C:\Program Files\Oracle\VirtualBox

2.VBoxManageでVirtualBoxにあるVMのUUIDの一覧を確認する。
※vm_nameにはvb.nameの名前が書かれている

C:\Program Files\Oracle\VirtualBox> VBoxManage.exe list vms
"vm_name" {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}

これでVMのUUIDがわかったので、下記のディレクトリ配下にidファイルを作成する。

$ vi /path/to/vagrant_home/.vagrant/machines/default/virtualbox/id

ファイルの先頭に調べたUUIDを書いて保存する。

XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

vagrant側のステータスを確認してみる。

$ vagrant status
Current machine states:

default                   saved (virtualbox)

To resume this VM, simply run `vagrant up`.

not createdからsavedに変わった事を確認して起動してみる。

$ vagrant resume
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2223
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection refused. Retrying...
==> default: Machine booted and ready!

ちゃんと起動しました。
よかった・・・。

2015年5月11日月曜日

Ansibleで変数入りのファイルをリモートマシンへコピーする

Ansibleで開発環境を作成しようとすると、各種設定ファイル等を使い回したくなります。
通常であれば、ホストマシン側に設定ファイルを置いておき、それをリモートマシンへcopyモジュールを利用してコピーするのですが、それだとホストマシンに置いてある設定ファイルをそのまま設置することになってしまいます。
プロジェクトによって書き換えたい部分とかが出てくると思います。ログの吐き出し先とか。
そういった場合はcopyモジュールを使用するのではなく、templateモジュールを使用する事で、元ファイルに入れてある変数を展開した状態でリモートマシンにコピーする事が出来ます。
変数の使い方に関しては割愛します。

変数入りのファイルを用意します。
$ vi files/file.conf
{{ project_name }}

プレイブックに下記の様な行を追加します。
$ vi playbook.yml
- template: src=files/file.conf dest=/etc/file.conf mode=0644

プレイブックを実行します。
ansible-playbook playbook.yml --extra-vars "project_name=test-project"

処理終了後にリモートマシンにコピーしたファイルを確認します。
$ vi /etc/file.conf
test-project

この様な感じで変数展開された状態のファイルが設置されています。