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 | Vagrant.configure("2") do |config| |
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 | ui.vm.provider "virtualbox" do |vb| |
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:
private_network
1
server.vm.network "private_network", ip: "192.168.50.5"
public_network
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
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!!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
- we only have one called