In a Vagrant setup, I have to upload a couple of files from the host to the guest during the provisioning phase.
In https://docs.vagrantup.com/v2/provisioning/file.html I can see how to upload a single file, from a source to a destination, but how can I upload multiple files or a folder structure?
NOTE: I know I can do that using the shell provisioner, but in this particular case a set of file uploads would be more appropriate.
Command: vagrant provision [vm-name]Runs any configured provisioners against the running Vagrant managed machine. This command is a great way to quickly test any provisioners, and is especially useful for incremental development of shell scripts, Chef cookbooks, or Puppet modules.
Using SCP. Another way to copy files and folders from host to guest in Vagrant is to use to SCP. Firstly, make sure you have scp installed on your host. If you don't have it, you can install the openssh-clients package if you're using Linux or install Cygwin if you're using Windows.
Vagrant reload will reboot the VM, if the VM was provisioned already in the next run reload will skip those by default. vagrant reload --provision will reboot the VM and run the provision steps if any. in the home of the users are a lot of files needed to ssh.
You can definitely run multiple Vagrant boxes concurrently, as long as their configuration does not clash with one another in some breaking way, e.g. mapping the same network ports on the host, or using same box names/IDs inside the same provider.
You would need a separate config.vim.provision
section for each file. You can add multiple of those sections to your Vagrantfile
, like this:
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
Output:
[default] Running provisioner: file...
[default] Running provisioner: file...
You see it is executing both provisioning actions. You can verify the file presence inside the virtual machine:
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
Folder content uploads seem to work as well (I've only tried it with files, not nested folders):
config.vm.provision :file, source: '../folder', destination: "/tmp/folder"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With