Обновить 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_instance: "VI_1"
vrrp_id: 51 vrrp_id: 51
auth_pass: "ansible_secure_pass" auth_pass: "ansible_secure_pass"
check_script: "/usr/local/bin/check_haproxy.sh"
master_priority: 150 master_priority: 150
backup_priority: 100 backup_priority: 100
@@ -20,43 +19,36 @@
tags: tags:
- keepalived - keepalived
- name: Deploy HAProxy health check script - name: Detect network interface
ansible.builtin.copy: ansible.builtin.shell: |
content: | ip -br link show | grep -E 'UP|UNKNOWN' | grep -v 'lo' | awk '{print $1}' | head -1
#!/bin/bash register: detected_interface
if pgrep -x "haproxy" > /dev/null; then changed_when: false
if ss -tlnp | grep -q ":80 "; then
exit 0
fi
fi
exit 1
dest: "{{ check_script }}"
mode: '0755'
owner: root
group: root
tags: tags:
- keepalived - 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: ansible.builtin.copy:
content: | content: |
global_defs { global_defs {
router_id {{ inventory_hostname }} 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 }} { vrrp_instance {{ vrrp_instance }} {
state {% if inventory_hostname == 'ha1-cod' %}MASTER{% else %}BACKUP{% endif %} state {% if inventory_hostname == 'ha1-cod' %}MASTER{% else %}BACKUP{% endif %}
interface eth0 interface {{ network_interface }}
virtual_router_id {{ vrrp_id }} virtual_router_id {{ vrrp_id }}
priority {% if inventory_hostname == 'ha1-cod' %}{{ master_priority }}{% else %}{{ backup_priority }}{% endif %} priority {% if inventory_hostname == 'ha1-cod' %}{{ master_priority }}{% else %}{{ backup_priority }}{% endif %}
advert_int 1 advert_int 1
@@ -64,11 +56,8 @@
auth_type PASS auth_type PASS
auth_pass {{ auth_pass }} auth_pass {{ auth_pass }}
} }
track_script {
check_haproxy
}
virtual_ipaddress { virtual_ipaddress {
{{ vip_address }}/{{ vip_cidr }} dev eth0 {{ vip_address }}/{{ vip_cidr }}
} }
} }
dest: /etc/keepalived/keepalived.conf dest: /etc/keepalived/keepalived.conf
@@ -83,6 +72,7 @@
name: keepalived name: keepalived
enabled: true enabled: true
state: started state: started
daemon_reload: true
tags: tags:
- keepalived - keepalived
@@ -90,4 +80,5 @@
- name: Restart keepalived - name: Restart keepalived
ansible.builtin.systemd: ansible.builtin.systemd:
name: keepalived name: keepalived
state: restarted state: restarted
daemon_reload: true