I have vagrant file which gives me ability to run Win 7 and Win 8 OS inside VirtualBox
Vagrant.configure("2") do |config|
# Win7 VM
config.vm.define "win7" do |win7|
win7.vm.box_url = "file:///C:/V_W_S/windows_7_virtualbox.box"
win7.vm.box = "windows_7_virtualbox"
win7.vm.communicator = "winrm"
win7.vm.boot_timeout = 600
win7.vm.graceful_halt_timeout = 600
win7.vm.network "public_network", auto_config: true
win7.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--usb", "on"]
vb.customize ["modifyvm", :id, "--usbehci", "on"]
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
vb.customize ["modifyvm", :id, "--memory", "4096"]
vb.customize ["modifyvm", :id, "--vram", "256"]
vb.cpus = 2
vb.gui = true
vb.name = "Win_7_x64_Mult"
end
end
# Windows 8 VM
config.vm.define "win8" do |win8|
win8.vm.box_url = "file:///C:/V_W_S/Windows8_x64.box"
win8.vm.box = "Windows8_x64"
win8.vm.communicator = "winrm"
win8.vm.boot_timeout = 600
win8.vm.graceful_halt_timeout = 600
win8.vm.network "public_network", auto_config: true
win8.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--usb", "on"]
vb.customize ["modifyvm", :id, "--usbehci", "on"]
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
vb.customize ["modifyvm", :id, "--memory", "4096"]
vb.customize ["modifyvm", :id, "--vram", "256"]
vb.cpus = 2
vb.gui = true
vb.name = "Win_8_x64_Mult"
end
end
end
But I would like to use loop to minimize amount of code. For example in my vagrant file configvalues are the same. Distinctions are win.vm.define value, win.vm.box & win7.vm.box_url values. How can I squeeze amount of Vagrantfile?
I would do something like this
Vagrant.configure("2") do |config|
config.vm.communicator = "winrm"
config.vm.boot_timeout = 600
config.vm.graceful_halt_timeout = 600
config.vm.network "public_network", auto_config: true
# Win7 VM
config.vm.define "win7" do |win7|
win7.vm.box_url = "file:///C:/V_W_S/windows_7_virtualbox.box"
win7.vm.box = "windows_7_virtualbox"
configure_provider(win7, "Win_7_x64_Mult")
end
# Windows 8 VM
config.vm.define "win8" do |win8|
win8.vm.box_url = "file:///C:/V_W_S/Windows8_x64.box"
win8.vm.box = "Windows8_x64"
configure_provider(win8, "Win_8_x64_Mult")
end
end
def configure_provider (node, node_name)
node.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--usb", "on"]
vb.customize ["modifyvm", :id, "--usbehci", "on"]
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
vb.customize ["modifyvm", :id, "--memory", "4096"]
vb.customize ["modifyvm", :id, "--vram", "256"]
vb.cpus = 2
vb.gui = true
vb.name = node_name
end
end
everything that is at config. level applied for all your machines (read https://www.vagrantup.com/docs/multi-machine/)
Just define a ruby function to apply your provider settings
Related
I have two Windows Vagrant boxes (Win 7 & Win 8). I would like to automatically update Guest Additions in that boxes. I have tried vagrant-vbguest plugin - but it doesn't work - exit code - Sorry, don't know how to check guest version of VirtualBox Guest Additions on this platform. Stopping installation.
Vagrant version:
Installed Version: 1.9.2
VirtualBox version:
5.1.18
Vagrantfile
Vagrant.configure("2") do |config|
# General settings for all VMs
config.vm.communicator = "winrm"
config.vm.boot_timeout = 600
config.vm.graceful_halt_timeout = 600
config.vm.network "public_network", auto_config: true
config.vm.provision :shell, path: "Auto_Logon.ps1"
config.vbguest.iso_path = "http://download.virtualbox.org/virtualbox/5.1.18/VBoxGuestAdditions_5.1.18.iso"
# Win7 VM
config.vm.define "win7" do |win7|
win7.vm.box_url = "file:///VagrantBoxes_Win_Series/windows_7_virtualbox.box"
win7.vm.box = "windows_7_virtualbox"
configure_provider(win7, "Win_7_x64_Mult")
end
# Windows 8 VM
config.vm.define "win8" do |win8|
win8.vm.box_url = "file:///VagrantBoxes_Win_Series/Windows8_x64.box"
win8.vm.box = "Windows8_x64"
configure_provider(win8, "Win_8_x64_Mult")
end
end
def configure_provider (node, node_name)
node.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--usb", "on"]
vb.customize ["modifyvm", :id, "--usbehci", "on"]
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
vb.customize ["modifyvm", :id, "--memory", "4096"]
vb.customize ["modifyvm", :id, "--vram", "256"]
vb.cpus = 2
vb.gui = true
vb.name = node_name
end
end
when trying to use NFS with Vagrant to sync folders, on my vagrant I see the permissions as user 501 group dialout.
I fixed the issue disabling NFS and specifying a user and group manually in Vagrant file. I would like to use NFS though.
Here is my VagrantFile
$BOX = "ubuntu/xenial64"
$IP = "10.0.0.10"
$MEMORY = ENV.has_key?('VM_MEMORY') ? ENV['VM_MEMORY'] : "1024"
$CPUS = ENV.has_key?('VM_CPUS') ? ENV['VM_CPUS'] : "1"
Vagrant.configure("2") do |config|
config.vm.hostname = "name.dev"
config.vm.box = $BOX
config.vm.network :private_network, ip: $IP
config.ssh.forward_agent = true
config.vm.synced_folder ".", "/var/www/name/current", type: "nfs"
config.vm.provider "virtualbox" do |v|
v.name = "name
v.customize ["modifyvm", :id, "--cpuexecutioncap", "100"]
v.customize ["modifyvm", :id, "--memory", $MEMORY]
v.customize ["modifyvm", :id, "--cpus", $CPUS]
end
end
I've tried nfsbind but it seems that this require absolute path which I would rather avoid.
Any tips?
Thanks
How do I tell vagrant to provision based on condition? I am spinning up multi VMs under a loop
Vagrantfile:
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Iterate through entries in YAML file
servers.each do |servers|
config.vm.define servers["name"] do |srv|
srv.vm.hostname = servers["name"] + DOMAIN
srv.vm.box = servers["box"]
srv.vm.network "private_network", ip: servers["ip"]
srv.vm.synced_folder ".", "/data", id: "mapping", disabled: servers["share"]
srv.vm.provider :virtualbox do |vb|
vb.memory = servers["ram"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
end
end
end
I want to add something like this:
servers.each do |servers|
config.vm.define servers["name"] do |srv|
srv.vm.hostname = servers["name"] + DOMAIN
srv.vm.box = servers["box"]
srv.vm.network "private_network", ip: servers["ip"]
srv.vm.synced_folder ".", "/data", id: "mapping", disabled: servers["share"]
srv.vm.provider :virtualbox do |vb|
vb.memory = servers["ram"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
if servers["name"] == "server1"
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = "#{cookbooks}"
chef.add_recipe "chef_blah::server"
end
else
puts("Skipping it...")
end
end
end
But every time it executes the provision block on all the VMs
That should be srv.vm.provision, i.e. you are defining it on only that server not the root Vagrant config.
I think I found the solution. It was my mistake
instead of:
config.vm.provision :chef_solo do |chef|
I should have used
srv.vm.provision :chef_solo do |chef|
In the srv loop
I have this vagrant settings below:
12 config.vm.provider "virtualbox" do |v|
13 v.customize ["modifyvm", :id, "--memory", "256"]
14 v.customize ["modifyvm", :id, "--cpus", "1"]
15 v.customize ["modifyvm", :id, "--paravirtprovider", "kvm"] #this should only be applied to LINUX guests
16 end
Found this on this link.
Case is, if VirtualBox version is >= 5.0, this will definitely should work.
But on versions below 5.0, error should be raised.
Question: How am I able to check if --paravirtprovider is enabled in a VirtualBox version. If possible, I want this be done in the Vagrantfile itself. Thank you!
There might be better ways to do this, but one way is:
# -*- mode: ruby -*-
# vi: set ft=ruby :
vbox_version = `VBoxManage --version`
Vagrant.configure(2) do |config|
config.vm.box = 'ubuntu/trusty64'
config.vm.provider 'virtualbox' do |v|
v.customize ['modifyvm', :id, '--memory', '256']
v.customize ['modifyvm', :id, '--cpus', '1']
if vbox_version.to_f >= 5.0
v.customize ['modifyvm', :id, '--paravirtprovider', 'kvm']
end
end
end
vbox_version = `VBoxManage --version` has to happen outside of the configure block because it needs to be executed on the host system running vagrant and VirtualBox.
I am trying to create and then provision a multi-machine vagrant environment using ansible.
My Vagrant file looks like:
# -*- 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|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
(1..3).each do |i|
config.vm.define "db#{i}" do |node|
node.vm.box = "trusty64"
node.vm.network "public_network", ip: "192.168.253.20#{i}", bridge: 'en0: Wi-Fi (AirPort)'
node.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", 512]
v.name = "db#{i}_box"
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
end
end
(1..1).each do |i|
config.vm.define "es#{i}" do |node|
node.vm.box = "trusty64"
node.vm.network "public_network", ip: "192.168.253.21#{i}", bridge: 'en0: Wi-Fi (AirPort)'
node.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", 1024]
v.name = "es#{i}_box"
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
end
end
config.vm.define "emailchecker" do |node|
node.vm.box = "trusty64"
node.vm.network "public_network", ip: "192.168.253.205", bridge: 'en0: Wi-Fi (AirPort)'
node.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", 1024]
v.name = "emailchecker_box"
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
end
config.vm.define "smartrouter" do |node|
node.vm.box = "trusty64"
node.vm.network "public_network", ip: "192.168.253.206", bridge: 'en0: Wi-Fi (AirPort)'
node.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", 1024]
v.name = "smartrouter_box"
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
end
config.vm.define "web" do |node|
node.vm.box = "trusty64"
node.vm.network "public_network", ip: "192.168.253.204", bridge: 'en0: Wi-Fi (AirPort)'
node.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", 1024]
v.name = "web_box"
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
####
#
# Note Provisioning is included here to ensure it only gets run once on all the boxes.
#
####
node.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible_playbook/playbook.yml"
ansible.verbose = "vvvv"
ansible.limit = 'all'
ansible.inventory_path = "ansible_playbook/vagrant_inventory"
end
end
end
I can create the machines by doing a vagrant up --no-provision and I can log into the machines by doing vagrant ssh web (for example).
However, when I try and provision the machines, I get the following error message:
fatal: [web] => SSH encountered an unknown error. The output was:
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /Users/smcgurk/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/Users/smcgurk/.ansible/cp/ansible-ssh-192.168.253.204-22-vagrant" does not exist
Has anyone any idea as to what might be going on here or suggestions about how I debug/ remedy this?
Thanks,
Seán
I have seen this error before and its usually down to spaces!
It seems that when the path to the SSH key contains spaces, these are not correctly escaped, effectively breaking Ansible.
I may be totally off the beaten track here but I have rechecked vagrant files in the past and found this to be the case.
Another thought, if you have any references to localhost instead of 127.0.0.1