LoginSignup
90
85

More than 5 years have passed since last update.

Vagrantの/etc/hosts系プラグインを比べてみた(vagrant-hostmanager / vagrant-hostsupdater / vagrant-hosts)

Last updated at Posted at 2015-02-08

ここに載ってる/etc/hosts系のプラグインについて調べてみた。

どんなプラグインか?

Vagrantfileに書かれたVMのIPアドレスとホスト名から、ホストOSまたはゲストOSの/etc/hostsを設定してくれる。
これらを使うとゲストOSにアクセスするときにIPアドレスではなくホスト名を使うことができる。

大別するとホストOSのhostsを設定するものとゲストOSのhostsを設定するものがある。

ホストOSのhosts設定は、例えばVMでDBサーバーを構築してホストOSからゲストOSへアクセスするような用途で使用する。

ゲストOSのhosts設定は、複数VM構成でDBサーバーとWebサーバーを構築してWebサーバーからDBサーバーを使用する、というようなゲストOSから別のゲストOSをアクセスするような用途で使用する。

プラグイン名 ホストOSの
hosts設定
ゲストOSの
hosts設定
備考
vagrant-hostmaster 最新のVagrant1.7.1で動かなかったので未検証
vagrant-hostmanager
vagrant-hostsupdater
vagrant-hosts
vagrant-flow ちょっと用途が違うようなので未検証

vagrant-hostmanager

ゲストOSとホストOSの/etc/hostsを設定するプラグイン。

インストール

vagrant plugin install vagrant-hostmanager

使い方

Vagrantfileにホスト名とプライベートネットワークでIPアドレスを指定しておく。

Vagrantfile
  config.vm.define "node1" do |node|
    node.vm.hostname = "node1"
    node.vm.network "private_network", ip: "192.168.33.11"
  end
  config.vm.define "node2" do |node|
    node.vm.hostname = "node2"
    node.vm.network "private_network", ip: "192.168.33.12"
  end

VMを起動してからvagrant hostmanagerを実行すると、ゲストOSの/etc/hostsにゲストOSの情報を書き込まれる。

$ vagrant up
Bringing machine 'node1' up with 'virtualbox' provider...
Bringing machine 'node2' up with 'virtualbox' provider...
:
$ vagrant hostmanager
[node1] Updating /etc/hosts file...
[node2] Updating /etc/hosts file...
ゲストOSの/etc/hosts
## vagrant-hostmanager-start
192.168.33.11   node1 
192.168.33.12   node2 
## vagrant-hostmanager-end

デフォルトではvagrant hostmanagerを実行しないと動作しないが、Vagrantfileに以下の行を追加しておけばvagrant up時に自動的にhosts設定を行ってくれる。

Vagrantfile
config.hostmanager.enabled = true

以下の行を追加すると、ホストOSのhostsへも追加してくれる。

Vagrantfile
config.hostmanager.manage_host = true

vagrant destroyするとhostsから削除される。

$ vagrant destroy -f
==> node2: Forcing shutdown of VM...
==> node2: Destroying VM and associated drives...
==> node2: Updating /etc/hosts file on active guest machines...
==> node2: Updating /etc/hosts file on host machine (password may be required)...
==> node1: Forcing shutdown of VM...
==> node1: Destroying VM and associated drives...
==> node1: Updating /etc/hosts file on active guest machines...
==> node1: Updating /etc/hosts file on host machine (password may be required)...

エイリアスにも対応している。

Vagrantfile
node.hostmanager.aliases = %w(alias1 alias2)
192.168.33.11   node1 alias1 alias2

vagrant-hostsupdater

VMの起動/停止を行った時に、ホストOSの/etc/hostsへゲストOSの情報を追加/削除してくれる。
Vagrantfileに特別なオプションを設定する必要がないので便利。

ただしゲストOSのhosts書き換えには未対応。

インストール

vagrant plugin install vagrant-hostsupdater

使い方

Vagrantfileにホスト名とプライベートネットワークでIPアドレスを指定しておく。

Vagrantfile
  config.vm.network :private_network, ip: "192.168.3.10"
  config.vm.hostname = "www.testing.de"
  config.hostsupdater.aliases = ["alias.testing.de", "alias2.somedomain.com"]

vagrant upすると/etc/hostsに以下のような形が追加される。

192.168.3.10  www.testing.de  # VAGRANT: 46578ee9ed1fb5802248fcde434beb13 (default) / c33ad775-b720-403a-942b-b6ae3aa4bdf6
192.168.3.10  alias.testing.de  # VAGRANT: 46578ee9ed1fb5802248fcde434beb13 (default) / c33ad775-b720-403a-942b-b6ae3aa4bdf6
192.168.3.10  alias2.somedomain.com  # VAGRANT: 46578ee9ed1fb5802248fcde434beb13 (default) / c33ad775-b720-403a-942b-b6ae3aa4bdf6

vagrant haltまたはvagrant destoryを実行するとhostsから削除される。

vagrant-hosts

ゲストOSのhostsを設定するプラグイン。

インストール

vagrant plugin install vagrant-hosts

使い方

Vagrantfileにnode.vm.provision :hostsを書いておくと、プロビジョニング実行時に/etc/hostsを設定してくれる。

Vagrantfile
  config.vm.define "node1" do |node|
    node.vm.hostname = "node1"
    node.vm.network "private_network", ip: "192.168.33.11"
    node.vm.provision :hosts
  end
  config.vm.define "node2" do |node|
    node.vm.hostname = "node2"
    node.vm.network "private_network", ip: "192.168.33.12"
    node.vm.provision :hosts
  end

VM未作成の状態からvagrant upして各ゲストOSのhostsを確認してみる。

node1の/etc/hosts
192.168.33.11 node1
node2の/etc/hosts
192.168.33.11 node1
192.168.33.12 node2

あれ?node1のhostsにnode2の情報がない。。。
node1のプロビジョニングが実行されたタイミングではnode2がまだ未作成だからだろうか?

vagrant provisionしてから再度確認するとちゃんと書き込まれていた。

node1の/etc/hosts
192.168.33.11 node1
192.168.33.12 node2
node2の/etc/hosts
192.168.33.11 node1
192.168.33.12 node2

VM以外のホストを追加することもできる。

Vagrantfile
    node.vm.provision :hosts do |provisioner|
      provisioner.autoconfigure = true
      provisioner.add_host '172.16.3.10', ['yum.mirror.local']
    end

感想

vagrant-hostmanagerがゲストOS/ホストOS両方のhosts設定に対応していてオプションも色々あるので一番オススメ。
だけどVagrantfileにオプションを追加しないと自動では設定してくれない。

vagrant-hostsupdaterは特別な設定をしなくてもいいので、ホストOSのhosts設定だけ必要なら使い易い。

というわけで、ホストOSのhosts設定はvagrant-hostsupdater、ゲストOSのhosts設定はvagrant-hostmanagerと2つ併用するのがベストだと思う。

90
85
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
90
85