Vagrantfile best practices for configuring environment, .vimrc. .bashrc, etc

So I’m new to Vagrant and I’m reading through “Hands-on DevOps with Vagrant”. I’m also looking at skillshare and other websites to expand my knowledge base.

One thing I would like to do is be able to configure a new VM with certain basic staples. For instance I have a .vimrc file I use with all my systems. With Vagrant, I’ve simply used

config.vm.provision "file", source: "~/.vimrc", destination: "/home/vagrant/.vimrc"

to transfer. However, when I make further changes to my Vagrantfile I don’t want to necessarily retransfer the .vimrc file when I include the –provision option.

Here is another situation. my .bashrc settings. I don’t want to transfer my whole .bashrc file from my other systems, but want to pass my alias commands to the .bashrc file that’s created on the new VM. I’ve done that by echoing the alias’s to the .bashrc file, however if I include the –provision option they it will add all the alias’s again.

Are there command line arguments when I run vagrant up --provision or vagrant reload --provision that I can use to identify which provisions I want to trigger? Or something of that nature?

Another way to keep files synced on your host and guest machines is to use synced folders. In your Vagrantfile that will look something like

  config.vm.synced_folder "~/.vmrc", "/home/vagrant/.vimrc"

Using synced folders, any change that happens on the host will also be available to the guest (and vice versa).

Vagrant has a provision-with option that you can use to specify which provisioners to run.

So, for a Vagrantfile with provisioners

config.vm.provision "file", name: "vimrc",  source: "~/.vimrc", destination: "/home/vagrant/.vimrc"
config.vm.provision "file", name: "bashrc",  source: "~/.bashrc", destination: "/home/vagrant/.bashrc"
config.vm.provision "shell", name: "hello", inline: "echo hello"

You can run just the vimrc provisioner with the command vagrant provision --provision-with vimrc

1 Like