2014-02-25 16:33:08 +00:00
|
|
|
---
|
2016-01-19 18:08:53 +00:00
|
|
|
layout: "docs"
|
2014-02-25 16:33:08 +00:00
|
|
|
page_title: "File Uploads - Provisioning"
|
|
|
|
sidebar_current: "provisioning-file"
|
2016-01-19 18:08:53 +00:00
|
|
|
description: |-
|
|
|
|
The Vagrant file provisioner allows you to upload a file or directory from the
|
|
|
|
host machine to the guest machine.
|
2014-02-25 16:33:08 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# File Provisioner
|
|
|
|
|
|
|
|
**Provisioner name: `"file"`**
|
|
|
|
|
2016-01-19 18:08:53 +00:00
|
|
|
The Vagrant file provisioner allows you to upload a file or directory from the
|
|
|
|
host machine to the guest machine.
|
2014-02-25 16:33:08 +00:00
|
|
|
|
|
|
|
File provisioning is a simple way to, for example, replicate your local
|
|
|
|
~/.gitconfig to the vagrant user's home directory on the guest machine so
|
2016-01-19 18:08:53 +00:00
|
|
|
you will not have to run `git config --global` every time you provision a
|
2014-02-25 16:33:08 +00:00
|
|
|
new VM.
|
|
|
|
|
2018-07-28 10:03:02 +00:00
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
# ... other configuration
|
2014-07-05 13:13:20 +00:00
|
|
|
|
2018-07-28 10:03:02 +00:00
|
|
|
config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
|
|
|
|
end
|
|
|
|
```
|
2014-07-05 13:13:20 +00:00
|
|
|
|
2017-08-30 22:48:46 +00:00
|
|
|
If you want to upload a folder to your guest system, it can be accomplished by
|
|
|
|
using a file provisioner seen below. When copied, the resulting folder on the guest will
|
|
|
|
replace `folder` as `newfolder` and place its on the guest machine. Note that if
|
|
|
|
you'd like the same folder name on your guest machine, make sure that the destination
|
|
|
|
path has the same name as the folder on your host.
|
|
|
|
|
2018-07-28 10:03:02 +00:00
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
# ... other configuration
|
2017-08-30 22:48:46 +00:00
|
|
|
|
2018-07-28 10:03:02 +00:00
|
|
|
config.vm.provision "file", source: "~/path/to/host/folder", destination: "$HOME/remote/newfolder"
|
|
|
|
end
|
|
|
|
```
|
2017-08-30 22:48:46 +00:00
|
|
|
|
|
|
|
Prior to copying `~/path/to/host/folder` to the guest machine:
|
|
|
|
|
|
|
|
folder
|
|
|
|
├── script.sh
|
|
|
|
├── otherfolder
|
|
|
|
│ └── hello.sh
|
|
|
|
├── goodbye.sh
|
|
|
|
├── hello.sh
|
|
|
|
└── woot.sh
|
|
|
|
|
|
|
|
1 directory, 5 files
|
|
|
|
|
|
|
|
After to copying `~/path/to/host/folder` into `$HOME/remote/newfolder` to the guest machine:
|
|
|
|
|
|
|
|
newfolder
|
|
|
|
├── script.sh
|
|
|
|
├── otherfolder
|
|
|
|
│ └── hello.sh
|
|
|
|
├── goodbye.sh
|
|
|
|
├── hello.sh
|
|
|
|
└── woot.sh
|
|
|
|
|
|
|
|
1 directory, 5 files
|
|
|
|
|
2015-09-25 13:14:12 +00:00
|
|
|
Note that, unlike with synced folders, files or directories that are uploaded
|
|
|
|
will not be kept in sync. Continuing with the example above, if you make
|
|
|
|
further changes to your local ~/.gitconfig, they will not be immediately
|
|
|
|
reflected in the copy you uploaded to the guest machine.
|
2014-02-25 16:33:08 +00:00
|
|
|
|
2015-11-19 02:13:37 +00:00
|
|
|
The file uploads by the file provisioner are done as the
|
|
|
|
_SSH or PowerShell user_. This is important since these users generally
|
2016-01-19 18:08:53 +00:00
|
|
|
do not have elevated privileges on their own. If you want to upload files to
|
2015-11-19 02:13:37 +00:00
|
|
|
locations that require elevated privileges, we recommend uploading them
|
|
|
|
to temporary locations and then using the
|
2016-01-19 18:08:53 +00:00
|
|
|
[shell provisioner](/docs/provisioning/shell.html)
|
2015-11-19 02:13:37 +00:00
|
|
|
to move them into place.
|
|
|
|
|
2014-02-25 16:33:08 +00:00
|
|
|
## Options
|
|
|
|
|
|
|
|
The file provisioner takes only two options, both of which are required:
|
|
|
|
|
2015-09-25 13:14:12 +00:00
|
|
|
* `source` (string) - Is the local path of the file or directory to be
|
|
|
|
uploaded.
|
2014-02-25 16:33:08 +00:00
|
|
|
|
|
|
|
* `destination` (string) - Is the remote path on the guest machine where
|
2015-09-25 13:14:12 +00:00
|
|
|
the source will be uploaded to. The file/folder is uploaded as the SSH user
|
|
|
|
over SCP, so this location must be writable to that user. The SSH user can be
|
2014-02-25 16:33:08 +00:00
|
|
|
determined by running `vagrant ssh-config`, and defaults to "vagrant".
|
2017-08-30 22:48:46 +00:00
|
|
|
|
|
|
|
## Caveats
|
|
|
|
|
|
|
|
While the file provisioner does support trailing slashes or "globing", this can
|
|
|
|
lead to some confusing results due to the underlying tool used to copy files and
|
|
|
|
folders between the host and guests. For example, if you have a source and
|
|
|
|
destination with a trailing slash defined below:
|
|
|
|
|
2018-07-28 10:03:02 +00:00
|
|
|
```ruby
|
|
|
|
config.vm.provision "file", source: "~/pathfolder", destination: "/remote/newlocation/"
|
|
|
|
```
|
2017-08-30 22:48:46 +00:00
|
|
|
|
|
|
|
You are telling vagrant to upload `~/pathfolder` under the remote dir `/remote/newlocation`,
|
|
|
|
which will look like:
|
|
|
|
|
|
|
|
newlocation
|
|
|
|
├── pathfolder
|
|
|
|
│ └── file.sh
|
|
|
|
|
|
|
|
1 directory, 2 files
|
|
|
|
|
|
|
|
This behavior can also be achieved by defining your file provisioner below:
|
|
|
|
|
2018-07-28 10:03:02 +00:00
|
|
|
```ruby
|
|
|
|
config.vm.provision "file", source: "~/pathfolder", destination: "/remote/newlocation/pathfolder"
|
|
|
|
```
|
2017-08-30 22:48:46 +00:00
|
|
|
|
|
|
|
Another example is using globing on the host machine to grab all files within a
|
|
|
|
folder, but not the top level folder itself:
|
|
|
|
|
2018-07-28 10:03:02 +00:00
|
|
|
```ruby
|
|
|
|
config.vm.provision "file", source: "~/otherfolder/.", destination: "/remote/otherlocation"
|
|
|
|
```
|
2017-08-30 22:48:46 +00:00
|
|
|
|
|
|
|
The file provisioner is defined to include all files under `~/otherfolder`
|
|
|
|
to the new location `/remote/otherlocation`. This idea can be achieved by simply
|
|
|
|
having your destination folder differ from the source folder:
|
|
|
|
|
2018-07-28 10:03:02 +00:00
|
|
|
```ruby
|
|
|
|
config.vm.provision "file", source: "/otherfolder", destination: "/remote/otherlocation"
|
|
|
|
```
|