From 67efc68362a4721cf01221619afcddb7f72a121a Mon Sep 17 00:00:00 2001 From: strNophix Date: Sat, 5 Mar 2022 12:51:32 +0100 Subject: [PATCH] moved ansible inventory generation out of Vagrant --- playbooks/templates/loadbalancer.cfg.j2 | 2 +- self_service.sh | 28 ++++++++++++++++--- site.yml | 0 templates/Vagrantfile.template | 36 ++----------------------- templates/ansible.cfg.template | 3 ++- 5 files changed, 30 insertions(+), 39 deletions(-) create mode 100644 site.yml diff --git a/playbooks/templates/loadbalancer.cfg.j2 b/playbooks/templates/loadbalancer.cfg.j2 index eb528f2..f00bef1 100644 --- a/playbooks/templates/loadbalancer.cfg.j2 +++ b/playbooks/templates/loadbalancer.cfg.j2 @@ -1,6 +1,6 @@ upstream app { {% for host in groups['webserver'] %} - server {{ hostvars[host]['ipv4_address'] }}:80; + server {{ host }}:80; {% endfor %} } diff --git a/self_service.sh b/self_service.sh index 6dca688..390a92e 100755 --- a/self_service.sh +++ b/self_service.sh @@ -3,14 +3,24 @@ copy_template() { cp -f ../../templates/$1 ./$2 } +write_inventory_group() { + local fmt="[$1]\n" + for ((i=$2; i<$2+$3; i++)) do + fmt+="192.168.56.$i\n" + done + echo -e $fmt >> ./inventory +} + +# Take customer inputs read -p "Klantnaam: " customerName -mkdir -p ./customers/$customerName && cd $_ - read -p "IpInt: " ipAddr - read -p "Number of webservers: " numWebserver read -p "Number of loadbalancers: " numLoadbalancers +# Create customer directory and cd +mkdir -p ./customers/$customerName && cd $_ + +# Copy and fill-in necessary templates copy_template ./Vagrantfile.template ./Vagrantfile sed -i "s/#{customerName}/$customerName/" ./Vagrantfile sed -i "s/#{ipAddr}/$ipAddr/" ./Vagrantfile @@ -19,9 +29,21 @@ sed -i "s/#{numLoadbalancers}/$numLoadbalancers/" ./Vagrantfile copy_template ./ansible.cfg.template ./ansible.cfg +# Generate ansible inventory file. +ipOffset=$ipAddr + +write_inventory_group "webserver" $ipOffset $numWebserver +((ipOffset+=numWebserver)) + +write_inventory_group "loadbalancer" $ipOffset $numLoadbalancers +((ipOffset+=numLoadbalancers)) + +# Generate a new seperate ssh key for the customer mkdir -p ./.ssh/ ssh-keygen -t rsa -b 2048 -f ./.ssh/id_rsa ln -s $(realpath ../../playbooks) ./ vagrant up +ansible-playbook ./playbooks/install_nginx.yml +ansible-playbook ./playbooks/install_loadbalancer.yml diff --git a/site.yml b/site.yml new file mode 100644 index 0000000..e69de29 diff --git a/templates/Vagrantfile.template b/templates/Vagrantfile.template index 2c1e62c..4a8dff5 100644 --- a/templates/Vagrantfile.template +++ b/templates/Vagrantfile.template @@ -13,20 +13,9 @@ Vagrant.configure("2") do |config| num_webserver = #{numWebserver} num_loadbalancer = #{numLoadbalancers} - ansible_groups = { - "loadbalancer" => [], - "webserver" => [] - } - ansible_vars = {} - (1..num_webserver).each do |nth| - machine_id = "#{customerName}-bloated-debian-web%d" % [nth] - ansible_groups["webserver"] << machine_id - + machine_id = "#{customerName}-bloated-debian-web%d" % [nth] machine_ip = increment_ip() - ansible_vars[machine_id] = { - "ipv4_address" => machine_ip - } config.vm.define machine_id do |web| web.vm.box = "ubuntu/focal64" @@ -40,25 +29,12 @@ Vagrant.configure("2") do |config| vb.gui = false vb.name = machine_id end - - if nth == num_webserver - config.vm.provision "ansible" do |ansible| - ansible.playbook = "./playbooks/install_nginx.yml" - ansible.groups = ansible_groups - ansible.host_vars = ansible_vars - end - end end end (1..num_loadbalancer).each do |nth| - machine_id = "#{customerName}-bloated-debian-lb%d" % [nth] - ansible_groups["loadbalancer"] << machine_id - + machine_id = "#{customerName}-bloated-debian-lb%d" % [nth] machine_ip = increment_ip() - ansible_vars[machine_id] = { - "ipv4_address" => machine_ip - } config.vm.define machine_id do |web| web.vm.box = "ubuntu/focal64" @@ -72,14 +48,6 @@ Vagrant.configure("2") do |config| vb.gui = false vb.name = machine_id end - - if nth == num_loadbalancer - config.vm.provision "ansible" do |ansible| - ansible.playbook = "./playbooks/install_loadbalancer.yml" - ansible.groups = ansible_groups - ansible.host_vars = ansible_vars - end - end end end end diff --git a/templates/ansible.cfg.template b/templates/ansible.cfg.template index 386f58f..8d56e09 100644 --- a/templates/ansible.cfg.template +++ b/templates/ansible.cfg.template @@ -1,4 +1,5 @@ [defaults] private_key_file = "./.ssh/id_rsa" remote_user = "vagrant" -inventory = ./.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory +inventory = ./inventory +host_key_checking = False