This commit is contained in:
martin.fencl
2025-12-23 21:08:39 +01:00
parent 1b1806907a
commit c3ff0514ee

View File

@@ -87,56 +87,75 @@
mdstat_text: "{{ mdstat_cmd.stdout | default('') }}"
changed_when: false
- name: Extract selected md block from mdstat # English comments
- name: Extract selected md block from mdstat (no regex) # 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)
)
}}
{%- set ns = namespace(block='') -%}
{%- for b in (mdstat_text.split('\n\n')) -%}
{%- set bb = (b | trim) -%}
{%- if bb.startswith(raid_md ~ ' :') or bb.startswith(raid_md ~ ':') -%}
{%- set ns.block = bb -%}
{%- endif -%}
{%- endfor -%}
{{ ns.block }}
changed_when: false
- name: Parse RAID status from mdstat (safe parsing) # English comments
- name: Parse RAID status from mdstat (no regex) # English comments
ansible.builtin.set_fact:
raid_present: "{{ (raid_block | length) > 0 }}"
raid_present: "{{ (raid_block | trim | length) > 0 }}"
# Extract [UU] / [U_] token by scanning bracket tokens and keeping only U/_ chars
raid_status: >-
{{
(
raid_block
| regex_findall('\\[[0-9]+/[0-9]+\\]\\s*\\[([U_]+)\\]')
| first
| default('', true)
)
}}
{%- set ns = namespace(st='') -%}
{%- for line in (raid_block.splitlines() if (raid_block | length) > 0 else []) -%}
{%- for tok in line.split() -%}
{%- if tok.startswith('[') and tok.endswith(']') -%}
{%- set inner = tok[1:-1] -%}
{%- if (inner | length) > 0 and ((inner | list | difference(['U','_'])) | length == 0) -%}
{%- set ns.st = inner -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{{ ns.st }}
raid_is_degraded: "{{ (raid_status | length > 0) and ('_' in raid_status) }}"
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_block_lower: "{{ raid_block | lower }}"
raid_is_rebuilding: >-
{{
('resync' in raid_block_lower) or
('recovery' in raid_block_lower) or
('reshape' in raid_block_lower) or
('repair' in raid_block_lower)
}}
raid_is_checking: "{{ 'check' in raid_block_lower }}"
# First line that contains an action keyword
raid_action_line: >-
{{
(
raid_block
| regex_findall('(?im)^(\\s*\\[[^\\]]+\\].*\\b(?:resync|recovery|reshape|repair|check)\\b.*)$')
| first
| default('', true)
)
}}
{%- set ns = namespace(line='') -%}
{%- set keys = ['resync','recovery','reshape','repair','check'] -%}
{%- for line in (raid_block.splitlines() if (raid_block | length) > 0 else []) -%}
{%- set l = (line | lower) -%}
{%- for k in keys -%}
{%- if ns.line == '' and (k in l) -%}
{%- set ns.line = (line | trim) -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{{ ns.line }}
# Parse progress from ".... = 12.3%" if present
raid_progress: >-
{{
(
raid_block
| regex_findall('(?i)\\b(?:resync|recovery|reshape|repair|check)\\b\\s*=\\s*([0-9.]+)%')
| first
| default('', true)
)
}}
{%- set ns = namespace(p='') -%}
{%- if (raid_action_line | length) > 0 and ('=' in raid_action_line) and ('%' in raid_action_line) -%}
{%- set right = raid_action_line.split('=', 1)[1] -%}
{%- set ns.p = (right.split('%', 1)[0] | trim) -%}
{%- endif -%}
{{ ns.p }}
changed_when: false
- name: Debug | Show mdstat and parsed values # English comments
ansible.builtin.debug:
msg: |