Don’t use symlinks in Vagrant for config files

I’ve been building a lot of Vagrant machines lately, and I’m learning a few things to make the process easier/optimal.

One of the things I recently discovered was that you should avoid using symlinks for your config files, if you’re using a provisioner like Puppet. I had gotten into the habit of just symlinking config files like /etc/php.ini, so if I updated the file I could just reload PHP instead of running vagrant provision again (to copy over my updated file).

The problem with this approach comes when you use restart/reload your Vagrant VM. Provisioning scripts are no longer run every time the VM starts, only on the initial vagrant up. When the VM restarts, your service will try to start (Say Nginx), but it’s config files will be missing as services start before Vagrant has mounted the shared folders (Meaning your symlinks are temporarily broken). Therefore, I recommend using the source attribute instead of the target attribute with Puppet (Puppet will ensure the file contents remains the same).

One other tip (A Puppet one) is that you can recursively copy over files from a directory in Puppet, using the recurse option. See this example:

file { '/etc/nginx':
  notify => Service['nginx'],
  require => Package['nginx'],
  ensure => 'directory',
  source => '/vagrant/vagrant/puppet/nginx',
  recurse => true,
}

Makes it a lot easier to write provisioning scripts!

Of course, you could also create an upstart job and have it start on vagrant-mounted.

One Reply to “Don’t use symlinks in Vagrant for config files”

  1. Can you give us an example?

    I build a nginx server with phuphpet/vagrant.
    now i want to move a lot of my user configs from my vm to my host.

    At the moment i did it with a mount but than i’ve got exactly that problem. I have to restart. I want to hold all files, databases, php/db & webserver configs on host, so i can provision or move the vm simply. Or should i do it with vagrant package?

    Greetings from Germany.

Leave a Reply