kayobe/ansible/roles/wipe-disks/tasks/main.yml
Will Szumski faa5b33fef Fixes an issue with --wipe-disks
The awk expression that was in use prior to this change was fragile
and in some cases could incorrectly identify the unmounted block
devices. This change switches to parsing the json output which
should hopefully be more robust.

Change-Id: Ifa89e7307eb445b4f1708f0c6ac3409b3f96aafe
Story: 2010367
Task: 46578
2022-10-17 17:37:55 +01:00

59 lines
1.9 KiB
YAML

---
# Warning! These tasks can result in lost data. Take care when developing and
# using them.
# Initialisation tasks to be applied on first boot of a system to initalise
# disks. We search for block devices that are not currently mounted, then wipe
# any LVM or file system state from them.
- name: Ensure LVM2 is installed
package:
name: lvm2
state: present
cache_valid_time: "{{ apt_cache_valid_time if ansible_facts.os_family == 'Debian' else omit }}"
update_cache: "{{ True if ansible_facts.os_family == 'Debian' else omit }}"
become: True
- name: Gather blockdevice facts
blockdevice_info:
register: block_devices
changed_when: False
- name: Ensure that all unmounted block devices have LVM state removed
shell: |
set -e
if pvs /dev/{{ item }} >/dev/null 2>&1
then
echo "Found PV on /dev/{{ item }}"
vg=$(pvs --noheadings -o vg_name /dev/{{ item }})
if [[ -n $vg ]] && [[ $vg != " " ]]
then
echo "Found VG $vg on PV /dev/{{ item }}"
lvs --noheadings -o lv_name $vg | while read lv
do
if [[ -n $lv ]] && [[ $lv != " " ]]
then
echo "Found LV $lv on VG $vg. Removing"
lvremove -yf ${vg}/${lv}
fi
done
vgremove -f $vg
fi
pvremove -yff /dev/{{ item }}
fi
with_items: "{{ block_devices.unmounted }}"
become: True
- name: Ensure that all unmounted block devices have filesystems wiped
command: "wipefs -f /dev/{{ item }}"
with_items: "{{ block_devices.unmounted }}"
become: True
# The command can fail in some cases which are valid, so ignore the
# result.
failed_when: False
- name: Ensure that all unmounted block device headers are zeroed
command: "dd if=/dev/zero of=/dev/{{ item }} bs=1M count=100"
with_items: "{{ block_devices.unmounted }}"
become: True