Finished loadbalancer playbook with config

This commit is contained in:
strNophix 2022-02-23 18:28:35 +01:00
parent 4cb905883c
commit 7fd3bb2c37
2 changed files with 22 additions and 27 deletions

View File

@ -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

View File

@ -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;
}
}
}