---
#
# Create Volumes for shares
#

- name: Initial Setup of LVM and volumes
  become: true
  # play safe, only execute dduring initial setup
  when: init_lvm | default('false') | bool
  block:

  # We do not want the fdisk part and the pvs/vgs creation handled by ansible
  # Attention:
  #   The disks and the pvs/vgs have to be setup for the playbook to work
  #   otherwise it will fail (intentionally)!

  - name: Check if volume groups exist
    block:

      - name: Check existing volume groups
        ansible.builtin.command: vgs --noheadings --options vg_name "{{ item }}"
        register: vg_list_result
        ignore_errors: true
        changed_when: false
        loop: "{{ volume_groups_to_check }}"

      - name: Fail if volume group does not exist
        ansible.builtin.fail:
          msg: "Volume Group {{ item.item }} does not exist."
        loop: "{{ vg_list_result.results }}"
        when: item.rc != 0

#  - name: Remove the backup lvm from the volume group system
#    tags:
#      - create-file-volumes
#      - remove-system-backup
#    block:
#
#      - name: Check if a backup lvm exists on the volume group system
#        ansible.builtin.command:
#          cmd: lvs --noheadings -o lv_name --separator ' ' system
#        register: lv_system_list
#        changed_when: false
#
#      - name: Delete system-backup in favour of backup-backup
#        community.general.lvol:
#          vg: system
#          lv: backup
#          state: absent
#          force: true
#        when: "'backup' in lv_system_list.stdout"
#    when: vg_name_backup != 'system'

  - name: create share lvs
    tags: create-file-volumes
    community.general.lvol:
      vg: "{{ item.vgname }}"
      lv: "{{ item.lvname }}"
      size: "{{ item.size }}"
      shrink: false
    loop:
      - { lvname: 'public', size: "{{ lv_public_size }}", vgname: "{{ vg_name_data }}" }
      - { lvname: 'share', size: "{{ lv_share_size }}", vgname: "{{ vg_name_data }}" }
      - { lvname: 'team', size: "{{ lv_team_size }}", vgname: "{{ vg_name_data }}" }
      - { lvname: 'qcs', size: "{{ lv_qcs_size }}", vgname: "{{ vg_name_data }}" }
      - { lvname: 'backup', size: "{{ lv_backup_size }}", vgname: "{{ vg_name_backup }}" }
      - { lvname: 'mprimages', size: "{{ lv_mprimages_size }}", vgname: "{{ vg_name_mprimages }}" }

  - name: format shares with ext4 fs
    tags: create-file-volumes
    community.general.filesystem:
      fstype: ext4
      dev: "/dev/mapper/{{ item.vgname }}-{{ item.lvname }}"
    loop:
      - { lvname: 'public', vgname: "{{ vg_name_data }}" }
      - { lvname: 'share', vgname: "{{ vg_name_data }}" }
      - { lvname: 'team', vgname: "{{ vg_name_data }}" }
      - { lvname: 'qcs', vgname: "{{ vg_name_data }}" }
      - { lvname: 'backup', vgname: "{{ vg_name_backup }}" }
      - { lvname: 'mprimages', vgname: "{{ vg_name_mprimages }}" }

  #
  # Create share mountpoints
  #
  - name: Create mountpoints public, share, team and qcs
    ansible.builtin.file:
      path: "/vol/shares/{{ item }}"
      state: directory
    loop:
      - public
      - share
      - team
      - qcs

  - name: Create mountpoint for mprimages
    ansible.builtin.file:
      path: "/vol/shares/team/IPC-Images"
      state: directory

  #
  # Mount shares
  #
  - name: Mount shares public, share, team and qcs
    ansible.posix.mount:
      path: "/vol/shares/{{ item }}"
      src: "/dev/mapper/{{ vg_name_data }}-{{ item }}"
      fstype: ext4
      opts: defaults
      passno: 2
      state: mounted
    loop:
      - public
      - share
      - team
      - qcs

  - name: Mount share mprimages
    ansible.posix.mount:
      path: "/vol/shares/team/IPC-Images"
      src: "/dev/mapper/{{ vg_name_mprimages }}-mprimages"
      fstype: ext4
      opts: defaults
      passno: 2
      state: mounted

  - name: Mount backup-backup device
    ansible.posix.mount:
      path: "/vol/backup"
      src: "/dev/mapper/{{ vg_name_backup }}-backup"
      fstype: ext4
      opts: defaults
      passno: 2
      state: mounted

  #
  # Create Windows share
  #
  - name: Create Windows-Shared
    ansible.builtin.file:
      path: /vol/shares/team/Windows-Shared
      state: directory
      mode: '0760'

  ## TODO: use permissons 's'
  ## This cannot be done until the user is availible from ldap.
  ## Anyway: This folder will be copied from previous system.
  #- name: Create Win-PC-*
  #  ansible.builtin.file:
  #    path: "/vol/shares/team/Windows-Shared/{{ item }}"
  #    state: directory
  #    mode: '0760'
  #  with_items:
  #    - Win-PC-Isabel
  #    - Win-PC-Karina
  #  when: init_lvm | default('false') | bool
