---
# tasks file

- name: Set main ceph host ip
  set_fact:
    ceph_main: "{{ hostvars[groups['ceph_servers'][0]]['ansible_host'] }}"

- name: Check ceph status
  stat:
    path: /var/log/ceph/rgw_start.log
  register: ceph_status

- name: Ending if ceph up and running
  meta: end_play
  when: ceph_status.stat.exists

- name: Include OS specific variables.
  include_vars: "{{ ansible_distribution }}.yml"

- name: Install OS specific setup
  include_tasks: "setup/{{ ansible_distribution }}.yml"

- name: Set the timezone to the {{ new_host_timezone }} one
  timezone: "name={{ new_host_timezone }}"

- name: Make sure ntp is started, and is enabled on restart.
  service:
    name: chrony
    state: started
    enabled: yes
    masked: no

- name: Bootstrap a new cluster
  shell: cephadm bootstrap --mon-ip {{ ansible_host }} --ssh-user {{ cephadm_ssh_user }} >> bootstrap.log
  args:
    chdir: /var/log/ceph
    creates: bootstrap.log
  run_once: true
  delegate_to: "{{ groups['ceph_servers'][0] }}"

- name: get the cephadm ssh pub key
  command: "ceph cephadm get-pub-key"
  changed_when: false
  run_once: true
  register: cephadm_get_pub_key
  delegate_to: "{{ groups['ceph_servers'][0] }}"

- name: allow ssh public key for {{ cephadm_ssh_user | default('root') }} account
  authorized_key:
    user: "{{ cephadm_ssh_user | default('root') }}"
    key: "{{ cephadm_get_pub_key.stdout }}"

- name: Restart chronyd
  service:
    name: chronyd
    state: restarted

- name: Pause to build ceph config based on cephadm bootstrap
  pause:
    minutes: 5

      #- name: Ending if ceph status is HEALTH_OK
      #  block:
      #    - name: Register ceph status
      #      shell: ceph status -f json-pretty
      #      register: ceph_status
      #      no_log: true
      #
      #    - name: Check ceph status
      #      meta: end_play
      #      when: (ceph_status.stdout | from_json).health.status == "HEALTH_OK"
      #  run_once: true
      #  delegate_to: "{{ groups['ceph_servers'][0] }}"

- name: Config the new cluster
  block:
    - name: set cephadm ssh user to {{ cephadm_ssh_user }}
      command: "ceph cephadm set-user {{ cephadm_ssh_user | default('root') }}"
      changed_when: false
    
    - name: add all ceph hosts
      shell: ceph orch host add {{ hostvars[groups['ceph_servers'][host_idx]]['inventory_hostname'] }} {{ hostvars[groups['ceph_servers'][host_idx]]['ansible_host'] }}
      when: host_idx != 0
      loop: "{{ groups['ceph_servers'] }}"
      loop_control:
        index_var: host_idx
        pause: 2
    
    - name: apply osd all available devices
      shell: ceph orch apply osd --all-available-devices
    
        #- name: Copy radosgw.yml file
        #  template:
        #    src: radosgw.yml.j2
        #    dest: /etc/ceph/radosgw.yml
        #    owner: root
        #    group: root
        #    mode: 0644

        #- name: Start a S3 Ceph Rados Gateway
        #  shell: ceph orch apply -i /etc/ceph/radosgw.yml  >> rgw_start.log
        #  args:
        #    chdir: /var/log/ceph
        #    creates: rgw_start.log

    - name: Start a S3 Ceph Rados Gateway
      shell: ceph orch apply rgw s3

    - name: Pause to build ceph config based on S3 Rados Gateway
      pause:
        minutes: 5
    
    - name: set rgw-credentials for dashboard
      shell: ceph dashboard set-rgw-credentials
    
    - name: create a radosgw user in order to access the object gateway service
      shell: radosgw-admin user create --uid={{ rgw_user }} --display-name={{ rgw_name }} --system
      register: user_rgw_json
    
    - name: check user rgw keys
      set_fact:
        access_key: "{{ (user_rgw_json.stdout | from_json) | json_query('keys[0].access_key') }}"
        secret_key: "{{ (user_rgw_json.stdout | from_json) | json_query('keys[0].secret_key') }}"
   
    - name: set user access_key in tmp
      shell: echo {{ access_key|string }} > /tmp/access_key
    
    - name: set user secret_key in tmp
      shell: echo {{ secret_key|string }} > /tmp/secret_key
    
    - name: set user access_key for dashboard
      shell: ceph dashboard set-rgw-api-access-key -i /tmp/access_key
    
    - name: set user secret_key for dashboard
      shell: ceph dashboard set-rgw-api-secret-key -i /tmp/secret_key

    - name: create a first bucket
      amazon.aws.s3_bucket:
        name: "{{ rgw_first_bucket }}"
        endpoint_url: "http://{{ ceph_main }}:{{ rgw_frontend_port }}"
        access_key: "{{ access_key|string }}"
        secret_key: "{{ secret_key|string }}"
        ceph: true
        validate_certs: false
      ignore_errors: yes
  run_once: true
  delegate_to: "{{ groups['ceph_servers'][0] }}"
