[Vagrant & VirtualBox] Vagrant: network, synced_folder, provisioning

続きです。

[markdown]

> * [Vagrant: + VirtualBox で仮想マシンを作成する | deadwood](https://www.d-wood.com/blog/2013/09/20_4688.html)

## Network に繋げる

方法が3つある。
Vagrantfile

“`ruby
# The url from where the ‘config.vm.box’ box will be fetched if it
# doesn’t already exist on the user’s system.
# config.vm.box_url = “http://domain.com/path/to/above.box”
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing “localhost:8080” will access port 80 on the guest machine.
# config.vm.network :forwarded_port, guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network :private_network, ip: “192.168.33.10”
“`

コメントを外して 192.168.33.10 で繋げる例。

“`ruby
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network :private_network, ip: “192.168.33.10”
“`

“`
% subl Vagrantfile
% vagrant reload
“`

Vagrantfile

> * [Vagrantで簡単仮想マシン構築 | Ryuzee.com](http://www.ryuzee.com/contents/blog/4292)

## 共有フォルダ(synced_folder)を使う

Mac上のプロジェクトフォルダ、つまり Vagrantfile のあるフォルダは、仮想マシン上で共有されている。

/Users/myuser/projects/VB_CentOS
“`
% pwd
/Users/myuser/projects/VB_CentOS
% ls
Vagrantfile
“`

対応する仮想マシン上のディレクトリは以下になる。

/vagrant
“`
[vagrant@localhost ~]$ cd /vagrant
[vagrant@localhost vagrant]$ pwd
/vagrant
[vagrant@localhost vagrant]$ ls
Vagrantfile
“`

使い方として、例えば共有フォルダをドキュメントルートにしてしまう。

“`
$ sudo rm -rf /var/www/html
$ sudo ln -fs /vagrant /var/www/html
“`

nfs mount を利用する。

> * [Vagrant: synced_folder 内のファイル変更が認識されない | deadwood](https://www.d-wood.com/blog/2014/01/30_5357.html)

## Provisioning

Vagrantfile を編集して、仮想マシンが起動した後の設定を自動化できる。
Provisioning のための高度なツールとして、[Chef](http://www.opscode.com/chef/) や [Puppet](http://puppetlabs.com/) がある。

“`ruby
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = “centos64”
# config.vm.provision :shell, :inline => “echo hello world”
config.vm.provision :shell, :path => “provision.sh”
“`

path で別ファイルを指定ができるので、シェルスクリプトで指定してみる。
provision.sh

“`bash
sudo yum -y install httpd
sudo service httpd start
sudo chkconfig httpd on
“`

vagrant up か、起動しているならば vagrant provision で設定を反映できる。

“`
% vagrant provision
“`

確認する。

“`
% vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ sudo service httpd status
httpd (pid 1867) is running…
“`

[/markdown]