---
- name: Check /tmp/cephfs path
  command: "docker exec -u 0 manila_share ls /tmp/cephfs/"
  register: check_cephfs
  changed_when: False
  failed_when: check_cephfs.rc != 0 and check_cephfs.stderr.find('No such file or directory') == -1
  run_once: True

- name: Create /tmp/cephfs path
  command: "docker exec -u 0 manila_share mkdir -p /tmp/cephfs"
  run_once: True
  when: check_cephfs.rc != 0

- name: Get cephfs addr
  set_fact: cephfs_addr={% for host in groups['ceph-mon'] %}{{ hostvars[host]['ansible_' + hostvars[host]['storage_interface']]['ipv4']['address'] }}{% if loop.last %}:6789:/{% else %},{% endif %}{% endfor %}
  run_once: True

- name: Pulling cephx keyring for manila
  command: docker exec ceph_mon ceph auth get-key client.manila
  register: manila_keyring
  delegate_to: "{{ groups['ceph-mon'][0] }}"
  changed_when: False
  run_once: True

- name: Umount cephfs
  command: "docker exec -u 0 manila_share umount /tmp/cephfs/"
  register: umount_cephfs
  changed_when: False
  failed_when: False
  run_once: True

- name: Mount cephfs
  command: "docker exec -u 0 manila_share mount -t ceph {{cephfs_addr}} /tmp/cephfs -o name=manila,secret={{ manila_keyring.stdout }}"
  register: mount_cephfs
  changed_when: False
  run_once: True

- name: Check volumes path
  command: "docker exec -u 0 manila_share ls /tmp/cephfs/volumes"
  register: check_volume
  changed_when: False
  failed_when: False
  run_once: True

- name: Create /tmp/cephfs/volumes path
  command: "docker exec -u 0 manila_share mkdir /tmp/cephfs/volumes"
  register: create_volume
  run_once: True
  when: check_volume.rc != 0

- name: Change the owner and group of /tmp/cephfs/volumes
  command: "docker exec -u 0 manila_share chown manila:manila /tmp/cephfs/volumes"
  register: chown_volume
  run_once: True
  when: check_volume.rc != 0 and create_volume.rc == 0

- name: Umount cephfs
  command: "docker exec -u 0 manila_share umount /tmp/cephfs"
  changed_when: False
  register: umount_cephfs
  run_once: True
  when: mount_cephfs.rc == 0
