---
- name: Ensuring cinder service ceph config subdirs exists
  vars:
    service: "{{ cinder_services[item] }}"
  file:
    path: "{{ node_config_directory }}/{{ item }}/ceph"
    state: "directory"
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
    mode: "0770"
  become: true
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  with_items:
    - "cinder-volume"
    - "cinder-backup"

- name: Copying over multiple ceph.conf for cinder services
  vars:
    services_need_config:
      - "cinder-volume"
      - "cinder-backup"
    service_name: "{{ item.0.key }}"
    service: "{{ item.0.value }}"
    cluster: "{{ item.1.cluster }}"
  merge_configs:
    sources:
      - "{{ node_custom_config }}/cinder/{{ cluster }}.conf"
      - "{{ node_custom_config }}/cinder/{{ service_name }}/{{ cluster }}.conf"
    dest: "{{ node_config_directory }}/{{ service_name }}/ceph/{{ cluster }}.conf"
    mode: "0660"
  become: true
  when:
    - service.enabled | bool
    - inventory_hostname in groups[service.group]
    - service_name in services_need_config
  with_nested:
    - "{{ cinder_services | dict2items }}"
    - "{{ cinder_ceph_backends }}"
  notify:
    - "Restart {{ service_name }} container"

- name: Copy over Ceph keyring files for cinder-volume
  vars:
    keyring: "{{ item.cluster }}.{{ ceph_cinder_keyring }}"
  template:
    src: "{{ node_custom_config }}/cinder/cinder-volume/{{ keyring }}"
    dest: "{{ node_config_directory }}/cinder-volume/ceph/{{ keyring }}"
    mode: "0660"
  become: true
  with_items: "{{ cinder_ceph_backends }}"
  when:
    - external_ceph_cephx_enabled | bool
    - inventory_hostname in groups['cinder-volume']
    - cinder_services['cinder-volume'].enabled | bool
  notify:
    - Restart cinder-volume container

- name: Copy over Ceph keyring files for cinder-backup
  template:
    src: "{{ node_custom_config }}/cinder/cinder-backup/{{ item }}"
    dest: "{{ node_config_directory }}/cinder-backup/ceph/{{ item }}"
    mode: "0660"
  become: true
  register: cinder_backup_ceph_keyring
  with_items:
    - "{{ cinder_backup_ceph_backend.cluster }}.{{ ceph_cinder_keyring }}"
    - "{{ cinder_backup_ceph_backend.cluster }}.{{ ceph_cinder_backup_keyring }}"
  when:
    - external_ceph_cephx_enabled | bool
    - inventory_hostname in groups['cinder-backup']
    - cinder_services['cinder-backup'].enabled | bool
  notify:
    - Restart cinder-backup container

- name: Ensuring config directory has correct owner and permission
  become: true
  file:
    path: "{{ node_config_directory }}/{{ item }}"
    recurse: yes
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
  when: inventory_hostname in groups[item]
  with_items:
    - "cinder-volume"
    - "cinder-backup"
