How to sync directory within Vagrant box (virtualbox provider) with local directory? - vagrant

I have used the vagrantfile provided in this here tutorial and can't seem to get my local files synced to the VM. I tried mkdir vagrant_data and that didn't show them in there. What do I do?
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.synced_folder "./", "/vagrant_data"
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "2048"
end

I believe you may be giving the wrong file path of the host machine. Instead of giving the ./ relative path which could refer to anything. Try using the absolute path instead. Here if the Official tutorial on how to configure.

Related

Vagrant is not syncing files

So I've been looking around a little bit for this issue, but maybe I lack something.
I freshly generated a Vagrant VM (With VirtualBox) but the files only seem to sync 1 time when the server loads/reloads.
I only have this in my configuration file, am I missing something here?
Vagrant.configure("2") do |config|
config.vm.box = "debian/stretch64"
config.vm.network "forwarded_port", guest: 25565, host: 25565
config.vm.provider "virtualbox" do |vb|
vb.memory = "8192"
end
end
I also tried with
config.vm.synced_folder ".", "/vagrant_files"
But that doesn't seem to change anything.
I don't have any errors on load, but the result is that when I add a file on the server, I don't see it in the project folder on my pc

Vagrant synced folder causes contents to be erased

I'm trying to add add an additional synced folder besides the default /vagrant.
I've read the documentation which indicates that this should be as simple as:
config.vm.synced_folder "/path/to/somewhere/on/host", "/path/to/guest_directory", create: true
When I add this to my Vagrantfile and then reprovision it, the guest_directory folder shows up in the expected place on the host, but it's empty. And the folder in the guest OS is also empty... which breaks my site because those are the website files. Interestingly, if I remove that from the Vagrantfile and reprovision again, the files reappear in the guest OS where they were originally.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.99"
# NEITHER OF THESE WORK...
# config.vm.synced_folder "/Users/ESL/Sites/project/web__public_web", "/home/vagrant/web__public_web", create: true
# config.vm.synced_folder "./web__public_web", "/home/vagrant/web__public_web", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.provider "virtualbox" do |vb|
vb.memory = "2046"
end
end
You can see that I've tried a two different type: options and got the same wrong result with both. I also tried placing a symlink to the project files inside /vagrant/ and that didn't work either. (The broken symlink showed up in the host os directory instead of the files I want to access)
Here are a couple more details about my setup, just in case they're relevant:
In the guest OS the project files live in /home/vagrant/web__public_web but the document root is /srv/www/... which contains a symlink pointing to the former path.
VirtualBox: Version 5.2.18 r124319 (Qt5.6.3)
Vagrant: v2.1.0
OSX: 10.13.6
What am I doing wrong?
Each mount requires a unique id, if the box is already built you'll need to rebuild it.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.99"
config.vm.synced_folder "/Users/ESL/Sites/project/web__public_web", "/path/where/mounted/on/vm", id: "unique_id_1", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.synced_folder "/home/vagrant/web__public_web", "/path/where/mounted/on/vm2", id: "unique_id_2", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.provider "virtualbox" do |vb|
vb.memory = "2046"
end
end
I answered my own question...
It turns out the sync was working all along. But I misunderstood the directionality of the sync. I was expecting the files in the guest OS to show up in the host directory, but it's the other way around. The empty folder on the host directory was getting synced to the guest and wiping out all the files.
After I manually copied all of the files from the guest to the host directory, the same configurations worked as expected.
Bottom line: if you want to sync an existing guest folder with files in it, you have to manually move it into place on the host OS first.

Vagrant Vagrantfile config

