Refactoring + final touches
This commit is contained in:
@@ -1,22 +1,66 @@
|
||||
---
|
||||
# tasks file for linstor_storage
|
||||
# NOTE: linstor_storage_pools[].targets keys are Ansible inventory_hostname values.
|
||||
# All Linstor registrations use ansible_hostname (real system hostname) instead,
|
||||
# to match Proxmox node names. The mapping is done per-host at registration time.
|
||||
|
||||
- name: Configure LVM global_filter for DRBD safely
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/lvm/lvm.conf
|
||||
insertafter: '^[ \t]*devices[ \t]*\{'
|
||||
# This regex ensures we only ever have ONE DRBD filter line, replacing the old one if it exists
|
||||
regexp: '^[ \t]*global_filter[ \t]*=.*r\|\^/dev/drbd\|'
|
||||
regexp: '^[ \t]*global_filter[ \t]*='
|
||||
line: ' global_filter = [ "r|^/dev/drbd|", "r|^/dev/mapper/[lL]instor|" ]'
|
||||
state: present
|
||||
|
||||
- name: Remove duplicate global_filter entries from lvm.conf
|
||||
ansible.builtin.shell: |
|
||||
count=$(grep -c '^[[:space:]]*global_filter[[:space:]]*=' /etc/lvm/lvm.conf)
|
||||
if [ "$count" -gt 1 ]; then
|
||||
awk '/^[[:space:]]*global_filter[[:space:]]*=/{if(++n>1)next}1' /etc/lvm/lvm.conf > /tmp/lvm.conf.dedup
|
||||
mv /tmp/lvm.conf.dedup /etc/lvm/lvm.conf
|
||||
echo "changed: removed $((count - 1)) duplicate(s)"
|
||||
fi
|
||||
args:
|
||||
executable: /bin/bash
|
||||
register: lvm_dedup_result
|
||||
changed_when: "'changed' in lvm_dedup_result.stdout"
|
||||
|
||||
- name: Wipe existing signatures and partition tables from raw disks
|
||||
ansible.builtin.shell: |
|
||||
DEV="{{ item.targets[inventory_hostname] }}"
|
||||
VG="{{ item.vg_name }}"
|
||||
|
||||
# Deactivate VG properly if LVM still knows about it
|
||||
vgchange -an "$VG" 2>/dev/null || true
|
||||
|
||||
# Forcibly remove any stale dm-mapper devices for this VG (handles the case
|
||||
# where the VG metadata was already wiped but kernel devices are still active)
|
||||
dmsetup ls 2>/dev/null \
|
||||
| awk -v vg="$VG" 'index($1, vg"-") == 1 { print $1 }' \
|
||||
| xargs -r dmsetup remove --force 2>/dev/null || true
|
||||
|
||||
wipefs -a "$DEV"
|
||||
sgdisk --zap-all "$DEV"
|
||||
partprobe "$DEV" 2>/dev/null || true
|
||||
args:
|
||||
executable: /bin/bash
|
||||
changed_when: true
|
||||
loop: "{{ linstor_storage_pools }}"
|
||||
when:
|
||||
- wipe_linstor_disks | default(false) | bool
|
||||
- item.targets[inventory_hostname] is defined
|
||||
- item.targets[inventory_hostname].startswith('/dev/')
|
||||
|
||||
- name: Ensure Volume Groups exist for raw disks
|
||||
community.general.lvg:
|
||||
vg: "{{ item.vg_name }}"
|
||||
pvs: "{{ item.targets[inventory_hostname] }}"
|
||||
pv_options: "{{ '--force --force' if wipe_linstor_disks | default(false) | bool else '--force' }}"
|
||||
loop: "{{ linstor_storage_pools }}"
|
||||
# Jinja2 magic: Only run this LVM task if the target string starts with '/dev/'
|
||||
when: item.targets[inventory_hostname].startswith('/dev/')
|
||||
# Only run for pools that target this node AND use a raw block device
|
||||
when:
|
||||
- item.targets[inventory_hostname] is defined
|
||||
- item.targets[inventory_hostname].startswith('/dev/')
|
||||
|
||||
- name: Ensure LVM Thin Pools exist for raw disks
|
||||
community.general.lvol:
|
||||
@@ -26,25 +70,31 @@
|
||||
opts: "-T -Zn"
|
||||
shrink: false
|
||||
loop: "{{ linstor_storage_pools }}"
|
||||
when: item.targets[inventory_hostname].startswith('/dev/')
|
||||
when:
|
||||
- item.targets[inventory_hostname] is defined
|
||||
- item.targets[inventory_hostname].startswith('/dev/')
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# LINSTOR STORAGE REGISTRATION (Executed ONLY on Node 1)
|
||||
# Uses ansible_hostname (real system hostname) for Linstor node names
|
||||
# ---------------------------------------------------------
|
||||
|
||||
- name: Register Storage Pools in LINSTOR
|
||||
ansible.builtin.shell: |
|
||||
set -o pipefail
|
||||
|
||||
# We use Jinja2 to write a custom bash loop for exactly the nodes in our inventory
|
||||
# Iterate over all inventory hosts for this pool.
|
||||
# Use ansible_hostname (real system hostname) as Linstor node name.
|
||||
{% for host in groups['proxmox'] %}
|
||||
{% if item.targets[host] is defined %}
|
||||
{% set target = item.targets[host] %}
|
||||
{% set lvm_path = (item.vg_name + '/' + item.thin_name) if target.startswith('/dev/') else target %}
|
||||
|
||||
if ! linstor storage-pool list | grep -q " {{ host }} .* {{ item.pool_name }} "; then
|
||||
linstor storage-pool create lvmthin "{{ host }}" "{{ item.pool_name }}" "{{ lvm_path }}" > /dev/null
|
||||
NODE_NAME="{{ hostvars[host]['ansible_facts']['hostname'] }}"
|
||||
if ! linstor storage-pool list | grep -q " ${NODE_NAME} .* {{ item.pool_name }} "; then
|
||||
linstor storage-pool create lvmthin "${NODE_NAME}" "{{ item.pool_name }}" "{{ lvm_path }}" > /dev/null
|
||||
echo "changed"
|
||||
fi
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
args:
|
||||
executable: /bin/bash
|
||||
@@ -57,7 +107,9 @@
|
||||
ansible.builtin.shell: |
|
||||
set -o pipefail
|
||||
if ! linstor resource-group list | grep -q " {{ item.rg_name }} "; then
|
||||
linstor resource-group create "{{ item.rg_name }}" --storage-pool "{{ item.pool_name }}" --place-count 3 > /dev/null
|
||||
linstor resource-group create "{{ item.rg_name }}" \
|
||||
--storage-pool "{{ item.pool_name }}" \
|
||||
--place-count {{ groups['proxmox'] | length }} > /dev/null
|
||||
linstor volume-group create "{{ item.rg_name }}" > /dev/null
|
||||
echo "changed"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user