9

How do I share a single file, instead of sharing a whole folder like

config.vm.synced_folder "host/folder", "box/folder"

?

In other words is there a way to have something like:

config.vm.synced_folder "host/folder/file.conf", "box/folder/file.conf"
Thorben Croisé
  • 12,407
  • 8
  • 39
  • 50
  • Why not share the whole folder? Or have a folder on the host containing just the one file? – ydaetskcoR Apr 01 '15 at 16:53
  • There are already other files present in the location box/folder which I don't want to override. I only want to "override" the single file – Thorben Croisé Apr 02 '15 at 11:51
  • @ydaetskcoR I also have the following case : I want to edit a `nginx.conf` config file, keep the config file in my repository but not import the whole folder. – nha Jun 30 '15 at 09:36

5 Answers5

5

The accepted answer (by cdmo) didn't work for me, but was close and led me to the right solution, so cheers. To copy just one file I needed to change it to:

config.vm.synced_folder "c:/hostpath/", "/guestpath/", type: "rsync",
  rsync__args: ["-r", "--include=file.text", "--exclude=*"]

Mind the arguments and their order, -r MUST be present and --include MUST precede --exclude.

If needed, instead of -r option you may use -a option which combines -r with several other options for preservation of permissions, ownership etc. (see rsync help).

My testing configuration: Vagrant 2.0.2/Win10/VirtualBox 5.2.6

jedik
  • 1,144
  • 11
  • 12
  • Thanks for your answer, to elaborate on it - here is a better explanation of all of the options for rsync: https://linux.die.net/man/1/rsync – domdambrogia Oct 03 '18 at 17:35
  • Please consider changing first line to: ````config.vm.synced_folder "/hostpath/", "/guestpath/", type: "rsync",```` not everybody is using Windows, and that could cause confusion – Jose Paez Jan 29 '20 at 18:24
4

Use the include flag in your rsync args array:

config.vm.synced_folder "host/folder/", "box/folder/", type: "rsync",
    rsync__args: ["--include=file.conf"]
cdmo
  • 1,239
  • 2
  • 14
  • 31
  • 1
    include is not documented https://www.vagrantup.com/docs/synced-folders/rsync.html and it is not working – fico7489 Dec 24 '18 at 11:15
4

You can use the file provisioner to do this:

Vagrant.configure("2") do |config|
  # ... other configuration

  config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
end
slm
  • 15,396
  • 12
  • 109
  • 124
  • 4
    This doesn't keep the file synced, so it's not really "sharing." It's copying. – Peter Ajtai Mar 25 '18 at 16:47
  • I would suggest changing the destination to "some/absolute/path/to/the/file" see if that works for someone, for now jedik's answer is hands down the best (well documented and it works) – Jose Paez Jan 29 '20 at 19:03
  • @JosePaez the example as shown is straight from the document that was linked in my A'er. When I tried this back in 2017 it was working for me. – slm Jan 29 '20 at 21:18
2

You can't synchronize a single file. But maybe you can achieve the same effect by synchronizing the folder using RSync with the rsync__exclude option:

rsync__exclude (string or array of strings) - A list of files or directories to exclude from the sync. The values can be any acceptable rsync exclude pattern.

Vagrant by default utilizes the file/folder synchronization mechanisms offered by the provider technologies (e.g., VirtualBox, VMWare, etc.). If the providers can't do synchronization on a per-file basis, then Vagrant can't either. Vagrant can also use NFS, RSync, or SMB for file synchronization. However, looking at the code, it would appear that each expects the per-folder paradigm.

Another possibility is to use one of the provisioning options to selectively synchronize the appropriate file(s). If you want to take the easy way out you could use the shell provisioner to just copy the file you need. For something more interesting, this is a reasonable guide to getting started with Puppet in Vagrant, and you might be interested in the puppet file Type.

0

As Benjamin Mosior explained, you can only synchronise a whole folder.

As a workaround, I ended up synchronising the whole folder to a temporary folder and in a later step creating a symlink where I need it to the single file inside this temporary folder.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Thorben Croisé
  • 12,407
  • 8
  • 39
  • 50