Using SaltStack grains file with Vagrant - 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

Related

Include a directory of config files for a Vagrantfile

I have a Vagrantfile that has the following semi-standard setup:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true
config.vm.network "private_network", ip: "192.168.56.40"
config.vm.hostname = "development.local"
config.vm.boot_timeout = 600
config.vm.provider "virtualbox" do |v|
v.name = "development.local"
v.memory = "1024"
v.check_guest_additions = true
end
config.vm.provision :shell, path: "bootstrap.sh"
end
My aim is to auto include files within a folder that configure the site on vagrant up.
For apache settings, I have managed to create a nice bash script that parses a folder and sets up sites-enabled for all the configs within a folder.
I would like to also share these folders inside the Vagrantfile without having to modify the core, much like apache parses the sites-enabled folder. This would mean I can easily share this box with others, without having to change the Vagrantfile every time I spin up a test environment and want to share a new folder.
The usual syntax is:
config.vm.synced_folder "../some-folder/", "/var/www/some-folder", owner: "www-data"
Using pseudocode, this is what I'm aiming for:
foreach([$files_in_$folder] as $synced_folder) {
require $synced_folder_config_file;
// ^ this would be a small config file
}
Note that the advantage of the small config file is that other things could be specified on a per-file bases (such as different owners/permissions etc).
Key things to do:
parse over folder contents
ensure that this config is executed as part of the vagrant up
I've barely touched ruby, so apologies if this is obvious.
Thanks
After some research and a lot of reprovisions and up/down/destroy:
Create appropriate files in the /synced-folders/, each consisting of something like:
config.vm.synced_folder "../development.local/", "/var/www/development.local", owner: "www-data"
Then, in your Vagrantfile:
Dir[File.dirname(__FILE__) + '/synced-folders/*.rb'].each {|file| eval File.read(file) }
This parses over the directory, and evals them.

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'

Vagrant synced_folder /vagrant shared_folder missing

Hi I'm trying to provision my box using vagrant and ansible but I always encounter /vagrant/ansible/playbook.yml is missing. It's looking for a /vagrant directory. When I ssh inside my box the /vagrant does not exist. I'm not sure what happen but what I did was to sync all my local copy into a specific folder location inside my box. Below is a sample implementation I did
Vagrant.configure("2") do |config|
config.vm.define "ans", primary: true do |ans|
ans.vm.box = "ubuntu/xenial64"
ans.vm.network "forwarded_port", guest: 80, host: 8080
ans.vm.network "private_network", ip: "10.8.9.20", auto_network: true
ans.vm.synced_folder ".", "/var/www/"
ans.vm.hostname = "my_project"
ans.vm.provision "pre-build", type: "shell", :path => "ansible/bootstrap.sh"
ans.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/playbook.yml"
end
end
end
I'm getting an error in the ansible.playbook = "ansible/playbook.yml" it seems this line is looking under /vagrant folder which does not exist. What will be the workaround or fix for this? My playbook exist in this location /var/www/ansible/playbook.yml inside the box
By default, vagrant automatically add a shared folder from your current directory with /vagrant folder on the VM.
Because you have added
ans.vm.synced_folder ".", "/var/www/"
the current folder shared folder has been replaced from /vagrant to /var/www (you can easily verify that by removing the line from your vagrantfile)
do you really need to sync the whole current folder with /var/www ideally you would have your project files in a subdirectory that you will share in /var/www like
ans.vm.synced_folder "html/", "/var/www/"
and push all your html/csss/js files under the html folder (feel free to rename project or something else) and you'll not need to sync all the vagrant scripts and provisioning script with your web server folder.

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

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.

Resources