Sync folders after provision script has ran?

773 Views Asked by At

Is it possible to have vagrant sync a folder after the provision script is ran?

I need to sync a guest folder, with the host folder, but the host folder must be empty for the provision script to work. The user and group also need to exist. Non of which can exist until the provision script has ran. My provision script creates the user, and group.

The synced folder is a Magento theme. Magento first needs to install before I can sync the directories as Magento will not install into a non empty directory.

Here is my Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.provision "shell", path: "provision.sh"
  config.vm.synced_folder ".", "/vagrant", disabled: true
  config.vm.synced_folder "app/design/frontend", 
              "/var/www/mage/app/design/frontend",
              create: false,
              owner: "mage",
              group: "www-data"
    
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "3000"
  end
end

I've taken a look at triggers, but I can figure out how to use config.vm.synced_folder within a trigger.

Any advice?

1

There are 1 best solutions below

0
On

I've found a solution.

In the Vagrantfile set config.vm.synced_folder to be false like so (also works for nfs):

config.vm.synced_folder "app/design/frontend", 
          "/var/www/mage/app/design/frontend",
          type: "rsync",
          disabled: false,
          owner: "mage",
          group: "www-data"

Now run vagrant up. Once the server provisioning is complete change disabled to true like so:

config.vm.synced_folder "app/design/frontend", 
          "/var/www/mage/app/design/frontend",
          type: "rsync",
          disabled: true,
          owner: "mage",
          group: "www-data"

Now run vagrant reload and the folder will be synced.

The docs say

disabled (boolean) - If true, this synced folder will be disabled and will not be setup. This can be used to disable a previously defined synced folder or to conditionally disable a definition based on some external factor.

Which makes me believe this may be one of the intended ways to solve my problem.

There is a feature request to mount folders post-provision which seems to be closed and not resolved so I don't think there's any other way to this: https://github.com/hashicorp/vagrant/issues/936