I have this vagranfile for box "centos/7"
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network :private_network, ip: "192.168.33.10"
config.vm.synced_folder ".", "/home/vagrant"
config.vm.provision "shell", path: "./conf/bitrix-env.sh"
config.vm.boot_timeout = 100
config.vm.provider :virtualbox do |vb|
vb.gui = true
vb.memory = "1024"
end
end
vagrant up - command works clear, but scripts in provision don't install
after i tried to start vagrant provision, but had an error:
SSH is getting permission denied errors when attempting to connect
to the IP for SSH. This is usually caused by network rules and not being
able to connect to the specified IP. Please try changing the IP on
which the guest machine binds to for SSH.
How can i fix this and install all provisions?
The problem is due to the below line in your vagrantfile
config.vm.synced_folder '.', '/home/vagrant'
The authorized_keys file for the vagrant user is located in /home/vagrant/.ssh inside the vagrant machine, which enables to ssh into the vagrant box.
As you are mounting your current directory to /home/vagrant, all the contents of /home/vagrant are over-written and there is no authorized key file.
Change the mount path to anything except /home/vagrant, and you will able to ssh into the machine. As example
config.vm.synced_folder '.', '/home/vagrant/somepath'

Setting vagrant home directory during provisioning

I have a VM that I'm pulling down with vagrant and using a VERY basic vagrant file
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "vagrant-rhel-devel"
config.vm.network :private_network, ip: "192.168.33.101"
config.vm.synced_folder ".", "/vagrant"
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
# vb.gui = true
# Enable 3d Rendering
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
# Sets 32megs video ram, higher number here = more POWERS
vb.customize ["modifyvm", :id, "--vram", "32"]
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
vb.name = "RedHat 3D"
end
end
My issue is that the vagrant user gets a home directory of /localhome/vagrant and I'd like him to have /home/vagrant as the home directory.
Is this something I'm able to change with provisioning or is it something that is set in the VM itself? I'm rather unskilled at the provisioning step at the moment so an example would be great.
The HOME directory has been set when the user has been created so this was done before the VM was packaged as vagrant box.
From there, you should be able to change that with
usermod -m -d /path/to/new/home/dir userNameHere
so in your case it would be
usermod -m -d /home/vagrant vagrant
check if you have existing files under the /localhome directory that would need to be copied into the new one (bash preferences file)
if you do not plan to destroy/create a bunch of new VM from this box, its not needed to add as provisioning; if you do plan to use this box and create lot of new VM, then it could make sense, you would just need to add an inline shell provision

Using SaltStack grains file with Vagrant

I would like to use minion.d/*.conf to provision a vagrant machine.
Here is my Vagrant configuration:
Vagrant.configure("2") do |config|
## Choose your base box
config.vm.box = "precise64"
## For masterless, mount your salt file root
config.vm.synced_folder "salt/roots/", "/srv/salt/"
## Use all the defaults:
config.vm.provision :salt do |salt|
salt.minion_config = "salt/minion"
salt.run_highstate = true
salt.grains_config = "salt/minion.d/vagrant.conf"
end
end
After provisioning the Vagrant machine, I have errors with rendering SLS files since the minion.d/*.conf files are not being copied to the guest machine under :
/etc/salt/minion.d/
Should I make a sync config in the Vagrant configuration to co ?
Have you just tried mounting a synced folder to /etc/salt/grains?
## For masterless, mount your salt file root
config.vm.synced_folder "salt/roots/", "/srv/salt/"
config.vm.synced_folder "salt/grains.d/", "/etc/salt/grains.d/"
#Utah_Dave's solution will work just fine, or you can do the following (which is how I run it).
Filesystem:
/dev
Vagrantfile
salt-minion.conf
salt/
top.sls
my-awesome-state/init.sls
pillar/
top.sls
my-awesome-pillar.sls
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "mafro/jessie64-au-salt"
# salt config directory & shared dir in /tmp
config.vm.synced_folder ".", "/srv/salt"
# setup the salt-minion
config.vm.provision :salt do |salt|
salt.minion_config = "salt-minion.conf"
end
end
salt-minion.conf
file_client: local
id: awesome
file_roots:
base:
- /srv/salt/salt
pillar_roots:
base:
- /srv/salt/pillar
Vagrant's implementation of salt.grains_config doesn't copy the file to the /etc/salt/minion.d folder as you might expect. Instead it copies the file to /etc/salt/grains.
To get the salt-minion to read this new grains file, you just need to add the following to your minion configuration:
/etc/salt/minion
include:
- /etc/salt/grains

Resources