在Vagrant设置中,我需要在配置阶段从主机上传一些文件到宾客机。在https://docs.vagrantup.com/v2/provisioning/file.html中,我可以看到如何上传单个文件,从源到目标,但是如何上传多个文件或文件夹结构?注意:我知道可以使用shell provisioner完成此操作,但在这种特殊情况下,一组文件上传更合适。
config.vim.provision
部分。你可以像这样在Vagrantfile
中添加多个这样的部分:
config.vm.provision :file do |file|
file.source = "/etc/file1.txt"
file.destination = "/tmp/1.txt"
end
config.vm.provision :file do |file|
file.source = "/etc/file2.txt"
file.destination = "/tmp/2.txt"
end
输出:
[default] Running provisioner: file...
[default] Running provisioner: file...
你会看到它正在执行两个配置操作。您可以在虚拟机内验证文件是否存在:
vagrant ssh -- ls -al /tmp/{1,2}.txt
-rw-r--r-- 1 vagrant vagrant 4 Aug 27 08:22 /tmp/1.txt
-rw-rw-r-- 1 vagrant vagrant 4 Aug 27 08:22 /tmp/2.txt
文件夹内容上传似乎也可以正常工作(我只试过文件,未试过嵌套文件夹):
config.vm.provision :file, source: '../folder', destination: "/tmp/folder"
/tmp/
每次重新启动Vagrant box时都会被清除,因此您看不到它。但是,如果您将此命令与以下目标一起使用,该目标在重新启动之间是持久的:/home/vagrant/folder/
。第一次启动Vagrant box时,您会得到/home/vagrant/folder/
。下一次启动Vagrant box时,您将获得/home/vagrant/folder/folder/
。接下来呢?/home/vagrant/folder/folder/folder/
。使用config.vm.synced_folder
和type: "rsync"
更好。使用Vagrant 1.8.1。 - Giacomo1968如果有人只需要快速简单地复制主机上的目录结构到客户机上,我最终是通过以下方式实现的:
require 'pathname' # at the beginning of the Vagrantfile
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# more configuration here
r = Pathname.new 'root'
Dir.glob(File.join("root", File.join("**","*"))) do |f|
if !File.directory?(f) then
s = Pathname.new f
t = s.relative_path_from r
config.vm.provision "file" do |fp|
fp.source = s.to_s
fp.destination = "/tmp/root/" + t.to_s
end
end
end
# more configuration here
end
。config.vm.synced_folder "upload/", "/home/vagrant/upload/", type: "rsync"
。 - Giacomo1968config.vm.synced_folder "upload/", "/home/vagrant", type: "rsync"
config.vm.synced_folder "upload/", "/home/vagrant/upload/", type: "rsync"
。 - Giacomo1968vagrant up
vagrant ssh
vagrant@yourvagrant: sudo chown -R testuser:testuser /var/www/ or whatever folder you want to upload your files
# we create add script to upload folder to a vagrant guest from local file
config.vm.provision :file, source: '/wamp/www/ian_loans', destination: "/var/www/solidbond"
只需将文件目录更改为您的需求。退出并不要忘记在您的vagrant目录中运行。
vagrant provision
vagrant@yourvagrant: cd /var/your file directory
这可能有点老了,但我会在这里为使用PowerShell的Windows用户提供整个目录结构的另一个答案。
首先,创建一个PowerShell脚本,从共享文件夹复制整个文件夹结构。
#Check if the directory you're copying already exists on the VM
$foldercheck = "path\to\folder\on\vm"
if (test-path $foldercheck)
{
Write-Host "Folder 'path\to\folder\on\vm' already exists..."
}
else {
#Create the new directory
New-Item -ItemType Directory -Force -Path "path\to\folder\on\vm"
#Copy over contents to the directory
Copy-Item "\\VBOXSRV\<share_name>\path\to\folder\to\copy\*" -Destination "path\to\folder\on\vm" -recurse
}
然后,配置您的Vagrantfile以自动挂载共享文件夹,复制PowerShell脚本,然后按指定顺序运行它们。
Vagrant.configure(2) do |config|
config.vm.provider "virtualbox" do |vb|
# Other settings
# ...
# Add shared folders
vb.customize ["sharedfolder", "add", :id, "--name", "<share_name>", "--hostpath", "path/to/host/folder/location", "--automount"]
end
# =================================================================
# PROVISIONING
# =================================================================
# Load up the files to the Guest environment for use by PowerShell
config.vm.provision "file", source: "path/to/provisioning/files/powershellfile.ps1", destination: "path/to/temporary/location/powershellfile.ps1"
# Rinse/repeat for additional provisioning files
# Now provision files in the order they are specified
config.vm.provision "shell", inline: "path/to/temporary/location/powershellfile.ps1"
# Rinse/repeat to run PowerShell scripts
end
config.vm.synced_folder
。 - hek2mglconfig.vm.provision
条目。 - hek2mgl