Обновить playbook3_haproxy.yml
This commit is contained in:
@@ -1,172 +1,84 @@
|
|||||||
---
|
---
|
||||||
- name: Configure Nginx Load Balancer (ALT Linux)
|
- name: Configure Keepalived for HA Proxy (ALT Linux)
|
||||||
hosts: proxy
|
hosts: proxy
|
||||||
become: true
|
become: true
|
||||||
vars:
|
vars:
|
||||||
vip_address: "172.16.1.253"
|
vip_address: "172.16.1.253"
|
||||||
backend_servers: "{{ groups['server'] }}"
|
vip_cidr: "23"
|
||||||
backend_port: 443
|
vrrp_instance: "VI_1"
|
||||||
stats_port: 9000
|
vrrp_id: 51
|
||||||
server_name: "www.au.team"
|
auth_pass: "ansible_secure_pass"
|
||||||
ssl_cert_path: "/etc/nginx/ssl/www.au.team.crt"
|
master_priority: 150
|
||||||
ssl_key_path: "/etc/nginx/ssl/www.au.team.key"
|
backup_priority: 100
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: Update package cache (ALT Linux)
|
- name: Install keepalived package (ALT Linux)
|
||||||
ansible.builtin.command:
|
|
||||||
cmd: apt-rpm update
|
|
||||||
changed_when: false
|
|
||||||
tags:
|
|
||||||
- nginx
|
|
||||||
|
|
||||||
- name: Install Nginx package (ALT Linux)
|
|
||||||
ansible.builtin.package:
|
ansible.builtin.package:
|
||||||
name: nginx
|
name: keepalived
|
||||||
state: present
|
state: present
|
||||||
tags:
|
tags:
|
||||||
- nginx
|
- keepalived
|
||||||
|
|
||||||
- name: Create SSL directory for Nginx
|
- name: Detect network interface
|
||||||
ansible.builtin.file:
|
|
||||||
path: /etc/nginx/ssl
|
|
||||||
state: directory
|
|
||||||
mode: '0755'
|
|
||||||
owner: root
|
|
||||||
group: root
|
|
||||||
tags:
|
|
||||||
- ssl
|
|
||||||
|
|
||||||
- name: Copy SSL certificate to Nginx (from web server)
|
|
||||||
ansible.builtin.shell: |
|
ansible.builtin.shell: |
|
||||||
cat /etc/angie/ssl/www.au.team.crt /etc/angie/ssl/www.au.team.key > /etc/nginx/ssl/www.au.team.pem
|
ip -br link show | grep -E 'UP|UNKNOWN' | grep -v 'lo' | awk '{print $1}' | head -1
|
||||||
chmod 600 /etc/nginx/ssl/www.au.team.pem
|
register: detected_interface
|
||||||
args:
|
changed_when: false
|
||||||
creates: /etc/nginx/ssl/www.au.team.pem
|
|
||||||
tags:
|
tags:
|
||||||
- ssl
|
- keepalived
|
||||||
|
|
||||||
- name: Create directories for Nginx config
|
- name: Set interface fact
|
||||||
ansible.builtin.file:
|
ansible.builtin.set_fact:
|
||||||
path: "{{ item }}"
|
network_interface: "{{ detected_interface.stdout | default('eth0') }}"
|
||||||
state: directory
|
|
||||||
mode: '0755'
|
|
||||||
owner: root
|
|
||||||
group: root
|
|
||||||
loop:
|
|
||||||
- /etc/nginx/conf.d
|
|
||||||
- /etc/nginx/sites-available
|
|
||||||
- /etc/nginx/sites-enabled
|
|
||||||
tags:
|
tags:
|
||||||
- nginx
|
- keepalived
|
||||||
|
|
||||||
- name: Configure Nginx upstream for backend servers
|
- name: Debug - show detected interface
|
||||||
|
ansible.builtin.debug:
|
||||||
|
msg: "Using network interface: {{ network_interface }}"
|
||||||
|
tags:
|
||||||
|
- keepalived
|
||||||
|
|
||||||
|
- name: Configure keepalived.conf (simple config without health check)
|
||||||
ansible.builtin.copy:
|
ansible.builtin.copy:
|
||||||
content: |
|
content: |
|
||||||
upstream backend_servers {
|
global_defs {
|
||||||
balance roundrobin;
|
router_id {{ inventory_hostname }}
|
||||||
{% for server in backend_servers %}
|
|
||||||
server {{ hostvars[server]['ansible_host'] | default(server) }}:{{ backend_port }} weight=1 max_fails=3 fail_timeout=30s;
|
|
||||||
{% endfor %}
|
|
||||||
}
|
}
|
||||||
dest: /etc/nginx/conf.d/upstream.conf
|
|
||||||
|
vrrp_instance {{ vrrp_instance }} {
|
||||||
|
state {% if inventory_hostname == 'ha1-cod' %}MASTER{% else %}BACKUP{% endif %}
|
||||||
|
interface {{ network_interface }}
|
||||||
|
virtual_router_id {{ vrrp_id }}
|
||||||
|
priority {% if inventory_hostname == 'ha1-cod' %}{{ master_priority }}{% else %}{{ backup_priority }}{% endif %}
|
||||||
|
advert_int 1
|
||||||
|
authentication {
|
||||||
|
auth_type PASS
|
||||||
|
auth_pass {{ auth_pass }}
|
||||||
|
}
|
||||||
|
virtual_ipaddress {
|
||||||
|
{{ vip_address }}/{{ vip_cidr }}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dest: /etc/keepalived/keepalived.conf
|
||||||
mode: '0644'
|
mode: '0644'
|
||||||
backup: true
|
backup: true
|
||||||
notify: Reload nginx
|
notify: Restart keepalived
|
||||||
tags:
|
tags:
|
||||||
- nginx
|
- keepalived
|
||||||
|
|
||||||
- name: Configure Nginx vhost with SSL and load balancing
|
- name: Enable and start keepalived service
|
||||||
ansible.builtin.copy:
|
|
||||||
content: |
|
|
||||||
# HTTP server - redirect to HTTPS
|
|
||||||
server {
|
|
||||||
listen {{ vip_address }}:80;
|
|
||||||
listen [::]:80;
|
|
||||||
server_name {{ server_name }};
|
|
||||||
return 301 https://$host$request_uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
# HTTPS server with load balancing
|
|
||||||
server {
|
|
||||||
listen {{ vip_address }}:443 ssl;
|
|
||||||
listen [::]:443 ssl;
|
|
||||||
server_name {{ server_name }};
|
|
||||||
|
|
||||||
ssl_certificate /etc/nginx/ssl/www.au.team.pem;
|
|
||||||
ssl_protocols TLSv1.2 TLSv1.3;
|
|
||||||
ssl_ciphers HIGH:!aNULL:!MD5;
|
|
||||||
ssl_prefer_server_ciphers on;
|
|
||||||
|
|
||||||
# Proxy to backend servers
|
|
||||||
location / {
|
|
||||||
proxy_pass https://backend_servers;
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
|
||||||
proxy_ssl_verify off;
|
|
||||||
proxy_connect_timeout 10s;
|
|
||||||
proxy_send_timeout 30s;
|
|
||||||
proxy_read_timeout 30s;
|
|
||||||
}
|
|
||||||
|
|
||||||
# HSTS header
|
|
||||||
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Stats server
|
|
||||||
server {
|
|
||||||
listen {{ vip_address }}:{{ stats_port }};
|
|
||||||
server_name {{ server_name }};
|
|
||||||
|
|
||||||
location / {
|
|
||||||
stub_status on;
|
|
||||||
allow 127.0.0.1;
|
|
||||||
allow 10.0.0.0/8;
|
|
||||||
allow 172.16.0.0/12;
|
|
||||||
allow 192.168.0.0/16;
|
|
||||||
deny all;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /haproxy_stats {
|
|
||||||
return 200 "Nginx Stats\nActive connections: $connections_active\n";
|
|
||||||
add_header Content-Type text/plain;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dest: /etc/nginx/sites-available/www.au.team.conf
|
|
||||||
mode: '0644'
|
|
||||||
backup: true
|
|
||||||
notify: Reload nginx
|
|
||||||
tags:
|
|
||||||
- nginx
|
|
||||||
|
|
||||||
- name: Enable site configuration
|
|
||||||
ansible.builtin.file:
|
|
||||||
src: /etc/nginx/sites-available/www.au.team.conf
|
|
||||||
dest: /etc/nginx/sites-enabled/www.au.team.conf
|
|
||||||
state: link
|
|
||||||
notify: Reload nginx
|
|
||||||
tags:
|
|
||||||
- nginx
|
|
||||||
|
|
||||||
- name: Remove default Nginx site
|
|
||||||
ansible.builtin.file:
|
|
||||||
path: /etc/nginx/sites-enabled/default
|
|
||||||
state: absent
|
|
||||||
ignore_errors: true
|
|
||||||
tags:
|
|
||||||
- nginx
|
|
||||||
|
|
||||||
- name: Enable and start Nginx service
|
|
||||||
ansible.builtin.systemd:
|
ansible.builtin.systemd:
|
||||||
name: nginx
|
name: keepalived
|
||||||
enabled: true
|
enabled: true
|
||||||
state: started
|
state: started
|
||||||
|
daemon_reload: true
|
||||||
tags:
|
tags:
|
||||||
- nginx
|
- keepalived
|
||||||
|
|
||||||
handlers:
|
handlers:
|
||||||
- name: Reload nginx
|
- name: Restart keepalived
|
||||||
ansible.builtin.systemd:
|
ansible.builtin.systemd:
|
||||||
name: nginx
|
name: keepalived
|
||||||
state: reloaded
|
state: restarted
|
||||||
|
daemon_reload: true
|
||||||
Reference in New Issue
Block a user