Skip to content

Instantly share code, notes, and snippets.

@alshell7
Last active May 10, 2021 08:49
Show Gist options
  • Save alshell7/0695fecadb0005f7937a8c2499f39fd5 to your computer and use it in GitHub Desktop.
Save alshell7/0695fecadb0005f7937a8c2499f39fd5 to your computer and use it in GitHub Desktop.
Array of hashes with settings for each virtual machine in Vagrant
# PS D:\vagrant_test> cat .\Vagrantfile
# Prefix for LAN
BRIDGE_NET="192.168.1."
# Prefix for Internal LAN
INTERNAL_NET="192.168.15."
# The domain that we will use for the entire site
DOMAIN="sample.com"
# An array of hashes with settings for each virtual machine
servers=[
{
:hostname => "app1." + DOMAIN,
:ip => BRIDGE_NET + "150",
:ip_int => "1",
:ram => 2000
},
{
:hostname => "app2." + DOMAIN,
:ip => BRIDGE_NET + "151",
:ip_int => "2",
:ram => 2000
},
{
:hostname => "db1." + DOMAIN,
:ip => BRIDGE_NET + "152",
:ip_int => "3",
:ram => 1000,
:hdd_name => "db1_hdd.vdi",
:hdd_size => "10000"
},
{
:hostname => "db2." + DOMAIN,
:ip => BRIDGE_NET + "153",
:ip_int => "4",
:ram => 1000,
:hdd_name => "db2_hdd.vdi",
:hdd_size => "10000"
}
]
# Entering vagrant Version 2 Main Configuration
Vagrant.configure(2) do |config|
# Add a share between host and guest
#config.vm.synced_folder "D://hostmachine/shared/folder", "/src/shara"
# Disable Default
config.vm.synced_folder ".", "/vagrant", disabled: true
# We go through the elements of the "servers" array
servers.each do |machine|
# We apply configurations for each server. The name of the machine (as it will be seen in the Vbox GUI) is in the variable "machine [: hostname]"
config.vm.define machine[:hostname] do |node|
# Raise a machine from the ubuntu 14.04 image that we created in a previous article
node.vm.box = "ubuntu 14.04"
# Also, you can specify the URL where to get the box you need if there is one
#node.vm.box_url = "http://files.vagrantup.com/precise64.box"
# The pool of ports that will be used to connect to each VM through 127.0.0.1
node.vm.usable_port_range = (2200..2250)
# Hostname that will be assigned to VM (OS itself)
node.vm.hostname = machine[:hostname]
#VBoxManage.exe list bridgedifs overwrite NAT adapter :adapter=>1
# Adding and configuring Bridge network adapter (bridge). To find out the exact name of the birdge adapter, you need to use VBoxManage.exe (see below)
node.vm.network "public_network", ip: machine[:ip], bridge: 'Intel(R) Centrino(R) Wireless-N 130'
# Adding and Configuring an Internal Network Adapter (Intnet)
node.vm.network "private_network", ip: machine[:ip_int], virtualbox__intnet: "intnet"
# Configure SSH Access
# Domain / IP to connect
#node.ssh.host = machine[:ip]
# To access by previously added key
#node.ssh.private_key_path = "private_key"
# SSH user login
# node.ssh.username = "alex"
# SSH Password
node.ssh.password = "vagrant"
# Thin settings for a specific provider (in our case - VBoxManage)
node.vm.provider "virtualbox" do |vb|
# RAM size
vb.customize ["modifyvm", :id, "--memory", machine[:ram]]
# You can rewrite VM name in Vbox GUI
vb.name = machine[:hostname]
# Adding a hard disk, if specified in the configuration
if (!machine[:hdd_name].nil?)
# Do not create a disk if it already exists
unless File.exist?(machine[:hdd_name])
vb.customize ["createhd", "--filename", machine[:hdd_name], "--size", machine[:hdd_size]]
end
# Connect the created disk to the streaming VM
vb.customize ["storageattach", :id, "--storagectl", "SATA", "--port", 1, "--device", 0, "--type", "hdd", "--medium", machine[:hdd_name]]
end
# Where to store snapshot
#vb.customize ["modifyvm", :id, "--snapshotfolder", "D:\\test"]
#Another way to change VM name in Vbox GUI
#vb.customize ["modifyvm", :id, "--name", "Gangnam Style"]
end
end
end
end
# In this configuration file, I tried to accommodate more information with comments and various ways of managing Vagrant settings.
# These are the most used options. To determine the name of the birdge adapter, use VBoxManage.exe.
# PS C:\Program Files\Oracle\VirtualBox> .\VBoxManage.exe list bridgedifs
# Here Name is the name of the adapter that needs to be written in the Vagrantfile.
# Now you can raise the test site.
# PS D:\vagrant_test> vagrant up
# Translated from http://sysadm.pp.ua/linux/sistemy-virtualizacii/vagrantfile.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment