Vagrant 手册之 Provisioning - File

原文地址html

Provisioner 名字:"file"git

Vagrant 的 file provisioner 容许将文件或目录从主机上传到客户机。web

File provisioning 文件配置是一种简单的方法,例如,将本地的 ~/.gitconfig 复制到客户机上的 Vagrant 用户主目录,这样每次配置新虚拟机时都没必要运行 git config --globalshell

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

  config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
end

若是想将文件夹上传到客户机系统,能够经过下面的 file provisioner 实现。复制时,客户机的最终文件夹将被替换。注意,若是但愿在客户机上使用相同的文件夹名称,请确保目标路径与主机上的文件夹名称相同。bash

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

  config.vm.provision "file", source: "~/path/to/host/folder", destination: "$HOME/remote/newfolder"
end

~/path/to/host/folder 复制到客户机以前:ssh

folder
    ├── script.sh
    ├── otherfolder
    │   └── hello.sh
    ├── goodbye.sh
    ├── hello.sh
    └── woot.sh

    1 directory, 5 files

~/path/to/host/folder 复制到客户机的 $HOME/remote/newfolder 以后:svg

newfolder
    ├── script.sh
    ├── otherfolder
    │   └── hello.sh
    ├── goodbye.sh
    ├── hello.sh
    └── woot.sh

    1 directory, 5 files

注意,与同步目录不一样,上传的文件或目录不会保持同步。继续上面的例子,若是对本地 ~/.gitconfig 进行了进一步更改,它们将不会当即反映在您上传到客户机的副本中。工具

由 file provisioner 上传的文件以 SSH 或 PowerShell 用户身份完成。这很重要,由于这些用户一般本身没法提高权限。若是想将文件上传到须要提高权限的位置,建议将它们上传到临时位置,而后使用 shell provisioner 将其移动到位。ui

1. 选项

file provisioner 只有两个选项,都是必须的:this

  • source (string):要上传的文件或目录的本地路径。
  • destination (string):用于上传的客户机的远端路径。文件或目录使用 SSH 用户借助 SCP 上传,所以 SSH 用户必须对这个目录具备写权限。SSH 用户默认是“vagrant”,能够经过 vagrant ssh-config 查看。

2. 注意事项

While the 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:
虽然 file provisioner 确实支持尾部斜杠或“全局”,但对于用于在主机和客户机之间复制文件和文件夹的底层工具,这可能会致使一些使人困惑的结果。例如,若是源和目标的尾部斜杠定义以下:

config.vm.provision "file", source: "~/pathfolder", destination: "/remote/newlocation/"

这是在告诉 vagrant 上传远程目录 /remote/newloaction 下的 ~/pathfolder 目录,看起来是这样的:

newlocation
    ├── pathfolder
    │   └── file.sh

    1 directory, 2 files

也能够用下面的定义实现这个目的:

config.vm.provision "file", source: "~/pathfolder", destination: "/remote/newlocation/pathfolder"

另外一个例子是在主机上使用 globing 来抓取文件夹内的全部文件,但不是顶层文件夹自己:

config.vm.provision "file", source: "~/otherfolder/.", destination: "/remote/otherlocation"

file provisioner 会将 ~/otherfolder 下的全部文件包含到新位置 /remote/otherlocation 中。这个想法能够经过简单地让目标文件夹与源文件夹不一样来实现:

config.vm.provision "file", source: "/otherfolder", destination: "/remote/otherlocation"
相关文章
相关标签/搜索