-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVagrantfile
66 lines (53 loc) · 1.99 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
vagrantfile_dir = File.dirname(File.realpath(__FILE__))
if File.file?("./vagrant.yml")
config_file = YAML.load_file("./vagrant.yml")
else
config_file = YAML.load_file("#{vagrantfile_dir}/vagrant.yml")
end
settings = config_file
# Helper functions
def destructure_host_dict(dict)
dict.values_at("ip", "hostname", "cpus", "memory", "box")
end
domain = settings["domain"]
# define groups and hostvars for ansible provisionner
ansible_configuration = settings["hosts"].each_with_object({"hostvars": {}, "groups": Hash.new {|hash, key| hash[key] = []}}) do |host, configuration|
ip, hostname, cpus, memory, box = destructure_host_dict(host)
configuration[:hostvars][hostname] = {
:ip => ip,
:domain => domain,
}
host.fetch("groups", []).each do |group|
configuration[:groups][group] << host["hostname"]
end # end group
end # end configuration
Vagrant.configure("2") do |config|
config.vm.synced_folder "./", "/vagrant", disabled: true
settings["hosts"].each do |host|
ip, hostname, cpus, memory, box = destructure_host_dict(host)
box ||= settings["box"]
config.vm.define hostname, autostart: true do |cfg|
cfg.vm.box = box
cfg.vm.hostname = hostname
cfg.vm.network "private_network", ip: ip
cfg.vm.provider :libvirt do |libvirt|
libvirt.cpus = cpus
libvirt.memory = memory
libvirt.qemu_use_session = false
end # end provider libvirt
cfg.vm.provider :virtualbox do |vb|
vb.name = hostname # sets gui name for VM
vb.customize ["modifyvm", :id, "--memory", memory, "--cpus", cpus, "--hwvirtex", "on"]
end # end provider virtualbox
end # end define
end # end settings
config.vm.provision :ansible do |ansible|
ansible.compatibility_mode = "2.0"
ansible.playbook = "#{vagrantfile_dir}/provision.yml"
ansible.host_vars = ansible_configuration[:hostvars]
ansible.groups = ansible_configuration[:groups]
end # end provision
end