3
0
forked from jakub/ansible
Files
ansible_fencl/check_raid.yml
martin.fencl 4038f5b6a1 redo
2025-12-23 23:34:57 +01:00

162 lines
5.0 KiB
YAML

# check_raid.yml
- name: Check Linux MD RAID health on VM via Proxmox
hosts: linux_servers
gather_facts: false
become: true
become_user: root
become_method: sudo
vars:
vm_ip: "{{ lookup('env', 'VM_IP') }}"
vm_user: "{{ lookup('env', 'VM_USER') }}"
vm_pass: "{{ lookup('env', 'VM_PASS') }}"
use_sudo: false
DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}"
RETRIES: "{{ lookup('env', 'RETRIES') | default(25) | int }}"
raid_md_device: "{{ lookup('env', 'RAID_MD') | default('md0', true) }}"
raid_allow_sync: "{{ lookup('env', 'RAID_ALLOW_SYNC') | default(1, true) | int }}"
raid_allow_no_array: "{{ lookup('env', 'RAID_ALLOW_NO_ARRAY') | default(0, true) | int }}"
raid_retries: "{{ RETRIES }}"
raid_delay: 2
ssh_hard_timeout: 30
ssh_opts:
- "-o" # English comments
- "StrictHostKeyChecking=no"
- "-o"
- "UserKnownHostsFile=/dev/null"
- "-o"
- "GlobalKnownHostsFile=/dev/null"
- "-o"
- "LogLevel=ERROR"
- "-o"
- "ConnectTimeout=15"
- "-o"
- "PreferredAuthentications=password"
- "-o"
- "PubkeyAuthentication=no"
- "-o"
- "KbdInteractiveAuthentication=no"
- "-o"
- "NumberOfPasswordPrompts=1"
raid_commands:
- |
python3 - <<'PY'
# Parse /proc/mdstat and validate MD RAID state
import re, sys
target = "{{ raid_md_device }}"
allow_sync = int("{{ raid_allow_sync }}")
allow_no_array = int("{{ raid_allow_no_array }}")
try:
txt = open("/proc/mdstat", "r", encoding="utf-8", errors="ignore").read()
except Exception as e:
print(f"ERROR: cannot read /proc/mdstat: {e}")
sys.exit(2)
arrays = {}
header_re = re.compile(r"^(md\d+)\s*:\s*active.*$", re.MULTILINE)
token_re = re.compile(r"^\s*\d+\s+blocks.*\[\d+/\d+\]\s*\[([U_]+)\]\s*$", re.MULTILINE)
for m in header_re.finditer(txt):
name = m.group(1)
chunk = txt[m.end():m.end() + 3000]
tm = token_re.search(chunk)
if tm:
arrays[name] = tm.group(1)
if not arrays:
print("NO_MD_ARRAYS: /proc/mdstat contains no active md arrays.")
print(txt.strip())
sys.exit(0 if allow_no_array else 2)
syncing = bool(re.search(r"\b(resync|recovery|reshape|check|repair)\b", txt))
if target == "auto":
to_check = sorted(arrays.keys())
else:
if target not in arrays:
print(f"ERROR: {target} not found in /proc/mdstat. Found={sorted(arrays.keys())}")
print(txt.strip())
sys.exit(2)
to_check = [target]
any_degraded = False
for name in to_check:
token = arrays[name]
degraded = "_" in token
any_degraded = any_degraded or degraded
print(f"RAID={name} token=[{token}] degraded={degraded} syncing={syncing} allow_sync={allow_sync}")
if any_degraded:
sys.exit(1)
if syncing and not allow_sync:
sys.exit(1)
sys.exit(0)
PY
tasks:
- name: Ensure sshpass is installed (for password-based SSH) # English comments
ansible.builtin.apt:
name: sshpass
state: present
update_cache: yes
- name: Run RAID check commands on VM (via SSH) # use SSHPASS env, hide item label
ansible.builtin.command:
argv: >-
{{
['timeout', '-k', '5', (ssh_hard_timeout | string)]
+ ['sshpass', '-e', 'ssh']
+ ssh_opts
+ [ vm_user ~ '@' ~ vm_ip,
'bash', '-lc',
('sudo ' if use_sudo else '') + item
]
}}
environment:
SSHPASS: "{{ vm_pass }}"
loop: "{{ raid_commands }}"
loop_control:
index_var: idx
label: "cmd-{{ idx }}"
register: raid_cmds
changed_when: false
failed_when: false
no_log: "{{ DEBUG == 0 }}"
retries: "{{ raid_retries }}"
delay: "{{ raid_delay }}"
until: raid_cmds.rc not in [124, 255]
run_once: true
- name: Show outputs for each RAID command
ansible.builtin.debug:
msg: |
RC: {{ item.rc }}
STDOUT:
{{ (item.stdout | default('')).strip() }}
STDERR:
{{ (item.stderr | default('')).strip() }}
loop: "{{ (raid_cmds.results if (raid_cmds.results is defined) else [raid_cmds]) }}"
when: DEBUG == 1
run_once: true
- name: Fail play if RAID check failed # English comments
ansible.builtin.assert:
that: "item.rc == 0"
fail_msg: "RAID check failed on VM: {{ (item.stdout | default(item.stderr) | default('no output')) | trim }}"
success_msg: "RAID check OK."
loop: "{{ (raid_cmds.results if (raid_cmds.results is defined) else [raid_cmds]) }}"
loop_control:
index_var: idx
label: "cmd-{{ idx }}"
run_once: true