原文地址

Provisioner 名字:"file"

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

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

Vagrant.configure("2") do |config|
# ... other configuration config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
end

如果想将文件夹上传到客户机系统,可以通过下面的 file provisioner 实现。复制时,客户机的最终文件夹将被替换。注意,如果希望在客户机上使用相同的文件夹名称,请确保目标路径与主机上的文件夹名称相同。

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 复制到客户机之前:

    folder
├── script.sh
├── otherfolder
│ └── hello.sh
├── goodbye.sh
├── hello.sh
└── woot.sh 1 directory, 5 files

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

    newfolder
├── script.sh
├── otherfolder
│ └── hello.sh
├── goodbye.sh
├── hello.sh
└── woot.sh 1 directory, 5 files

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

由 file provisioner 上传的文件以 SSH 或 PowerShell 用户身份完成。这很重要,因为这些用户通常自己无法提升权限。如果想将文件上传到需要提升权限的位置,建议将它们上传到临时位置,然后使用 shell provisioner 将其移动到位。

1. 选项

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

  • 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"

Vagrant 手册之 Provisioning - File的更多相关文章

  1. Vagrant 手册之 Provisioning - file 配置程序

    原文地址 Provisioner 命令:"file" 通过 file 配置程序可以上传宿主机的文件或目录到虚拟机中. 使用场景:将宿主机的 ~/.gitconfig 复制到虚拟机中 ...

  2. Vagrant 手册之 Provisioning - 基本用法

    原文地址 虽然 Vagrant 提供了用于配置虚拟机的多个选项,但是有标准用法,好多知识点对这个 provisioner 是通用的. 配置 首先,Vagrantfile 中配置的每个 provisio ...

  3. Vagrant 手册之 Provisioning - 概述

    原文地址 通过 Vagrant 中的 provisioner 配置程序,可以在使用 vagrant up 启动虚拟机时,在虚拟机上执行安装软件.更改配置等操作. box 通常是通用的,而每个项目总有自 ...

  4. Vagrant 手册之 Provisioning - Shell 配置程序

    原文地址 Provisioner 命令:"shell" 示例: node.vm.provision "shell" do |s| s.inline = < ...

  5. Vagrant 手册之 Vagrantfile - SSH 设置 config.ssh

    原文地址 配置的命名空间:config.ssh config.ssh 中的设置与配置 Vagrant 如何通过 SSH 访问您的计算机相关. 大多数 Vagrant 设置一样,一般使用默认设置即可,但 ...

  6. Vagrant 手册之 Vagrantfile - 机器设置 config.vm

    原文地址 配置的命名空间:config.vm config.vm 中的设置修改 Vagrant 管理的机器的配置. 1. 可用的设置项 config.vm.boot_timeout Vagrant 等 ...

  7. Vagrant 手册之多个虚拟机 multi-machine

    原文地址 Vagrant 可以通过一个 Vagrantfile 定义并控制多个客户机.这就是所谓的"multi-machine"多虚拟机环境. 这些机器通常可以协同工作,或者互相关 ...

  8. Vagrant 手册之 box - box 的信息格式

    原文地址 创建 Vagrant 的 box 时,可以提供在运行 vagrant box list -i 时展示的与用户相关的其他信息.例如,可以打包 box,以包含有关该 box 的作者和网站信息: ...

  9. Vagrant 手册之网络 - 端口转发

    原文地址 Vagrantfile 配置文件中端口转发的网络标识符:forwarded_port,例如: config.vm.network "forwarded_port", gu ...

随机推荐

  1. django shell的基本使用

    作者:python技术人 博客:https://www.cnblogs.com/lpdeboke/ 在日常工作再发中,经常需要测试一些对象.函数.类...等是否正确,但是如果整体运行项目特别麻烦,并且 ...

  2. Java 类在 Tomcat 中是如何加载的?

    作者 :xingoo https://www.cnblogs.com/xing901022/p/4574961.html 说到本篇的Tomcat类加载机制,不得不说翻译学习Tomcat的初衷. 之前实 ...

  3. 10 个常用的 Linux 命令?

    pwd 显示工作路径ls 查看目录中的文件 cd /home 进入 '/ home' 目录'cd .. 返回上一级目录cd ../.. 返回上两级目录mkdir dir1 创建一个叫做 'dir1' ...

  4. Oracle 常用统计视图汇总

         Oracle统计信息对数据库性能优化和故障排除都相当重要,目前接触到的与统计信息相关的视图大体有 4 个:   1.v$sysstat 视图      该视图用于记录系统级的统计信息,共 5 ...

  5. jquery点击按钮弹出图片

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. Python subprocess ffmpeg

    # -*- coding:utf-8 -*- import os, sys, getopt import numpy as np import subprocess as sp import cv2 ...

  7. 基于socket实现大文件上传

    import socket 1.客户端: 操作流程: 先拿到文件--->获取文件大小---->创建字典 1.制作表头 header  如何得到 他是一个二进制字符串 序列化得到 字典字符串 ...

  8. Elasticsearch Java Low Level REST Client(嗅探器)

    https://segmentfault.com/a/1190000016828977?utm_source=tag-newest#articleHeader0 嗅探器 允许从正在运行的Elastic ...

  9. devops持续集成,Centos7.6下gitlab+jenkins(pipeline)实现代码自动上线

    持续集成 gitlab+jenkins(pipeline)实现代码自动上线 环境准备:Centos7.6版本ip:192.168.0.13 主机名:gitip:192.168.0.23 主机名:jen ...

  10. Codeforces Round #595 (Div. 3) 题解

    前言 大家都在洛谷上去找原题吧,洛谷还是不错的qwq A 因为没有重复的数,我们只要将数据排序,比较两两之间有没有\(a_j - a_i == 1 (j > i)\) 的,有则输出 \(2\) ...