Options for config.vm.synced_folder

190 views
Skip to first unread message

James Newton

unread,
Aug 29, 2017, 12:33:53 PM8/29/17
to Vagrant
Hello Vagrant experts!

Is it possible to use config.vm.synced_folder to update the folder on the host machine rather than overwriting it on the guest?

I have a package where nginx is already installed, and I am hoping to have a copy of the /etc/nginx/ folder on the host machine, so that I can edit the files in it without resorting to SSH.  I discover that if I use...

    config.vm.synced_folder 'nginx/', '/etc/nginx'

... then the /etc/nginx/ folder on the virtual machine is replaced by the (intially) empty nginx folder on the host. What would be the options to use to achieve this?

Or is there a completely different technique for copying these files from the VM to the host, so that on the next vagrant up, the synchronization can work as expected?

Thanks in advance,

James

Alvaro Miranda Aguilera

unread,
Aug 29, 2017, 3:38:08 PM8/29/17
to vagra...@googlegroups.com
Hello

Shared folder goes from host to guest

what you can do is copy the folder out and then mount it on the guest

however will be a bit late (after servides start) and you will be need to reload the services after.

So you can just copy the files to the location and restart/reload the service in one single script. to keep things simple.

Alvaro

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/3fbb36a9-73a1-49a3-998e-bcb73aac6fc3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

James Newton

unread,
Aug 29, 2017, 5:11:14 PM8/29/17
to vagra...@googlegroups.com
On 29 August 2017 at 22:38, Alvaro Miranda Aguilera <kik...@gmail.com> wrote:
Shared folder goes from host to guest

what you can do is copy the folder out and then mount it on the guest

however will be a bit late (after servides start) and you will be need to reload the services after.

So you can just copy the files to the location and restart/reload the service in one single script. to keep things simple.

Hello Alvaro,

Thank you for your feedback.

Would it be impertinent to ask what script I could use to copy the files from the guest to the host? I am guessing that this would need to be done only once, since from that moment on, any changes in the host will be carried back to the guest.

Thanks in advance for your insights,

James 

Alvaro Miranda Aguilera

unread,
Aug 29, 2017, 5:28:31 PM8/29/17
to vagra...@googlegroups.com
Hello

So Vagrant provison by default runs only once, when the VM is created

so your script can look like:

