SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3.  
  4. NUMBER_WEBSERVERS = 2
  5. START_NUM_WEB = 0
  6. if START_NUM_WEB == 1
  7.   NUMBER_WEBSERVERS = 1
  8. end
  9. LB_SERVERS = []
  10. NUMBER_LB = 1
  11. NUMBER_DATA = 1
  12. MEMORY = 512
  13. MEMORY_DB = 1024
  14. VM_VERSION = "ubuntu/xenial64"
  15. CUSTOMER_NAME = "0002"
  16. ANSIBLE_INVENTORY_DIR = "hosts"
  17. ANSIBLE_INVENTORY_FILE = "inventory.ini"
  18.  
  19. Vagrant.configure("2") do |config|
  20.   # ensure directory exists
  21.   Dir.mkdir(ANSIBLE_INVENTORY_DIR) unless Dir.exist?(ANSIBLE_INVENTORY_DIR)
  22.  
  23.   # create empty file
  24.   File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'w') do | f |
  25.     f.write ""
  26.   end
  27.  
  28.   (1..NUMBER_WEBSERVERS).each do |i|
  29.     config.vm.define "web0#{i + START_NUM_WEB}" do |web_config|
  30.       web_config.vm.box = VM_VERSION
  31.       web_config.vm.hostname = "Hname-Web0#{i + START_NUM_WEB}-#{CUSTOMER_NAME}"
  32.       web_config.vm.network "private_network", ip: "192.168.2.5#{i + START_NUM_WEB}"
  33.       web_config.vm.provider "virtualbox" do |vb|
  34.         vb.gui = false
  35.  
  36.         vb.memory = MEMORY
  37.         vb.name = "VMname-Web0#{i + START_NUM_WEB}-#{CUSTOMER_NAME}"
  38.       end
  39.  
  40.       if i == 1
  41.         File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  42.           f.write "[webservers]\n"
  43.         end
  44.       end
  45.  
  46.       # add webservers
  47.       File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  48.         f.write "Hname-Web0#{i + START_NUM_WEB}-#{CUSTOMER_NAME} ansible_ssh_port=22 ansible_ssh_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/web0#{i + START_NUM_WEB}/virtualbox/private_key ansible_python_interpreter=/usr/bin/python3\n"
  49.       end
  50.  
  51.       if i == NUMBER_WEBSERVERS
  52.         # add webservers
  53.         File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  54.           f.write "\n"
  55.         end
  56.  
  57.         web_config.vm.provision :ansible do |ansible|
  58.           ansible.compatibility_mode = "2.0"
  59.           ansible.inventory_path = "#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}"
  60.           ansible.limit = "webservers"
  61.           ansible.playbook = "playbook.yml"
  62.           ansible.become = true
  63.           ansible.extra_vars = {
  64.             http_port:80,
  65.             max_clients:200
  66.           }
  67.         end
  68.       end
  69.     end
  70.   end
  71.  
  72.   (1..NUMBER_LB).each do |i|
  73.     config.vm.define "lb0#{i}" do |lb_config|
  74.       lb_config.vm.box = VM_VERSION
  75.       lb_config.vm.hostname = "Hname-Lb0#{i}-#{CUSTOMER_NAME}"
  76.       lb_config.vm.network "private_network", ip: "192.168.2.6#{i}"
  77.       lb_config.vm.provider "virtualbox" do |vb|
  78.         vb.gui = false
  79.  
  80.         vb.memory = MEMORY
  81.         vb.name = "VMname-Lb0#{i}-#{CUSTOMER_NAME}"
  82.       end
  83.       if i == 1
  84.         File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  85.           f.write "[load_balancers]\n"
  86.         end
  87.       end
  88.  
  89.       # add lbservers
  90.       File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  91.         f.write "Hname-Lb0#{i}-#{CUSTOMER_NAME} ansible_ssh_port=22 ansible_ssh_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/lb0#{i}/virtualbox/private_key ansible_python_interpreter=/usr/bin/python3\n"
  92.       end
  93.  
  94.       if i == NUMBER_LB
  95.         # add lbservers
  96.         File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  97.           f.write "\n"
  98.         end
  99.  
  100.         if NUMBER_WEBSERVERS == 1
  101.           if START_NUM_WEB == 0
  102.             LB_SERVERS << {
  103.               "name" => "server1",
  104.               "ip" => "192.168.2.51",
  105.               "port" =>  80,
  106.               "paramstring" => "cookie A check"
  107.             }
  108.           end
  109.           if START_NUM_WEB == 1
  110.             LB_SERVERS << {
  111.               "name" => "server2",
  112.               "ip" => "192.168.2.52",
  113.               "port" =>  80,
  114.               "paramstring" => "cookie A check"
  115.             }
  116.           end
  117.         end
  118.         if NUMBER_WEBSERVERS == 2
  119.           LB_SERVERS << {
  120.             "name" => "server1",
  121.             "ip" => "192.168.2.51",
  122.             "port" =>  80,
  123.             "paramstring" => "cookie A check"
  124.           }
  125.           LB_SERVERS << {
  126.             "name" => "server2",
  127.             "ip" => "192.168.2.52",
  128.             "port" =>  80,
  129.             "paramstring" => "cookie A check"
  130.           }
  131.         end
  132.  
  133.         lb_config.vm.provision :ansible do |ansible|
  134.           ansible.compatibility_mode = "2.0"
  135.           ansible.inventory_path = "#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}"
  136.           ansible.limit = "load_balancers"
  137.           ansible.playbook = "playbook.yml"
  138.           ansible.become = true
  139.           ansible.extra_vars = {
  140.             "haproxy_app_name" => "haproxy",
  141.             "haproxy_mode" => "http",
  142.             "haproxy_enable_stats" => "enable",
  143.             "haproxy_algorithm" => "roundrobin",
  144.             "haproxy_backend_servers" => LB_SERVERS,
  145.             "haproxy_stats_users" => [
  146.               {
  147.                 "username" => "vagrant",
  148.                 "password" => "vagrant"
  149.               },
  150.             ]
  151.           }
  152.         end
  153.       end
  154.     end
  155.   end
  156.  
  157.     (1..NUMBER_DATA).each do |i|
  158.     config.vm.define "db0#{i}" do |db_config|
  159.       db_config.vm.box = VM_VERSION
  160.       db_config.vm.hostname = "Hname-Db0#{i}-#{CUSTOMER_NAME}"
  161.       db_config.vm.network "private_network", ip: "192.168.2.7#{i}"
  162.       db_config.vm.provider "virtualbox" do |vb|
  163.         vb.gui = false
  164.  
  165.         vb.memory = MEMORY
  166.         vb.name = "VMname-Db0#{i}-#{CUSTOMER_NAME}"
  167.       end
  168.  
  169.       if i == 1
  170.         File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  171.           f.write "[databases]\n"
  172.         end
  173.       end
  174.  
  175.         # add dbservers
  176.         File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  177.           f.write "Hname-Db0#{i}-#{CUSTOMER_NAME} ansible_ssh_port=22 ansible_ssh_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/db0#{i}/virtualbox/private_key ansible_python_interpreter=/usr/bin/python3\n"
  178.         end
  179.  
  180.       if i == NUMBER_DATA
  181.         # add dbservers
  182.         File.open("#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}" ,'a') do | f |
  183.           f.write "\n"
  184.         end
  185.  
  186.         db_config.vm.provision :ansible do |ansible|
  187.           ansible.compatibility_mode = "2.0"
  188.           ansible.inventory_path = "#{ANSIBLE_INVENTORY_DIR}/#{ANSIBLE_INVENTORY_FILE}"
  189.           ansible.limit = "databases"
  190.           ansible.playbook = "playbook.yml"
  191.           ansible.become = true
  192.           ansible.extra_vars = {
  193.             "mysql" => {
  194.               "user" => "root",
  195.               "oldpassword" => "root",
  196.               "newpassword" => "root",
  197.               "hosts" => [
  198.                 "Hname-Db0#{i}-#{CUSTOMER_NAME}",
  199.                 "127.0.0.1",
  200.                 "192.168.2.51",
  201.                 "192.168.2.52",
  202.                 "::1",
  203.                 "localhost"
  204.               ],
  205.               "privileges" => "*.*:ALL,GRANT"
  206.             }
  207.           }
  208.         end
  209.       end
  210.     end
  211.   end
  212.  
  213. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top