Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I upload more than one file with vagrant file provisioning?

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.

like image 767
Giacomo Tesio Avatar asked Aug 26 '14 22:08

Giacomo Tesio


People also ask

How do I use vagrant provision?

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.

How do I transfer files to vagrant?

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.

What does vagrant reload -- provision do?

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.

Can I have more than one vagrant box?

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.


2 Answers

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
like image 125
hek2mgl Avatar answered Nov 16 '22 01:11

hek2mgl


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"
like image 36
Al Belsky Avatar answered Nov 16 '22 03:11

Al Belsky