Skip to content

Commit 5d98fcd

Browse files
rguichardclaude
andcommitted
Add auto-detection feature for RAID array creation
This commit introduces an auto-detection mechanism that automatically identifies unused disks and creates RAID arrays without manual configuration. Changes: - Add detect_unused_disks.yml task to identify available disks - Update main.yml to integrate auto-detection workflow - Add auto-detection configuration options in defaults/main.yml - Update README.md with auto-detection usage examples The feature detects disks that are not mounted, not in RAID, not in LVM, and not formatted, then uses them to create RAID arrays based on the configured template. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent e3eaee4 commit 5d98fcd

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,41 @@ None
1717

1818
## Example Playbook
1919

20+
### Manual configuration
2021
```yaml
2122
- hosts: all
2223
become: true
2324
vars:
25+
mdadm_arrays:
26+
- name: md200
27+
devices:
28+
- /dev/nvme0n1
29+
- /dev/nvme1n1
30+
filesystem: lvm
31+
level: 5
32+
state: present
2433
roles:
2534
- role: ansible-mdadm
26-
tasks:
2735
```
2836
37+
### Auto-detection mode
38+
```yaml
39+
- hosts: all
40+
become: true
41+
vars:
42+
mdadm_auto_detect_arrays: true
43+
mdadm_auto_detect_config:
44+
- name: md200
45+
filesystem: lvm
46+
level: 5
47+
state: present
48+
min_disks: 3
49+
roles:
50+
- role: ansible-mdadm
51+
```
52+
53+
Note: Auto-detection will automatically use all unused disks (not mounted, not in RAID, not in LVM, not formatted) if `mdadm_arrays` is empty and `mdadm_auto_detect_arrays` is true.
54+
2955
## License
3056

3157
BSD

defaults/main.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@
3535
# mountpoint: '/mnt/md0'
3636
# state: 'present'
3737
mdadm_arrays: []
38+
39+
# Auto-detection settings
40+
mdadm_auto_detect_arrays: false
41+
mdadm_auto_detect_config:
42+
# Default configuration for auto-detected arrays
43+
- name: md200
44+
filesystem: lvm
45+
level: 5
46+
state: present
47+
# Minimum number of disks required for auto-detection
48+
min_disks: 3

tasks/detect_unused_disks.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
# Detect unused disks that can be used for RAID arrays
3+
# This task will identify disks that are:
4+
# - Not mounted
5+
# - Not part of existing RAID arrays
6+
# - Not used in LVM
7+
# - Not formatted with a filesystem
8+
9+
- name: detect_unused_disks | Get all block devices
10+
shell: lsblk -ln -o NAME,TYPE,MOUNTPOINT,FSTYPE | grep -E '^[a-z]+[0-9]*\s+disk\s*$' | awk '{print "/dev/" $1}'
11+
register: all_disks
12+
changed_when: false
13+
14+
- name: detect_unused_disks | Get mounted devices
15+
shell: mount | awk '{print $1}' | grep '^/dev/' | sort | uniq
16+
register: mounted_devices
17+
changed_when: false
18+
19+
- name: detect_unused_disks | Get devices used in existing RAID arrays
20+
shell: cat /proc/mdstat | grep -oE 'sd[a-z]+[0-9]*|nvme[0-9]+n[0-9]+' | sed 's|^|/dev/|' | sort | uniq
21+
register: raid_devices
22+
changed_when: false
23+
failed_when: false
24+
25+
- name: detect_unused_disks | Get devices used in LVM
26+
shell: pvs --noheadings -o pv_name 2>/dev/null | tr -d ' ' | sort | uniq
27+
register: lvm_devices
28+
changed_when: false
29+
failed_when: false
30+
31+
- name: detect_unused_disks | Get devices with filesystem
32+
shell: blkid | cut -d: -f1 | sort | uniq
33+
register: formatted_devices
34+
changed_when: false
35+
failed_when: false
36+
37+
- name: detect_unused_disks | Filter unused disks
38+
set_fact:
39+
unused_disks: "{{ all_disks.stdout_lines |
40+
difference(mounted_devices.stdout_lines | default([])) |
41+
difference(raid_devices.stdout_lines | default([])) |
42+
difference(lvm_devices.stdout_lines | default([])) |
43+
difference(formatted_devices.stdout_lines | default([])) }}"
44+
45+
- name: detect_unused_disks | Debug unused disks found
46+
debug:
47+
msg: "Unused disks detected: {{ unused_disks }}"
48+
when: unused_disks | length > 0

tasks/main.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@
1212
when: >
1313
mdadm_force_wipe is defined and
1414
mdadm_force_wipe
15+
16+
- include_tasks: detect_unused_disks.yml
17+
when: >
18+
mdadm_auto_detect_arrays and
19+
(mdadm_arrays | length == 0)
20+
21+
- name: main | Set auto-detected arrays
22+
set_fact:
23+
mdadm_arrays: "{{ mdadm_auto_detect_config | map('combine', {'devices': unused_disks}) | list }}"
24+
when: >
25+
mdadm_auto_detect_arrays and
26+
(mdadm_arrays | length == 0) and
27+
(unused_disks | length >= mdadm_auto_detect_config[0].min_disks)
28+
1529
- include_tasks: arrays.yml

0 commit comments

Comments
 (0)