diff --git a/check_raid.yml b/check_raid.yml index 3938d97..9e204cd 100644 --- a/check_raid.yml +++ b/check_raid.yml @@ -1,7 +1,7 @@ # check_raid.yml -- name: Check mdadm RAID status on VM (via SSH from Semaphore host) - hosts: linux_servers +- name: Check mdadm RAID status on VM (via SSH from proxmox_nextcloud) + hosts: proxmox_nextcloud gather_facts: false become: true become_user: root @@ -70,7 +70,7 @@ changed_when: false failed_when: false no_log: "{{ DEBUG == 0 }}" - + - name: Fail if we cannot read /proc/mdstat (SSH/auth/network) # English comments ansible.builtin.assert: that: @@ -81,35 +81,59 @@ success_msg: "Successfully read /proc/mdstat from {{ vm_ip }}." changed_when: false - - name: Parse RAID status from mdstat # English comments + - name: Build base variables # English comments ansible.builtin.set_fact: raid_md: "{{ RAID_DEVICE | regex_replace('^/dev/', '') }}" mdstat_text: "{{ mdstat_cmd.stdout | default('') }}" - raid_present: "{{ (mdstat_cmd.stdout | default('')) is search('(?m)^' ~ (RAID_DEVICE | regex_replace('^/dev/','')) ~ '\\s*:') }}" + changed_when: false + + - name: Extract selected md block from mdstat # English comments + ansible.builtin.set_fact: + raid_block: >- + {{ + ( + mdstat_text + | regex_findall( + '(?ms)^' ~ raid_md ~ '\\s*:.*?(?=^md\\d+\\s*:|^unused devices:|\\Z)' + ) + | first + | default('', true) + ) + }} + changed_when: false + + - name: Parse RAID status from mdstat (safe parsing) # English comments + ansible.builtin.set_fact: + raid_present: "{{ (raid_block | length) > 0 }}" raid_status: >- {{ - (mdstat_cmd.stdout | default('')) | - regex_search( - '(?ms)^' ~ (RAID_DEVICE | regex_replace('^/dev/','')) ~ '\\s*:.*?\\n.*?\\[[0-9]+/[0-9]+\\]\\s*\\[([U_]+)\\]', - '\\1' - ) | default('') + ( + raid_block + | regex_findall('\\[[0-9]+/[0-9]+\\]\\s*\\[([U_]+)\\]') + | first + | default('', true) + ) }} raid_is_degraded: "{{ (raid_status | length > 0) and ('_' in raid_status) }}" - raid_is_rebuilding: "{{ (mdstat_cmd.stdout | default('')) is search('(?i)\\b(resync|recovery|reshape|repair)\\b') }}" - raid_is_checking: "{{ (mdstat_cmd.stdout | default('')) is search('(?i)\\bcheck\\b') }}" + raid_is_rebuilding: "{{ raid_block is search('(?i)\\b(resync|recovery|reshape|repair)\\b') }}" + raid_is_checking: "{{ raid_block is search('(?i)\\bcheck\\b') }}" raid_action_line: >- {{ - (mdstat_cmd.stdout | default('')) | - regex_search( - '(?im)^(\\s*\\[[^\\]]+\\].*\\b(resync|recovery|reshape|repair|check)\\b.*)$', - '\\1' - ) | default('') + ( + raid_block + | regex_findall('(?im)^(\\s*\\[[^\\]]+\\].*\\b(?:resync|recovery|reshape|repair|check)\\b.*)$') + | first + | default('', true) + ) }} raid_progress: >- {{ - (mdstat_cmd.stdout | default('')) | - regex_search('(?i)\\b(resync|recovery|reshape|repair|check)\\b\\s*=\\s*([0-9.]+)%', '\\2') | - default('') + ( + raid_block + | regex_findall('(?i)\\b(?:resync|recovery|reshape|repair|check)\\b\\s*=\\s*([0-9.]+)%') + | first + | default('', true) + ) }} changed_when: false @@ -118,8 +142,9 @@ msg: | --- /proc/mdstat --- {{ mdstat_text }} + --- md block ({{ raid_md }}) --- + {{ raid_block }} --- Parsed --- - raid_md={{ raid_md }} raid_present={{ raid_present }} raid_status={{ raid_status }} raid_is_degraded={{ raid_is_degraded }} @@ -133,7 +158,7 @@ ansible.builtin.assert: that: - raid_present - fail_msg: "RAID {{ raid_md }} not found in /proc/mdstat on {{ vm_ip }}." + fail_msg: "RAID {{ raid_md }} not found in /proc/mdstat on VM {{ vm_ip }}." success_msg: "RAID {{ raid_md }} found in /proc/mdstat." changed_when: false @@ -143,7 +168,7 @@ - not raid_is_degraded fail_msg: >- RAID {{ raid_md }} is DEGRADED: status={{ raid_status }}. - mdstat excerpt={{ (mdstat_text | trim) }} + mdstat excerpt={{ (raid_block | trim) }} success_msg: "RAID {{ raid_md }} is OK: status={{ raid_status }}." changed_when: false @@ -154,7 +179,7 @@ fail_msg: >- RAID {{ raid_md }} is rebuilding/resyncing. {{ 'progress=' ~ raid_progress ~ '%; ' if (raid_progress | length) > 0 else '' }} - line={{ raid_action_line | default('n/a') }} + line={{ (raid_action_line | default('n/a', true)) }} success_msg: "RAID {{ raid_md }} is not rebuilding/resyncing." when: FAIL_ON_RESYNC | int == 1 changed_when: false @@ -166,7 +191,7 @@ fail_msg: >- RAID {{ raid_md }} is running a periodic check. {{ 'progress=' ~ raid_progress ~ '%; ' if (raid_progress | length) > 0 else '' }} - line={{ raid_action_line | default('n/a') }} + line={{ (raid_action_line | default('n/a', true)) }} success_msg: "RAID {{ raid_md }} is not running a periodic check." when: FAIL_ON_CHECK | int == 1 changed_when: false