aboutsummaryrefslogtreecommitdiffhomepage
path: root/Vagrant/roles/ansible-rabbitmq/Vagrant/Vagrantfile
blob: bca571814e60976a14fb05c561926b7837765966 (plain) (blame)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.

# ---- Define number of nodes to spin up ----
N = 3

# ---- Define any custom memory/cpu requirement ----
# if custom requirements are desired...ensure to set
# custom_cpu_mem == true otherwise set to false
# By default if custom requirements are defined and set below
# any node not defined will be configured as the default...
# which is 1vCPU/512mb...So if setting custom requirements
# only define any node which requires more than the defaults.
nodes = [
  {
    :node => "node0",
    :box => "mrlesmithjr/trusty64",
    :cpu => 1,
    :mem => 1024
  }
]

# ---- Define variables below ----
#Define if additional disks should be added (true|false)
additional_disks = false
additional_disks_controller = "SATA Controller"
#Define the number of additional disks to add
additional_disks_num = 1
#Define disk size in GB
additional_disks_size = 10
#Define if additional network adapters should be created (true|false)
additional_nics = true
#Define if add'l network adapters are auto configured addresses (true|false)
additional_nics_auto_config = true
#Define if additional network adapters should be DHCP assigned (true|false)
additional_nics_dhcp = false
#Define the number of additional nics to add
additional_nics_num = 1
ansible_groups = {
  "test-nodes" => ["node[0:#{N-1}]"]
}
#Define Vagrant box to load
box = "ubuntu/trusty64"
#Define if custom cpu and memory requirements are needed (true|false)
  #defined within nodes variable above
custom_cpu_mem = false
#Define if running desktop OS (true|false)
desktop = false
#Define if custom boxes should be used...defined in nodes var..
enable_custom_boxes = false
#Define if port forwards should be enabled (true|false)
enable_port_forwards = false
#Defines if nodes should be linked from master VM (true|false)
linked_clones = false
port_forwards = [
  {
    :node => "node0",
    :guest => 3306,
    :host => 3306
  },
  {
    :node => "node0",
    :guest => 80,
    :host => 8080
  },
  {
    :node => "node0",
    :guest => 8000,
    :host => 8000
  }
]
#Define if provisioners should run (true|false)
provision_nodes = true
#Define if IP's are random assigned if not DHCP (true|false)
random_ips = false
#Define number of CPU cores
  #will be ignored if custom_cpu_mem == true
server_cpus = 1
#Define amount of memory to assign to node(s)
  #will be ignored if custom_cpu_mem == true
server_memory = 512
#Define subnet for private_network (If not using DHCP)
subnet = "192.168.202."
#Define starting last octet of the subnet range to begin addresses for node(s)
subnet_ip_start = 200

Vagrant.configure(2) do |config|

  #Iterate over nodes
  (1..N).each do |node_id|
    nid = (node_id - 1)

    config.vm.define "node#{nid}" do |node|
      if enable_custom_boxes
        #Initially no so it can be set to yes if found in custom box defined
        box_set = "no"
        nodes.each do |cust_box|
          if cust_box[:node] == "node#{nid}"
            node.vm.box = cust_box[:box]
            box_set = "yes"
          end
        end
        if box_set == "no"
          node.vm.box = box
        end
      end
      if not enable_custom_boxes
        node.vm.box = box
      end
      node.vm.provider "virtualbox" do |vb|
        if linked_clones
          vb.linked_clone = true
        end
        if not custom_cpu_mem
          vb.customize ["modifyvm", :id, "--cpus", server_cpus]
          vb.customize ["modifyvm", :id, "--memory", server_memory]
        end
        if custom_cpu_mem
          nodes.each do |cust_node|
            if cust_node[:node] == "node#{nid}"
              vb.customize ["modifyvm", :id, "--cpus", cust_node[:cpu]]
              vb.customize ["modifyvm", :id, "--memory", cust_node[:mem]]
            end
          end
        end

        # Setup desktop environment
        if desktop
          vb.gui = true
          vb.customize ["modifyvm", :id, "--graphicscontroller", "vboxvga"]
          vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
          vb.customize ["modifyvm", :id, "--ioapic", "on"]
          vb.customize ["modifyvm", :id, "--vram", "128"]
          vb.customize ["modifyvm", :id, "--hwvirtex", "on"]
        end

        # Add additional disks
        if additional_disks
          (1..additional_disks_num).each do |disk_num|
            dnum = (disk_num + 1)
            ddev = ("node#{nid}_Disk#{dnum}.vdi")
            unless File.exist?("#{ddev}")
              vb.customize ['createhd', '--filename', ("#{ddev}"), \
                '--variant', 'Fixed', '--size', additional_disks_size * 1024]
            end
            vb.customize ['storageattach', :id,  '--storagectl', \
              "#{additional_disks_controller}", '--port', dnum, '--device', 0, \
              '--type', 'hdd', '--medium', "node#{nid}_Disk#{dnum}.vdi"]
          end
        end
      end
      node.vm.hostname = "node#{nid}"

      # Define additional network adapters below
      if additional_nics
        if not additional_nics_dhcp
          (1..additional_nics_num).each do |nic_num|
            if random_ips
              nnum = Random.rand(0..50)
              if additional_nics_auto_config
                node.vm.network :private_network, \
                ip: subnet+"#{subnet_ip_start + nid + nnum}"
              end
              if not additional_nics_auto_config
                node.vm.network :private_network, \
                ip: subnet+"#{subnet_ip_start + nid + nnum}",
                  auto_config: false
              end
            end
            if not random_ips
              if additional_nics_auto_config
                node.vm.network :private_network, \
                ip: subnet+"#{subnet_ip_start + nid}"
              end
              if not additional_nics_auto_config
                node.vm.network :private_network, \
                ip: subnet+"#{subnet_ip_start + nid}",
                  auto_config: false
              end
            end
          end
        end
        if additional_nics_dhcp
          (1..additional_nics_num).each do |nic_num|
            node.vm.network :private_network, type: "dhcp"
          end
        end
      end

      # Define port forwards below
      if enable_port_forwards
        port_forwards.each do |pf|
          if pf[:node] == "node#{nid}"
            node.vm.network :forwarded_port, guest: pf[:guest], \
            host: pf[:host]
          end
        end
      end

      # Provisioners
      if provision_nodes
        if node_id == N
          node.vm.provision "ansible" do |ansible|
            ansible.limit = "all"
            #runs bootstrap Ansible playbook
            ansible.playbook = "bootstrap.yml"
          end
          node.vm.provision "ansible" do |ansible|
            ansible.limit = "all"
            #runs Ansible playbook for installing roles/executing tasks
            ansible.playbook = "playbook.yml"
            ansible.groups = ansible_groups
          end
        end
      end

    end
  end
  if provision_nodes
    #runs initial shell script
    config.vm.provision :shell, path: "bootstrap.sh", keep_color: "true"
  end
end