From 1ab99a97a9dc6608ba85e8e2a4eb41a85c7e297e Mon Sep 17 00:00:00 2001
From: pullyvan <pullyvan.kris@gmail.com>
Date: Tue, 1 Feb 2022 13:19:45 +0100
Subject: [PATCH 1/2] fix crash on creating partition table GPT by adding a
 ignore_errors condition and double the command first to make the device nvme
 visible then to format it

---
 tasks/ubuntu2004.yml | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tasks/ubuntu2004.yml b/tasks/ubuntu2004.yml
index 9379da7..48c230a 100644
--- a/tasks/ubuntu2004.yml
+++ b/tasks/ubuntu2004.yml
@@ -36,6 +36,15 @@
     state: unmounted
   ignore_errors: yes
 
+- name: Make nvme visible on /dev/
+  community.general.parted:
+    device: "{{ dev }}"
+    number: 1
+    state: present
+    fs_type: "{{ fstype }}"
+    label: gpt
+  ignore_errors: yes
+
 - name: Create a new partition on disk
   community.general.parted:
     device: "{{ dev }}"
@@ -43,6 +52,7 @@
     state: present
     fs_type: "{{ fstype }}"
     label: gpt
+  ignore_errors: yes
 
 - name: Create a filesystem on device
   community.general.filesystem:
-- 
GitLab


From f457e1d97218238c6598eb367f18bc99f46c0c3d Mon Sep 17 00:00:00 2001
From: pullyvan <pullyvan.kris@gmail.com>
Date: Tue, 1 Feb 2022 16:59:47 +0100
Subject: [PATCH 2/2] fix device not found by adding a check if device exists
 and destroy and recreate it if it does

---
 defaults/main.yml    |  1 +
 tasks/ubuntu2004.yml | 30 +++++++++++++++++++-----------
 2 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/defaults/main.yml b/defaults/main.yml
index 22ca5fe..03830c4 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -5,3 +5,4 @@ dev: /dev/nvme0n1
 mount: /home
 opts: "rw"
 state: mounted
+devpart: nvme0n1p1
\ No newline at end of file
diff --git a/tasks/ubuntu2004.yml b/tasks/ubuntu2004.yml
index 48c230a..8031fce 100644
--- a/tasks/ubuntu2004.yml
+++ b/tasks/ubuntu2004.yml
@@ -22,28 +22,32 @@
 - name: "Check if {{ dev }} exists"
   stat:
     path: "{{ dev }}"
+  register: device_exist
 
-- name: Unmount a mounted volume
-  ansible.posix.mount:
-    path: "{{ mount }}"
-    state: unmounted
-  ignore_errors: yes
-
+- name: "Check if {{ devpart }} exists"
+  stat:
+    path: "{{ dev }}"
+  register: devpart_exist
 
-- name: Unmount a mounted folder
+- name: "Unmount a {{ mount }} mounted volume"
   ansible.posix.mount:
     path: "{{ mount }}"
     state: unmounted
   ignore_errors: yes
 
-- name: Make nvme visible on /dev/
+- name: Delete a first partition on disk
   community.general.parted:
     device: "{{ dev }}"
     number: 1
-    state: present
+    state: absent
     fs_type: "{{ fstype }}"
     label: gpt
-  ignore_errors: yes
+  when: devpart_exist.stat.islnk is defined
+
+- name: "Check if {{ devpart }} exists"
+  stat:
+    path: "{{ dev }}"
+  register: devpart2_exist
 
 - name: Create a new partition on disk
   community.general.parted:
@@ -52,13 +56,15 @@
     state: present
     fs_type: "{{ fstype }}"
     label: gpt
-  ignore_errors: yes
+  when: devpart2_exist.stat.islnk == False
+
 
 - name: Create a filesystem on device
   community.general.filesystem:
     fstype: "{{ fstype }}"
     dev: "{{ dev }}p1"
     force: yes
+  when: devpart2_exist.stat.islnk == False
 
 - name: Mount up device on folder in fstab
   mount:
@@ -66,6 +72,7 @@
     src: "{{ dev }}p1"
     fstype: "{{ fstype }}"
     state: present
+  when: devpart2_exist.stat.islnk == False
 
 - name: Remount a mounted volume
   ansible.posix.mount:
@@ -73,3 +80,4 @@
     src: "{{ dev }}p1"
     fstype: "{{ fstype }}"
     state: mounted
+  when: devpart2_exist.stat.islnk == False
-- 
GitLab