$script = <<SCRIPT
cp -ap /vagrant/nginx/* /etc/nginx/
chkconfig nginx on
service nginx reload
SCRIPT Vagrant.configure("2") do |config| config.vm.provision "shell", inline: $script end

and you put your files in nginx/ directory




--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

James Newton

unread,
Aug 29, 2017, 5:32:03 PM8/29/17
to vagra...@googlegroups.com
Thank you so much for your help!

You received this message because you are subscribed to a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/XVFPIOXU8aI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vagrant-up+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/CAHqq0exp6Rdwp7WKaU6%3DBeLk9aaXNsJYJB_9kFYZh%2By5x5x0CQ%40mail.gmail.com.

Alvaro Miranda Aguilera

unread,
Aug 29, 2017, 6:06:35 PM8/29/17
to vagra...@googlegroups.com
Please share back what you got working, so other people can learn from it.

Sharing is caring :D

Alvaro

James Newton

unread,
Sep 2, 2017, 11:04:32 AM9/2/17
to Vagrant
On Wednesday, 30 August 2017 01:06:35 UTC+3, Alvaro Miranda Aguilera wrote:
Please share back what you got working, so other people can learn from it.

Sharing is caring :D

 Hi Alviro,

The code you posted provoked errors, but it did give me a starting point from which to work out a solution. 

# cp -ap /vagrant/nginx/* /etc/nginx/ ## should be:
cp -ap /vagrant/nginx/. /etc/nginx/
# chkconfig nginx on
## chkconfig is not shipped with Ubuntu since 12.04,
## if my research is accurate. Should be:
service nginx start

To help others who may not be using the custom package that I have, I've prepared a Vagrantfile that can be used after a simple `vagrant init` in an empty folder:

# -*- mode: ruby -*-
# vi: set ft=ruby :

$script
= <<SCRIPT
# Install nginx.
apt
-get -y update
apt
-get -y install nginx
service nginx start

# Copy the nginx and www folders to the /vagrant folder, so that they
# can be synced with the host. The www and nginx folders will be
# created automatically. They don't need to exist on the host yet.
cp
-ap /etc/nginx/. /vagrant/nginx/
cp
-ap /var/www/. /vagrant/www/

# Syncing must be done as a second step. Uncomment the lines beginning
# #*# below after the first `vagrant up`, then use `vagrant reload`

SCRIPT


Vagrant.configure("2") do |config|

  config
.vm.box = "ubuntu/xenial64"

 
#Run a provisioning script on first launch
  config
.vm.provision "shell", inline: $script

 
## Sync the www and nginx folders, so that they can be edited on
 
## the host machine.
 
## Uncomment the next line only after the box has been provisioned,
 
## otherwise the host's nginx/folder will overwrite the
 
## guest's installation.
 
#*#config.vm.synced_folder 'www', '/var/www'
 
#*#config.vm.synced_folder 'nginx', '/etc/nginx'

 
## On the first `vagrant up`, Vagrant will automatically create a
 
## /vagrant file at the root of the guest, and sync it with the
 
## folder that contains this Vagrantfile. This is necessary so that
 
## the command `cp -ap /etc/nginx/. /vagrant/nginx/` can copy the
 
## contents of the /etc/nginx/ folder to the host.
 
##
 
## On subsequent launches, only the ./www/ and ./nginx/ folders need
 
## to be synced, so you can uncomment the line below.
 
#*#config.vm.synced_folder '.', '/vagrant', disabled: true
end

This installed nginx and copied the contents of the /etc/nginx/ and /var/www/ directories to the host. I then manually uncommented three lines (as shown in bold below) and called vagrant reload.

# -*- mode: ruby -*-
# vi: set ft=ruby :

$script
= <<SCRIPT
# Install nginx.
apt
-get -y update
apt
-get -y install nginx
service nginx start

# Copy the nginx and www folders to the /vagrant folder, so that they
# can be synced with the host. The www and nginx folders will be
# created automatically. They don't need to exist on the host yet.
cp
-ap /etc/nginx/. /vagrant/nginx/
cp
-ap /var/www/. /vagrant/www/

# Syncing must be done as a second step. Uncomment the lines beginning
# #*# below after the first `vagrant up`, then use `vagrant reload`

SCRIPT


Vagrant.configure("2") do |config|

  config
.vm.box = "ubuntu/xenial64"

 
#Run a provisioning script on first launch
  config
.vm.provision "shell", inline: $script

 
## Sync the www and nginx folders, so that they can be edited on
 
## the host machine.
 
## Uncomment the next line only after the box has been provisioned,
 
## otherwise the host's nginx/folder will overwrite the
 
## guest's installation.
 
config.vm.synced_folder 'www', '/var/www'
  config
.vm.synced_folder 'nginx', '/etc/nginx'


 
## On the first `vagrant up`, Vagrant will automatically create a
 
## /vagrant file at the root of the guest, and sync it with the
 
## folder that contains this Vagrantfile. This is necessary so that
 
## the command `cp -ap /etc/nginx/. /vagrant/nginx/` can copy the
 
## contents of the /etc/nginx/ folder to the host.
 
##
 
## On subsequent launches, only the ./www/ and ./nginx/ folders need
 
## to be synced, so you can uncomment the line below.
  config.vm.synced_folder '.', '/vagrant', disabled: true
end

It seems cumbersome to have to make these manual changes and to run vagrant reload. Is it possible to modify this script so that the config.vm.synced_folder calls are made only if /etc/nginx/ already exists, and vagrant reload is called automatically if not?

Having a single, unchanging file that gets read twice would make sharing the Vagrant package much more straightforward.

Thanks in advance for your ideas,

James


Alvaro Miranda Aguilera

unread,
Sep 4, 2017, 12:55:36 AM9/4/17
to vagra...@googlegroups.com
Once all is in place you can test with

vagrant destroy
vagrant up


Copying files from guest to host into the /vagrant folder is good to put all into the project, you can have all in Source control.

Once is done, the user experience for the new box will be

git clone <url>
cd <dir>
vagrant up

Alvaro

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Reply all
Reply to author
Forward
0 new messages