managing existing hosts with vagrant bypassing the provider - vagrant

I'm managing a few web services residing on different fixed hosts with ssh. I wanted to use vagrant so that I can edit local files and have them synced automagically.
however I'm having problems as I'm not using no provider or box, it's a fixed host and it feels like I'm going against vagramt's aim.
here's my Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.ssh.host = ...
config.ssh.username = ...
config.ssh.private_key_path = ".ssh/id_rsa"
config.vm.synced_folder "src/", "..."
config.vm.box = "myhost"
config.vm.provision :shell, :path => "bootstrap.sh"
end
and here's my bootstrap.sh file:
pip install flask sqlalchemy
but I can't make vagrant skip providing (with virtualbox or so)

well, as it always comes out - fighting against your tool in order to force it to do things it's not designed to are a bad idea.
there was probably a way to make vagrant use a void box but vagrant is too much for just keeping 2 directories synced. I found this nice tool that does exactly the same as vagrant for sync just without all the provider/provision etc.

Related

Can I load a Vagrantfile that loads another Vagrantfile?

I'm trying to set up an inheritance structure in my forest of Vagrantfiles. Basically I wanted something like this:
/base
Vagrantfile
/testvm
Vagrantfile
/nginx-test
Vagrantfile
The testvm/Vagrantfile would load the base/Vagrantfile and the nginx-test/Vagrantfile would load the testvm/Vagrantfile. In my test case, the base Vagrantfile specifies one box type (eg bento/ubuntu-18.04) and sets 1GB of memory for the VM and some other stuff. The testvm Vagrantfile loads that and overrides the box to bento/ubuntu-22.04 and sets a different hostname. The nginx-test Vagrantfile loads that last file and sets its own hostname plus adds another provisioning block to install nginx.
I've tried this but I get this SystemStackError: stack level too deep error:
$ vagrant up
Vagrant failed to initialize at a very early stage:
There was an error loading a Vagrantfile. The file being loaded
and the error message are shown below. This is usually caused by
a syntax error.
Path: /Users/donseiler/vagrant_test/testvm/nginx-test/Vagrantfile
Line number: 5
Message: SystemStackError: stack level too deep
I've been searching but haven't found anyone attempting multiple layers of loading yet. I can run vagrant up just fine from the base and testvm directories to bring those machines up. I can't run any vagrant commands from the nginx-test directory (not even vagrant status, etc)
Here are the file contents:
/base/Vagrantfile:
VAGRANTFILE_API_VERSION = "2"
NAME = "basevm"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Default to bento bionic box
config.vm.box = "bento/ubuntu-18.04"
config.vm.hostname = NAME
# Set default virtualbox provider settings
config.vm.provider :virtualbox do |vb|
vb.name = NAME
vb.memory = "1024"
vb.gui = false
end
config.vm.provision "shell", inline: "DEBIAN_FRONTEND=noninteractive apt-get update"
end
/base/testvm/Vagrantfile:
# Load parent Vagrantfile
load "../Vagrantfile"
# define hostname
NAME = "testvm"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Change to jammy
config.vm.box = "bento/ubuntu-22.04"
config.vm.provision "shell", inline: <<-SHELL
export DEBIAN_FRONTEND=noninteractive
apt-get -y install curl
SHELL
end
/base/testvm/nginx-test/Vagrantfile:
# Load parent Vagrantfile
load "../Vagrantfile"
# define hostname
NAME = "nginx-test"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provision "shell", inline: <<-SHELL
export DEBIAN_FRONTEND=noninteractive
apt-get -y install nginx
SHELL
end
UPDATE: It looks like I can do multiple loading just fine as long as I use absolute file paths to the Vagrantfiles (or only use relative paths in the pwd's Vagrantfile, or if all Vagrantfiles are in the same directory). The relative path apparently is relative to the pwd, so when ../Vagrantfile also tries to load ../Vagrantfile, it does so from pwd and not from the parent dir. Then it becomes somewhat of an infinite loop as the parent dir's Vagrantfile keeps trying to load itself from the child dir until vagrant short-circuits itself.
Basically, it loads Vagrantfiles from wherever but the commands in them are executed from the pwd, so relative paths are always relative to the pwd, NOT relative to where the Vagrantfile lives.
Not being able to use relative paths makes things a little less flexible but at least it's a working alternative.

How do I set default SMB credentials at the user scope?

At work, we use Vagrant on Windows 10 with the Hyper-V provider and, necessarily, SMB synced folders. It gets kind of annoying typing our entire corporate domain username and password every time we up a box... is there any way to set default SMB credentials?
It needs to work with Vagrantfiles checked into source control and cloned onto any developer machine.
Vagrant loads multiple Vagrantfiles from different locations. One of those being a user Vagrantfile.
~/.vagrant.d/Vagrantfile
https://www.vagrantup.com/docs/vagrantfile/#load-order-and-merging
But, the only username and _password like options, as of Vagrant 2.2.7, are provided directly on the synced_folder method.
https://www.vagrantup.com/docs/synced-folders/smb.html#options
Something you can do in the meantime is pull defualt username and domain info from environment vars...
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
default_smb_username = ENV['USERNAME']
default_smb_username = "#{ENV['USERNAME']}##{ENV['USERDNSDOMAIN'].downcase}" if ENV['USERDNSDOMAIN']
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant"
config.vm.synced_folder ".", "/vagrant", smb_username: default_smb_username if default_smb_username
end
An issue has been opened asking for a solution.
https://github.com/hashicorp/vagrant/issues/11413

Vagrant 'permission denied' on Windows

I am having trouble accessing files through Vagrant on Windows. I have been using it on OS X for quite some time and have my Vagrantfile setup correctly which works every time.
I have sent my colleague the same Vagrant file, he is on Windows and receives 'Permission Denied' when trying to access files through the browser.
Just to be clear, the permission errors are returned by the server when accessing 'dev.local' in the browser and not from Vagrant itself... it will be a configuration error on Windows or within the VM.
The VM is CentOS 6.5
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "chef/centos-6.5"
config.vm.network "private_network", ip: "192.168.33.21"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.provision :shell, :path => "install.sh"
config.vm.hostname = "dev.local"
config.vm.synced_folder ".", "/home", id: "vagrant", :nfs => false, :mount_options => ["dmode=777","fmode=777"]
config.ssh.insert_key = false
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
end
Can any Windows Vagrant users shed any light on this?
It was VBGuestAdditions out of date. The permission error was caused by not being able to sync to my local folder (which contained an index.php) so it was using the servers /home folder which didn't contain anything and since viewing directory structure is disabled it returned permission errors.
I had 4.X.X installed, and VirtualBox is on 5.X.X Here is the fix:
run command: vagrant plugin install vagrant-vbguest
the run vagrant up which may still throw an error as the plugin fails to copy a file.
vagrant ssh to get into the box and run the following command:
sudo ln -s /opt/VBoxGuestAdditions-5.X.X/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions
Replace 5.X.X with the version of your VirtualBox.
logout and run vagrant reload
do a happy dance

Updated Vagrantfile but still getting warning that my Vagrantfile is outdated

I have updated my Vagrantfile to this:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define :ceph do |ceph|
ceph.vm.box = "big-ceph"
ceph.vm.network :private_network, ip: "192.168.251.100"
ceph.vm.hostname = "ceph"
end
config.vm.define :client do |client|
client.vm.box = "hashicorp/precise64"
client.vm.hostname = "ceph-client"
client.vm.provision :shell, path: "setup/ceph.sh"
client.vm.network :private_network, ip: "192.168.251.101"
end
end
but I am still getting this warning message whenever I vagrant up my virtual machines.
calvin % vagrant reload ceph && vagrant reload client
There were warnings and/or errors while loading your Vagrantfile
for the machine 'ceph'.
Your Vagrantfile was written for an earlier version of Vagrant,
and while Vagrant does the best it can to remain backwards
compatible, there are some cases where things have changed
significantly enough to warrant a message. These messages are
shown below.
Warnings:
* `config.vm.customize` calls are VirtualBox-specific. If you're
using any other provider, you'll have to use config.vm.provider in a
v2 configuration block.
Any idea why?
Ok, I figured it out. This 3rd party ceph box that I am using comes with its own Vagrantfile which overrides my Vagrantfile and the included box Vagrantfile (which is located in ~/.vagrant.d/boxes/big-ceph) still contains
config.vm.customize ["modifyvm", :id, "--nictype1", "virtio"]
Comment that out and I no longer see the annoying warning.

vagrant Vagrantfile mount point ignored

I have a Vagrantfile as below running under libvirt. When the box boots, the project directory is mounted under "/vagrant" not "/path/to/source". It works fine on another machine under VirtualBox.
Any ideas? Symlinking /vagrant to the actual place I want the mount is hacky.
vagrant up --debug shows that it isn't even attempting it. The port forwarding works OK though.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "mybox"
file = File.open("#{Dir.home}/.mybox_key", "rb")
key = file.read
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.synced_folder "./", "/path/to/source/", type: "nfs"
end
If you're running an older version of Vagrant that doesn't support this option (e.g. 1.3.5), it may fail silently instead of trying to setup the NFS share. Ensure you're running at least Vagrant 1.5.1.

Resources