- name: setup directory symlink for using custom data directory/volume
  block:
    - name: Create a xfs filesystem on /dev/nvme1n1
      # First non-root device is always mapped to /dev/nvme1n1
      # See: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html
      community.general.filesystem:
        dev: /dev/nvme1n1
        fstype: xfs

    - name: ensure external data directory exists
      file:
        path: "{{ custom_data_directory }}"
        state: directory

    - name: Mount the EBS volume
      ansible.posix.mount:
        path: "{{ custom_data_directory }}"
        src: /dev/nvme1n1
        state: mounted
        fstype: xfs

    - name: set custom data directory permission
      file:
        path: "{{ custom_data_directory }}"
        state: directory
        recurse: yes
        owner: "{{ xray_user }}"
        group: "{{ xray_group }}"
        mode: "u=rwX,g=rwX,o=rwX"

    - name: remove var directory if exists
      file:
        path: "{{ xray_home }}/var"
        state: absent

    - name: symlink custom data directory to var
      file:
        src: "{{ custom_data_directory }}"
        path: "{{ xray_home }}/var"
        state: link
        owner: "{{ xray_user }}"
        group: "{{ xray_group }}"
  become: yes
  when: use_custom_data_directory and custom_data_directory is defined
