From 7fd3bb2c373718d5c59ceb628b71fbe6902eaf68 Mon Sep 17 00:00:00 2001 From: strNophix Date: Wed, 23 Feb 2022 18:28:35 +0100 Subject: [PATCH] Finished loadbalancer playbook with config --- playbooks/install_loadbalancer.yml | 27 +++++++++++-------------- playbooks/templates/loadbalancer.cfg.j2 | 22 +++++++++----------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/playbooks/install_loadbalancer.yml b/playbooks/install_loadbalancer.yml index 95627ae..6421f3b 100644 --- a/playbooks/install_loadbalancer.yml +++ b/playbooks/install_loadbalancer.yml @@ -1,4 +1,4 @@ -- hosts: all +- hosts: loadbalancer tasks: - name: Install nginx package: @@ -6,20 +6,17 @@ state: present update_cache: yes become: true - - name: "Ansible | List all known variables and facts" - debug: - var: hostvars[inventory_hostname] - # - name: Move config to vm - # ansible.builtin.template: - # src: ./templates/loadbalancer.cfg.j2 - # dest: /etc/nginx/sites-available/nginx.cfg - # become: true - # - name: Enable loadbalancer config - # file: - # src: /etc/nginx/sites-available/nginx.cfg - # dest: /etc/nginx/sites-enabled/default - # state: link - # become: true + - name: Move config to vm + ansible.builtin.template: + src: ./templates/loadbalancer.cfg.j2 + dest: /etc/nginx/sites-available/nginx.cfg + become: true + - name: Enable loadbalancer config + file: + src: /etc/nginx/sites-available/nginx.cfg + dest: /etc/nginx/sites-enabled/default + state: link + become: true - name: Restart nginx service: name: nginx diff --git a/playbooks/templates/loadbalancer.cfg.j2 b/playbooks/templates/loadbalancer.cfg.j2 index dd67fe0..eb528f2 100644 --- a/playbooks/templates/loadbalancer.cfg.j2 +++ b/playbooks/templates/loadbalancer.cfg.j2 @@ -1,15 +1,13 @@ -http { - upstream app { - {% for host in ip %} - server {{ host.ip }}; - {% endfor %} - } +upstream app { + {% for host in groups['webserver'] %} + server {{ hostvars[host]['ipv4_address'] }}:80; + {% endfor %} +} - server { - listen 80; +server { + listen 80; - location / { - proxy_pass http://app; - } + location / { + proxy_pass http://app; } -} \ No newline at end of file +}