---
- name: check if ceph ansible is enabled
  command: "grep -Eq 'ceph-ansible.yaml|storage-environment.yaml' {{ overcloud_deploy_script }}"
  register: ceph_ansible
  ignore_errors: true

- name: check if ceph osd hieradata has already been removed
  stat:
    path: "{{ working_dir }}/pre_upgrade_ceph_osd_hieradata_env.yaml"
  register: ceph_osd_adjusted

- block:
    - name: register environment files
      shell: |
        grep '\-e\ \|\-\-environment-file' {{ overcloud_deploy_script }} | awk {'print $2'}  | grep -v '\$'
      register: envs

    - name: look for ceph osd extra config
      shell: |
        grep 'ceph::profile::params::osds' {{ item }}
      ignore_errors: true
      register: osd_env
      loop: "{{ envs.stdout_lines }}"

    - name: set ceph_env fact
      set_fact:
        ceph_env: "{{ item.item }}"
      when: item.stdout|length > 0
      loop: "{{ osd_env.results }}"

    - block:
        - name: make a copy of the initial environment file
          copy:
            src: "{{ ceph_env }}"
            dest: "{{ working_dir }}/pre_upgrade_ceph_osd_hieradata_env.yaml"
            remote_src: true

        - name: register environment file
          command: "cat {{ ceph_env }}"
          register: ceph_puppet

        - name: set ceph_osds fact
          vars:
            ceph_yaml: "{{ (ceph_puppet.stdout | from_yaml).parameter_defaults }}"
            ceph_osds_var: "{{ ceph_yaml.ExtraConfig['ceph::profile::params::osds']}}"
          set_fact:
            ceph_osds: "{{ (ceph_osds_var.keys()|list if ceph_osds_var | type_debug == 'dict' else ceph_osds_var) | default([]) }}"

        - name: set ceph_osds_pg fact
          vars:
            ceph_yaml: "{{ (ceph_puppet.stdout | from_yaml).parameter_defaults }}"
          set_fact:
            ceph_osds_pg: "{{ ceph_yaml.ExtraConfig['ceph::profile::params::osd_pool_default_pg_num'] }}"
          ignore_errors: true

        - name: set ceph_osds_non_collocated fact
          set_fact:
            ceph_osds_non_collocated: true
          when: "'journal' in ceph_puppet.stdout"

        - name: generate new environment file
          template:
            src: cephosd.yaml.j2
            dest: "{{ working_dir }}/ceph-ansible-env.yaml"

        - name: set extra_config fact
          set_fact:
            extra_config: "{{ (ceph_puppet.stdout | from_yaml).parameter_defaults.ExtraConfig }}"

        - name: remove unused ceph parameters.
          lineinfile:
            path: "{{ ceph_env }}"
            regexp: "{{ item }}"
            state: absent
          loop:
            - "ceph::profile::params::osd_pool_default_pg_num"
            - "ceph::profile::params::osd_pool_default_pgp_num"
            - "ceph::profile::params::osds"

        - name: remove journal from ceph_env
          lineinfile:
            path: "{{ ceph_env }}"
            regexp: "journal"
            state: absent

        - name: remove ceph_osd from ceph env.
          lineinfile:
            path: "{{ ceph_env }}"
            regexp: "{{ item }}"
            state: absent
          loop: "{{ ceph_osds }}"

        - name: register new environment file
          command: "cat {{ ceph_env }}"
          register: ceph_puppet

        - name: set extra_config from ceph puppet.
          set_fact:
            extra_config: "{{ (ceph_puppet.stdout | from_yaml).parameter_defaults.ExtraConfig }}"

        - name: replace empty extraconfig
          replace:
            path: "{{ ceph_env }}"
            regexp: 'ExtraConfig:'
            replace: 'ExtraConfig: {}'
          when:
            - extra_config | length | int == 0

      when: ceph_env is defined

  when: ceph_ansible is succeeded and not ceph_osd_adjusted.stat.exists
