---
- name: Check nextcloud user id is over 2999
  ansible.builtin.assert:
    that:
      - nextcloud_user_uid >= 3000

- name: Create {{ nextcloud_user }} unix group

  ansible.builtin.group:
    name: "{{ nextcloud_user | mandatory }}"
    state: present
    gid: "{{ nextcloud_user_uid | mandatory }}"
  tags:
    - nextcloud

- name: Create {{ nextcloud_user | mandatory }} unix user
  ansible.builtin.user:
    name: "{{ nextcloud_user | mandatory }}"
    group: "{{ nextcloud_user | mandatory }}"
    home: "{{ nextcloud_home | mandatory }}"
    uid: "{{ nextcloud_user_uid | mandatory }}"
    shell: '/bin/bash'
    create_home: True
    state: present
#    mode: "0755"
  tags:
  - nextcloud

- name: Set mode for home directory
  ansible.builtin.file:
    path: "{{ nextcloud_home }}"
    mode: "0701"

- name: Create top-level directories
  ansible.builtin.file:
    dest: "{{ item }}"
    state: directory
    mode: "0700"
    owner: "{{ nextcloud_user }}"
    group: "{{ nextcloud_user }}"
  loop:
    - "{{ nextcloud_home }}/log"
    - "{{ nextcloud_home }}/tmp"
    - "{{ nextcloud_home }}/data"
  tags:
    - nextcloud

- name: Mount up Ceph volume by UUID
  ansible.posix.mount:
    path: "{{ nextcloud_data }}"
    src: "UUID={{ nextcloud_data_uuid }}"
    fstype: ext4
    opts: defaults,noexec,nosuid,nodev,relatime,lazytime
    state: mounted

- name: Set volume's root permissions and ownership
  ansible.builtin.file:
    path: "{{ nextcloud_data }}"
    owner: "{{ nextcloud_user }}"
    group: "{{ nextcloud_user }}"
    mode: "0700"
