Vagrant で複数の仮想マシンを管理しているときデフォルトの操作対象を指定する
まず、一つの Vagrantfile で web, app, db という 3 台の仮想マシンを管理している場合を考える。
$ cat Vagrantfile # -*- mode: ruby -*- # vi: set ft=ruby : # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.define :web do |web| web.vm.box = "centos65" end config.vm.define :app do |app| app.vm.box = "centos65" end config.vm.define :db do |db| db.vm.box = "centos65" end end $ vagrant status Current machine states: web running (virtualbox) app running (virtualbox) db running (virtualbox) This environment represents multiple VMs. The VMs are all listed above with their current state. For more information about a specific VM, run `vagrant status NAME`.
この状態だと vagrant コマンドの各操作で、その操作の対象とする仮想マシンを指定する必要がある。
$ vagrant ssh This command requires a specific VM name to target in a multi-VM environment. $ vagrant ssh web vagrant $ cat /etc/redhat-release CentOS release 6.5 (Final)
ただ、主に操作する仮想マシンが決まっている場合には、指定なしで操作できると便利そう。 そんなときは操作したい仮想マシンの設定に primary: true を付ける。
$ cat Vagrantfile # -*- mode: ruby -*- # vi: set ft=ruby : # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.define :web, primary: true do |web| web.vm.box = "centos65" end config.vm.define :app do |app| app.vm.box = "centos65" end config.vm.define :db do |db| db.vm.box = "centos65" end end
primary: true を付けるとその仮想マシンがデフォルトの操作対象として選ばれるようになる。
$ vagrant ssh vagrant $ cat /etc/redhat-release CentOS release 6.5 (Final)