-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVagrantfile
151 lines (127 loc) · 4.97 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Use config.yaml for basic VM configuration.
#
################################################
### --Vagrantfile--
#
# -Project: DevOps#1
################################################
# Vars:
require "yaml"
rsa_pub = File.read(File.join(Dir.home, ".ssh", "id_rsa.pub"))
cfg_file = "./config/config.yaml"
ans_user = "./files/secrets/ansible_user.sec"
ans_pass = "./files/secrets/ansible_pass.sec"
#cur_dir = File.dirname(File.expand_path(__FILE__))
#cfg_file = "#{cur_dir}/config/config.yaml"
if !File.exist?("#{cfg_file}")
raise "---ERROR: Config file missing!, make sure the config file exists & try again ==> #{cfg_file}."
exit
else
vconfig = YAML::load_file("#{cfg_file}")
end
if !File.exist?("#{ans_user}")
raise "---ERROR: Secrets file missing!, make sure the config file exists & try again ==> #{ans_user}."
end
if !File.exist?("#{ans_pass}")
raise "---ERROR: Secrets file missing!, make sure the config file exists & try again ==> #{ans_pass}."
end
NETWORK = vconfig["vagrant_ip"]
DOMAIN = vconfig["vagrant_domain_name"]
RAM = vconfig["vagrant_memory"]
VM = vconfig["vagrant_box"]
VM_VER = vconfig["vagrant_box_version"]
SSH = "22" # SSH port prefix, suffix will be added in :ssh_port
servers = [
{
:hostname => "graf1." + "#{DOMAIN}",
:box => "#{VM}",
:box_ver => "#{VM_VER}",
:ram => "#{RAM}",
:updater => "./files/scripts/updater_deb.sh",
:ip => "#{NETWORK}" + "11",
:ssh_port => "#{SSH}" + "11"
},
{
:hostname => "web1." + "#{DOMAIN}",
:box => "#{VM}",
:ram => "#{RAM}",
:updater => "./files/scripts/updater_deb.sh",
:ip => "#{NETWORK}" + "12",
:ssh_port => "#{SSH}" + "12",
:source => "./files",
:destination => "/home/vagrant"
},
{
:hostname => "web2." + "#{DOMAIN}",
:box => "#{VM}",
:ram => "#{RAM}",
:updater => "./files/scripts/updater_deb.sh",
:ip => "#{NETWORK}" + "13",
:ssh_port => "#{SSH}" + "13",
:source => "./files",
:destination => "/home/vagrant"
},
{
:hostname => "prom1." + "#{DOMAIN}",
:box => "#{VM}",
:ram => "#{RAM}",
:ip => "#{NETWORK}" + "14",
:ssh_port => "#{SSH}" + "14",
:updater => "./files/scripts/updater_deb.sh",
:ansible_install => "./files/scripts/ansible_deb.sh",
:ansible_config => "./files/scripts/ansible_config.sh",
:source => "./files",
:destination => "/home/vagrant"
}]
# Main:
Vagrant.configure(2) do |config|
# use vagrant-vbguest plugin to auto-update the boxes VBGuest additions:
if Vagrant.has_plugin?("vagrant-vbguest")
config.vbguest.auto_update = true
end
servers.each do |machine|
config.vm.define machine[:hostname] do |node|
node.vm.box = machine[:box]
# node.vm.box_version = machine[:box_ver]
node.vm.hostname = machine[:hostname]
node.vm.network :private_network, ip: machine[:ip]
node.vm.network "forwarded_port", guest: 22, host: machine[:ssh_port], id: "ssh"
## VirtualBox Provider:
node.vm.provider :virtualbox do |vbox|
vbox.customize ["modifyvm", :id, "--memory", machine[:ram]]
vbox.customize ["modifyvm", :id, "--name", machine[:hostname]]
end # provider virtualbox ##
## File Provisioner to copy our scripts to the vagrant boxes that need them:
if (!machine[:source].nil?)
node.vm.provision "file", source: machine[:source], destination: machine[:destination], run: "once"
end
## Shell Provisioner to update the repos of our vagrant boxes:
if (!machine[:updater].nil?)
if File.exist?(machine[:updater])
node.vm.provision "shell", privileged: true, path: machine[:updater], run: "once"
end
end
## Ansible Install & Configure:
if (!machine[:ansible_install].nil?)
if File.exist?(machine[:ansible_install])
node.vm.provision "shell", privileged: true, path: machine[:ansible_install], run: "once"
end
if File.exist?(machine[:ansible_config])
node.vm.provision "shell", path: machine[:ansible_config], run: "once"
# we could launch ansible playbooks from here to provision software, but we have more control by invoking them from
# bash scripts:
# node.vm.provision "shell", inline: "sudo -u vagrant ansible-playbook ~vagrant/playbooks/nginx_config.yaml"
end
end # ansible
end # define
end # each
# VM boot timeout increase to 8 minutes instead of the default 300, for lazy boxes:
config.vm.boot_timeout = 480
# Shell provisioner to create SSH key and set the recommended permissions:
config.vm.provision "shell", inline: "echo 'appending SSH public key to ~vagrant/.ssh/authorized_keys' && echo '#{rsa_pub}' >> ~vagrant/.ssh/authorized_keys && chmod 700 ~vagrant/.ssh && chmod 600 ~vagrant/.ssh/authorized_keys"
# don't let vagrant insert it's insecure SSH keypair:
config.ssh.insert_key = false
end # configure
# End