diff --git a/defaults/main.yaml b/defaults/main.yaml index 0cc823f..99d8505 100644 --- a/defaults/main.yaml +++ b/defaults/main.yaml @@ -32,5 +32,9 @@ standard_user: namespace: dev-admin role: admin -delete_openshift_storage: true +delete_openshift_storage: false +disk_file_path: /mnt/microshift-storage-file +disk_file_size: 20G +vg_name: rhel + overwrite_container_policy: false diff --git a/tasks/main.yaml b/tasks/main.yaml index c9c16a2..f17a1fd 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -47,6 +47,5 @@ ansible.builtin.include_tasks: standard-user.yaml when: standard_user -- name: Delete openshift storage +- name: Create lvm or delete openshift storage ansible.builtin.include_tasks: openshift-storage.yaml - when: delete_openshift_storage diff --git a/tasks/openshift-storage.yaml b/tasks/openshift-storage.yaml index b59b965..d30cb6d 100644 --- a/tasks/openshift-storage.yaml +++ b/tasks/openshift-storage.yaml @@ -1,15 +1,69 @@ --- -# NOTE: The Openshift storage uses topolvm, that requires additional volume, -# which is not necessary for us, due we mostly use that playbook for CI. -- name: Check if openshift-storage namespace exists - ansible.builtin.command: oc get namespace openshift-storage - register: _openshift_storage_ns - failed_when: _openshift_storage_ns.rc not in [0, 1] - -- name: Delete openshift storage namespace - ansible.builtin.command: oc delete namespace openshift-storage - when: _openshift_storage_ns.rc == 0 - -- name: Delete topolvm-provisioner storageclass - ansible.builtin.command: oc delete storageclass topolvm-provisioner - when: _openshift_storage_ns.rc == 0 +- name: Delete openshift-storage if needed + when: delete_openshift_storage + block: + # NOTE: The Openshift storage uses topolvm, that requires additional volume, + # which is not necessary for us, due we mostly use that playbook for CI. + - name: Check if openshift-storage namespace exists + ansible.builtin.command: oc get namespace openshift-storage + register: _openshift_storage_ns + failed_when: _openshift_storage_ns.rc not in [0, 1] + + - name: Delete openshift storage namespace + ansible.builtin.command: oc delete namespace openshift-storage + when: _openshift_storage_ns.rc == 0 + + - name: Delete topolvm-provisioner storageclass + ansible.builtin.command: oc delete storageclass topolvm-provisioner + when: _openshift_storage_ns.rc == 0 + +- name: Create LVM on loop device to deploy openshift storage with topolvm + when: not delete_openshift_storage + block: + - name: Check if file already exists + become: true + stat: + path: "{{ disk_file_path }}" + register: _disk_file + + - name: Create loop disk + when: not _disk_file.stat.exists + block: + - name: Create empty file + become: true + community.general.filesize: + path: "{{ disk_file_path }}" + size: "{{ disk_file_size }}" + + - name: Make partition on empty file + become: true + community.general.parted: + device: "{{ disk_file_path }}" + number: 1 + state: present + flags: ["lvm"] + + - name: Get first available loop device + become: true + ansible.builtin.command: losetup -f + register: loop_device + + - name: Create loop device service + become: true + ansible.builtin.template: + src: microshift-loop-device.service.j2 + dest: /usr/lib/systemd/system/microshift-loop-device.service + + - name: Enable loop device service + become: true + ansible.builtin.systemd: + name: microshift-loop-device + state: started + enabled: true + daemon_reload: true + + - name: Create volume group + become: true + community.general.lvg: + vg: "{{ vg_name }}" + pvs: "{{ loop_device.stdout }}" diff --git a/tasks/pre.yaml b/tasks/pre.yaml index 24856e1..1aa2754 100644 --- a/tasks/pre.yaml +++ b/tasks/pre.yaml @@ -12,6 +12,7 @@ - git - golang - python3-cryptography + - lvm2 state: present - name: Add host to the hosts file diff --git a/templates/microshift-loop-device.service.j2 b/templates/microshift-loop-device.service.j2 new file mode 100644 index 0000000..0941fcc --- /dev/null +++ b/templates/microshift-loop-device.service.j2 @@ -0,0 +1,16 @@ +[Unit] +Description=Setup loopback device +DefaultDependencies=no +Conflicts=umount.target +Before=local-fs.target umount.target microshift.service + +[Service] +ExecStart=/sbin/losetup {{ loop_device.stdout }} {{ disk_file_path }} +ExecStop=/sbin/losetup -d {{ loop_device.stdout }} +Type=oneshot +TimeoutSec=60 +RemainAfterExit=yes + +[Install] +WantedBy=local-fs-pre.target +Also=systemd-udevd.service