winodwsでvagrant-hostsupdate導入後にvagrant upできなくなった失敗談と対処法。

#この記事に含まれる内容。

- `vagrant plugin install vagrant-hostsupdater`の説明。
- 上記のpluginを入れた事で`vagrant up`できない問題の発生。
- 以上をふまえた際の解決策と考察


#開発環境(ホスト)
|ホストOS|windows8.1 64bit|
|:--|--:|
|vagrant|ver 1.81|
|Oracle VM VirtualBox|バージョン 5.0.12|
|git|ver 2.8.|
|アカウントの種類|標準ユーザ|



#結論
`vagrant-hostsupdate`を入れることで`vagrant up`した際にホストOSのhostsファイルに自動で書き込みを行う事が可能になるが、Windowsではhostsファイルの編集には__管理者権限(Administrator)__でなければ出来ない為、はじかれてしまい、結果エラー(Permission denied)が発生した。

```
Permission denied @ rb_sysopen - 
C:/WINDOWS/system32/drivers/etc/hosts (Errno::EACCES)                                                  

        from C:/Users/chi/.vagrant.d/gems/gems/vagrant-hostsupdater-1                                                  .0.2/lib/vagrant-hostsupdater/HostsUpdater.rb:98:in `open'
        from C:/Users/chi/.vagrant.d/gems/gems/vagrant-hostsupdater-1                                                  .0.2/lib/vagrant-hostsupdater/HostsUpdater.rb:98:in `addToHosts'
        from C:/Users/chi/.vagrant.d/gems/gems/vagrant-hostsupdater-1                                                  .0.2/lib/vagrant-hostsupdater/HostsUpdater.rb:47:in `addHostEntries'
        from C:/Users/chi/.vagrant.d/gems/gems/vagrant-hostsupdater-1                                                  .0.2/lib/vagrant-hostsupdater/Action/UpdateHosts.rb:17:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/check_guest_additions.rb:49:in `cal                                                  l'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/Users/chi/.vagrant.d/gems/gems/vagrant-vbguest-0.11.0                                                  /lib/vagrant-vbguest/middleware.rb:31:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/customize.rb:40:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/wait_for_communicator.rb:71:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/customize.rb:40:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/boot.rb:18:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/customize.rb:40:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/sane_defaults.rb:38:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/set_hostname.rb:16:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/forward_ports.rb:31:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/network_fix_ipv6.rb:41:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/network.rb:122:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/clear_network_interfaces.rb:26:in `                                                  call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/prepare_nfs_settings.rb:18:in `call                                                  '
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/synced_folders.rb:87:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/synced_folder_cleanup.rb:28:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/synced_folders/nfs/action_cleanup.rb:19:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/prepare_nfs_valid_ids.rb:12:in `cal                                                  l'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/handle_forwarded_port_collisions.rb:49:in `c                                                  all'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/prepare_forwarded_port_collision_pa                                                  rams.rb:30:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/env_set.rb:19:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/Users/chi/.vagrant.d/gems/gems/vagrant-omnibus-1.4.1/                                                  lib/vagrant-omnibus/action/install_chef.rb:38:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/Users/chi/.vagrant.d/gems/gems/vagrant-cachier-1.2.1/                                                  lib/vagrant-cachier/action/install_buckets.rb:14:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/provision.rb:80:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/Users/chi/.vagrant.d/gems/gems/vagrant-cachier-1.2.1/                                                  lib/vagrant-cachier/action/configure_bucket_root.rb:20:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/clear_forwarded_ports.rb:15:in `cal                                                  l'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/set_name.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/clean_machine_folder.rb:17:in `call                                                  '
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/check_accessible.rb:18:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builder.rb:116:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/runner.rb:66:in `block in run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/util/busy.rb:19:in `busy'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/runner.rb:66:in `run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/call.rb:53:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builder.rb:116:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/runner.rb:66:in `block in run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/util/busy.rb:19:in `busy'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/runner.rb:66:in `run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/call.rb:53:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builder.rb:116:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/runner.rb:66:in `block in run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/util/busy.rb:19:in `busy'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/runner.rb:66:in `run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/call.rb:53:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/box_check_outdated.rb:78:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/builtin/config_validate.rb:25:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl                                                  ugins/providers/virtualbox/action/check_virtualbox.rb:17:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li                                                  b/vagran


