はやさがたりない。

へっぽこぷろぐらまのメモログ

vagrantで複数仮想マシンの実行

Vagrantfileに追記
こうすることでweb,dbそれぞれの仮想マシンが実行される
config.vm.boxに記述したOSをインポートしてくれて
webサーバ用のプロビジョニング、dbサーバ用のプロビジョニング我実行される

  config.vm.define "web" do |web|
    web.vm.provision "shell" , inline: "hostname"
    web.vm.provision "shell" , inline: "echo web"
  end

  config.vm.define "db" do |db|
    db.vm.provision "shell" , inline: "hostname"
    db.vm.provision "shell" , inline: "echo db"
  end

実行するとこんな感じ。

$ vagrant up
Bringing machine 'web' up with 'virtualbox' provider...
Bringing machine 'db' up with 'virtualbox' provider...
==> web: Importing base box 'centos6.5'...
==> web: Matching MAC address for NAT networking...
==> web: Setting the name of the VM: ansible_web_1402146864324_22420
==> web: Fixed port collision for 22 => 2222. Now on port 2200.
==> web: Clearing any previously set network interfaces...
==> web: Preparing network interfaces based on configuration...
    web: Adapter 1: nat
==> web: Forwarding ports...
    web: 22 => 2200 (adapter 1)
==> web: Booting VM...
==> web: Waiting for machine to boot. This may take a few minutes...
    web: SSH address: 127.0.0.1:2200
    web: SSH username: vagrant
    web: SSH auth method: private key
    web: Warning: Remote connection disconnect. Retrying...
    web: Warning: Remote connection disconnect. Retrying...
    web: Warning: Remote connection disconnect. Retrying...
==> web: Machine booted and ready!
==> web: Checking for guest additions in VM...
==> web: Mounting shared folders...
    web: /vagrant => /Users/akira/vagrant/ansible
==> web: Running provisioner: shell...
    web: Running: inline script
==> web: vagrant-centos65.vagrantup.com
==> web: Running provisioner: shell...
    web: Running: inline script
==> web: web
==> db: Importing base box 'centos6.5'...
==> db: Matching MAC address for NAT networking...
==> db: Setting the name of the VM: ansible_db_1402146904643_94627
==> db: Fixed port collision for 22 => 2222. Now on port 2201.
==> db: Clearing any previously set network interfaces...
==> db: Preparing network interfaces based on configuration...
    db: Adapter 1: nat
==> db: Forwarding ports...
    db: 22 => 2201 (adapter 1)
==> db: Booting VM...
==> db: Waiting for machine to boot. This may take a few minutes...
    db: SSH address: 127.0.0.1:2201
    db: SSH username: vagrant
    db: SSH auth method: private key
    db: Warning: Remote connection disconnect. Retrying...
    db: Warning: Remote connection disconnect. Retrying...
==> db: Machine booted and ready!
==> db: Checking for guest additions in VM...
==> db: Mounting shared folders...
    db: /vagrant => /Users/akira/vagrant/ansible
==> db: Running provisioner: shell...
    db: Running: inline script
==> db: vagrant-centos65.vagrantup.com
==> db: Running provisioner: shell...
    db: Running: inline script
==> db: db

ステータスを見てみよう。
ばっちり2台起動していることがわかる。

$ vagrant status
Current machine states:

web                       running (virtualbox)
db                        running (virtualbox)

個々の仮想マシンに対してコマンド実行するときは後ろに名前を指定する
複数指定することも可能っぽい。

$ vagrant status web
Current machine states:

web                       running (virtualbox)
$ vagrant status db
Current machine states:

db                        running (virtualbox)
$ vagrant status web db
Current machine states:

web                       running (virtualbox)
db                        running (virtualbox)

おまけ

Vagrantfileで構築できるとはいえいろんな人に渡したときに毎回プロビジョニングさせてたら もったいないなーとおもったらやっぱりBoxを自作できるのか。
ちな、未確認

vagrant package
vagrant package --base my_new_box
vagrant package --base my_new_box --vagrantfile=Vagrantfile