Vagrantfile<2>--Vagrantfile

Introduction

Vagrantfile is a configuration file:

  • It describe and how to configure and provision these VMs.
  • The syntax of Vagrantfiles is Ruby
  • Use version control to share environment with team members.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Vagrant.configure("2") do |config|
config.vm.define :ui do |ui|
ui.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.name = "ui"
vb.cpus = "4"
end
ui.vm.box = "centos/7"
ui.vm.hostname = "ui"
ui.vm.network "private_network", ip: "192.168.50.4"
end

config.vm.define :server do |server|
server.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.name = "ui"
vb.cpus = "4"
end
server.vm.box = "centos/7"
server.vm.hostname = "server"
server.vm.network "private_network", ip: "192.168.50.5"
end
end

box

1
server.vm.box = "centos/7"

It defines what box(system) for this VM. Find a box here public Vagrant box catalog

provider

1
2
3
4
5
ui.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.name = "ui"
vb.cpus = "4"
end

Used to modify settings which are specific to a certain provider

hostname

1
server.vm.hostname = "server"

Set hostname of this VM

network

How VM connect to the network. This one use private_network with a static IP. Options:

  1. private_network

    1
    server.vm.network "private_network", ip: "192.168.50.5"
  2. public_network

  3. forwarded_port

    1
    config.vm.network "forwarded_port", guest: 80, host: 8080

We can then open browser to localhost:8080 and browse the website, while all actual network data is being sent to the guest.

provision

  • Install and configure software automatically
  • It happens when vagrant up vagrant provision vagrant reload --provision
  1. file

    1
    config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"

    It upload this file/directory to new VM. So I don’t need to git config --global every time!!

  2. shell

    1
    config.vm.provision "shell", inline: "echo Hello, World"

    Run inline script

    1
    config.vm.provision "shell", path: "script.sh"

    Run external script

synced_folder

1
config.vm.synced_folder "src/", "/srv/website"
  • Didn’t use it in example because VirtualBox has its own share folder setting. This folders on your host machine can be synced to and from the guest machine
  • So useful!!!!!! Some personal setting can be sent to all VM. So I don’t need to worry about changing an alias at one VM, then have to edit all others

others

  • push

    • As of version 1.7, Vagrant is capable of deploying or “pushing” application code in the same directory as your Vagrantfile to a remote such as an FTP server.
  • plugin

    • we only have one called vagrant-libvirt