How I simplified my Vagrantfile by reading in the ansible group_vars

I’m working on simplifying the process of creating a new development sites in my bedrock-ansible Vagrant VM.

I run a single VM for all of my development sites. Previously I would add each new site to group_vars/development, then set up the host alias and mount the synced folder in my Vagrantfile.

I got around modifying the Vagrantfile for each site by simply reading in group_vars/development and using that information in the Vagrantifle:

group_vars = YAML.load(File.read('group_vars/development'))
group_vars['wordpress_sites'].each do |site|
    site_name = site['site_name']
    config.hostsupdater.aliases.push(site_name)
    config.vm.synced_folder "../#{site_name}", "/vagrant-nfs-#{site_name}", type: 'nfs'
    config.bindfs.bind_folder "/vagrant-nfs-#{site_name}", "/srv/www/#{site_name}/current", u: 'vagrant', g: 'www-data'
end

Nothing too fancy, but it let me delete 20-odd repetitive lines in my Vagrantfile.

4 Likes

Great idea! I’ll probably document this at some point. Maybe finally start the Wiki up.

One minor thing you can just use YAML.load_file:

group_vars = YAML.load_file('group_vars/development')