```

#ことの始まり
借りているレンタルサーバ(ロリポップ!)には標準から__WordPress__をインストールしてくれる環境が整っており、どうせなら仮想環境を整えてからアップローダしようと思い、今朝からWordPressで色々見ていた。見ていく内にどうやらVagrantのboxとしてWordPressがセットになっている__[VCCW](http://vccw.cc/)__というものがあることを知り、早速いじって見る事に。
![vccw.png](https://qiita-image-store.s3.amazonaws.com/0/64829/7cd71dfc-78c2-82b9-d239-b464b1a9992a.png)


そこで以下のサイトを参考にして環境構築を始める。
>
[VCCWによるWordPress用ローカル環境構築＆ハマりやすいポイント](http://tonari-it.com/vccw/#vagrant-hostsupdater)

サイト通りにVCCWを導入するにあたって、まずは`vagrant-hostsupdater`が必要である事を知りインストールする。

#そもそもvagrant-hostsupdateとは?
公式より引用
[Vagrant Host Manager](https://github.com/devopsgroup-io/vagrant-hostmanager)

>
This plugin adds an entry to your /etc/hosts file on the host system.
On up, resume and reload commands, it tries to add the information, if its not already existant in your hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit the file.
On halt, destroy, and suspend, those entries will be removed again. By setting the config.hostsupdater.remove_on_suspend = false, suspend and halt will not remove them.

意訳
>
このプラグインは、あなたのホスト・システム上`/etc/hosts`ファイルにエントリ(ゲストOSのホスト名とipアドレス)を追加します。立ち上げ(`vagrant up`)や再起動(`vagrant resume`)、再ロード(`vagrant reload`)すると、ホストOSのhostsファイルにエントリがない場合は情報を追加しようとします。追加する場合はファイル編集に`sudo`を使用するので管理者パスワードの入力を求められます。停止(`vagrant halt`)、破壊(`vagrant vagrant destroy`)、一時停止(`vagrant suspend`)でこれらのエントリが再び削除されます。 しかし`config.hostsupdater.remove_on_suspend`を設定することにより、`vagrant halt`や`vagrant suspend`をしてもエントリが削除されないようにする事も可能です。

以上よりvagrant-hostsupdateとはvagrantのpluginの一種であり、VagrantのゲストOSを起動した際に、ホストOSのhostsファイルに__ゲストOSのホスト名・IP__を追加するpluginであることが理解できます。

インストール手順

```
vagrant plugin install vagrant-hostsupdater
```

設定ファイル
VagrantfaileからIPアドレスとhostnameの設定が行えます。

基本的な使い方としてはホスト名とIPアドレスを以下のように設定して起動します。

```
config.vm.network "private_network", ip: "192.168.00.00"
config.vm.hostname = "vm-node"
```

##使い方
`vagrant up`後にhostsファイルに設定したゲストOSのIPアドレスが書き込まれ、ホストOS上からSSHクライアントで`ssh vagrant@hogehoge.devel`で接続が出来る事が可能となります。


参考にしたサイト
>
- [VagrantでゲストOSを起動した際に、ホストOSのhostsファイルにゲストOSのホスト名・IPを追加・削除してくれるVagrantプラグイン「Vagrant::Hostsupdater」](https://mistymagich.wordpress.com/2013/09/02/vagrant%E3%81%A7%E3%82%B2%E3%82%B9%E3%83%88os%E3%82%92%E8%B5%B7%E5%8B%95%E3%81%97%E3%81%9F%E9%9A%9B%E3%81%AB%E3%80%81%E3%83%9B%E3%82%B9%E3%83%88os%E3%81%AEhosts%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB/)
- [Vagrantプラグインの使い方（vbguest, hostmanager, sahara, snapshot）
Vagrant](http://www.task-notes.com/entry/20150714/1436842800)

#エラー発生と対策。
上記に述べた通り、原因は`Permission denied`であるため、Winodwsユーザであれば管理者権限に書き換えるか、ホストOSがLinuxであればsudo権限で実行すべきであると考える。

また自分の環境ではこのErrorはVCCWを導入するときにエラーが発生したが、__既にある`vagran box`を`vagrant up`する際にもエラーが発生した__為、vagrant-hostsupdateを導入する__競合リスク__が大きいと考え、`vagrant plgin list`からアンインストールした。結果hostsファイルに直接書き込む事で無事VCCWが導入でき、他の`vagrant box`も元通り`vagrant up`する事が出来た。

#まとめ

- 今回軽いWordPressを揃えるVCCWの環境構築をするだけで、1日がおわってしまった...。
- サイトを信用してそのままやるのもいいが、なぜこれが必要なのか?と疑い自分で調べるべきだったと反省。
- GWの一日目が環境構築で終わる虚しさ...

_※追記 2016 4/30_
同じErrorで捕まった例。
こちらはvagrant のversionによるエラーだそうです。
環境によっては色んな要因がありそう

[Vagrantのプラグイン(vagrant-hostsupdater)のインストールにコケたことがあった。](http://qiita.com/naru0504/items/58011181f2c271808c7f)