From 5d1d61f190750b2de2e3d8ed0acbdd25afa32ba4 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 10 Apr 2026 05:39:53 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20playbook1=5Fkeepalived.yml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playbook1_keepalived.yml | 55 +++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/playbook1_keepalived.yml b/playbook1_keepalived.yml index fdc6c84..c4636d4 100644 --- a/playbook1_keepalived.yml +++ b/playbook1_keepalived.yml @@ -8,7 +8,6 @@ vrrp_instance: "VI_1" vrrp_id: 51 auth_pass: "ansible_secure_pass" - check_script: "/usr/local/bin/check_haproxy.sh" master_priority: 150 backup_priority: 100 @@ -20,43 +19,36 @@ tags: - keepalived - - name: Deploy HAProxy health check script - ansible.builtin.copy: - content: | - #!/bin/bash - if pgrep -x "haproxy" > /dev/null; then - if ss -tlnp | grep -q ":80 "; then - exit 0 - fi - fi - exit 1 - dest: "{{ check_script }}" - mode: '0755' - owner: root - group: root + - name: Detect network interface + ansible.builtin.shell: | + ip -br link show | grep -E 'UP|UNKNOWN' | grep -v 'lo' | awk '{print $1}' | head -1 + register: detected_interface + changed_when: false tags: - keepalived - - name: Configure keepalived.conf + - name: Set interface fact + ansible.builtin.set_fact: + network_interface: "{{ detected_interface.stdout | default('eth0') }}" + tags: + - keepalived + + - 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: content: | global_defs { router_id {{ inventory_hostname }} - script_user root - enable_script_security - } - - vrrp_script check_haproxy { - script "{{ check_script }}" - interval 2 - weight -20 - fall 3 - rise 2 } vrrp_instance {{ vrrp_instance }} { state {% if inventory_hostname == 'ha1-cod' %}MASTER{% else %}BACKUP{% endif %} - interface eth0 + interface {{ network_interface }} virtual_router_id {{ vrrp_id }} priority {% if inventory_hostname == 'ha1-cod' %}{{ master_priority }}{% else %}{{ backup_priority }}{% endif %} advert_int 1 @@ -64,11 +56,8 @@ auth_type PASS auth_pass {{ auth_pass }} } - track_script { - check_haproxy - } virtual_ipaddress { - {{ vip_address }}/{{ vip_cidr }} dev eth0 + {{ vip_address }}/{{ vip_cidr }} } } dest: /etc/keepalived/keepalived.conf @@ -83,6 +72,7 @@ name: keepalived enabled: true state: started + daemon_reload: true tags: - keepalived @@ -90,4 +80,5 @@ - name: Restart keepalived ansible.builtin.systemd: name: keepalived - state: restarted \ No newline at end of file + state: restarted + daemon_reload: true \ No newline at end of file