Some neat Vagrant tricks

I’ve been using Vagrant for a while now (though I’m not a Vagrant expert at all) and I’ve found myself setting some configuration over and over again as well performing some tasks on every new virtual machine the very same way I’d do on a physical one.

Things I like to set on a new vagrant-created vm:

  • have virtualboc guest extensions installed by default, but disable update-on-creation (it slows down machine startup considerably)
  • set a host-only network interface in the private network section of the Vagrant file
  • install screen
  • load my ssh public key for user vagrant
  • load my ssh public key for user root

I was thinking that there should be a better way to do all this, and provisioning certainly comes to my mind.

However, I just wanted all of the above stuff to be, basically, the default.

While reading the f… abulous manual, I discovered that Vagrantfiles has some sort of incremental way of loading settings, that is: the settings you define in your Vagrant file will not just be applied to a blank set of settings (pun not intended). Quite the contrary: such settings will be applied on top of defaults you can factor out and put in special places. One of such special places is the ~/.vagrant.d.

Thus, I now have in my ~/.vagrant.d/Vagrantfile:

Vagrant.configure("2") do |config| = "centos/7" "private_network", type: "dhcp"
  config.vbguest.auto_update = false

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"

  config.vm.provision "shell", inline: %q{                                                                              
     if [ -e /var/log/provisioned ] ; then                                                                              
        echo "inline provisioning script ran already, skipping."                                                        
     echo "Updating yum cache ... "                                                                                     
     sudo yum makecache                                                                                                 
     echo "Installing gnu screen ..."                                                                                   
     sudo yum install -y screen                                                                                         
     echo "Installing GNU/Emacs (no X) ... "                                                                            
     #sudo yum install -y emacs-nox                                                                                     
     echo "Loading public key for user vagrant..."                                                                      
     echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB.... VSkr1CtJZGf myuser@mymac" >>  ~vagrant/.ssh/authorized_keys                     
     echo "Loading public key for user `whoami`..."                                                                     
     sudo mkdir -p ~/.ssh                                                                                               
     echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB.... VSkr1CtJZGf myuser@mymac" >>  ~/.ssh/authorized_keys                            
     touch /var/log/provisioned                                                                                         

Such code allows me to do some nice things:

  • set the default memory for a new vm to 2GB
  • disable virtualbox guest additions auto update
  • create a private network interface, with a dynamic address
  • if the box is based on the “centos/7” image:
    • update the yum cache
    • install screen
    • install emacs-nox
    • load my ssh key for both users vagrants and root
    • show the network connection information

Only thing: I am assuming a centos-based box, because that’s what I use the most.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.