---
- name: Ensuring config directories exist
  become: true
  file:
    path: "{{ node_config_directory }}/{{ item.key }}"
    state: "directory"
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
    mode: "0770"
  when:
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ hacluster_services }}"

- name: Copying over config.json files for services
  become: true
  template:
    src: "{{ item.key }}.json.j2"
    dest: "{{ node_config_directory }}/{{ item.key }}/config.json"
    mode: "0660"
  register: config_jsons
  when:
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ hacluster_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Copying over corosync.conf into hacluster-corosync
  vars:
    service: "{{ hacluster_services['hacluster-corosync'] }}"
  template:
    src: "{{ item }}"
    dest: "{{ node_config_directory }}/hacluster-corosync/corosync.conf"
    mode: "0660"
  become: true
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  with_first_found:
    - "{{ node_custom_config }}/hacluster-corosync/{{ inventory_hostname }}/corosync.conf"
    - "{{ node_custom_config }}/hacluster-corosync/corosync.conf"
    - "hacluster_corosync.conf.j2"
  notify:
    - Restart hacluster-corosync container

- name: Copying over Corosync authkey file
  vars:
    service: "{{ hacluster_services['hacluster-corosync'] }}"
  copy:
    src: "{{ item }}"
    dest: "{{ node_config_directory }}/hacluster-corosync/authkey"
    mode: "0600"
  become: true
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  with_first_found:
    - "{{ node_custom_config }}/hacluster-corosync/{{ inventory_hostname }}/authkey"
    - "{{ node_custom_config }}/hacluster-corosync/authkey"
  notify:
    - Restart hacluster-corosync container

- name: Copying over Pacemaker authkey file
  vars:
    service: "{{ hacluster_services['hacluster-pacemaker'] }}"
  copy:
    src: "{{ item }}"
    dest: "{{ node_config_directory }}//hacluster-pacemaker/authkey"
    mode: "0600"
  become: true
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  with_first_found:
    - "{{ node_custom_config }}/hacluster-pacemaker/{{ inventory_hostname }}/authkey"
    - "{{ node_custom_config }}/hacluster-pacemaker/authkey"
  notify:
    - Restart hacluster-pacemaker container

- name: Copying over Pacemaker authkey file into hacluster-pacemaker-remote
  vars:
    service: "{{ hacluster_services['hacluster-pacemaker-remote'] }}"
  copy:
    src: "{{ item }}"
    dest: "{{ node_config_directory }}/hacluster-pacemaker-remote/authkey"
    mode: "0600"
  become: true
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  with_first_found:
    - "{{ node_custom_config }}/hacluster-pacemaker/{{ inventory_hostname }}/authkey"
    - "{{ node_custom_config }}/hacluster-pacemaker/authkey"
  notify:
    - Restart hacluster-pacemaker-remote container
