Обновить playbook1_keepalived.yml

This commit is contained in:
2026-04-10 05:39:53 +00:00
parent 7435be25d4
commit 5d1d61f190

View File

@@ -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
state: restarted
daemon_reload: true