Provisioning (vm.provision): How to customize the box
Okay, this is great. We created a mini web server locally on our computer using Vagrant. But, Vagrant is supposed to be easy!
Maybe there's a way to automate all this? There is!
Vagrantfile can run shell commands after it mounts the VM and builds your "box".
Provisioning simply refers to "building" or "adding to" the original
box file. So, for example, you could automatically install Apache.
There's a couple of ways to do this. We can just do this with
shell provisioning just like we were.
Shell Provisioning (Inline)
You can write your
bash commands inline for shell provisioning to work. You do this directly in the Vagrantfile.
config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install apache2 -y SHELL
vagrant up, your Vagrant will install apache. See how we're creeping closer to full automation?
Shell Provisioning (Path)
Another way is to have all your shell scripts in a file. For example:
#!/bin/bash # Update Packages apt-get update # Install Apache apt-get install apache2 -y
Then, in your
Vagrantfile, you can reference it like so:
config.vm.provision "shell", path: "install.sh"
When I do this, I like to turn "root privileges" off. Mainly because I had an issue installing something (I think)
Node once using this method. So:
#!/bin/bash sudo apt-get update sudo apt-get install apache2 -y
config.vm.provision "shell", path: "install.sh", privileged: false