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

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

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

- name: Copying files for letsencrypt-lego
  vars:
    service: "{{ letsencrypt_services['letsencrypt-lego'] }}"
  template:
    src: "{{ item.src }}"
    dest: "{{ node_config_directory }}/letsencrypt-lego/{{ item.dest }}"
    mode: "0660"
  become: true
  with_items:
    - { src: "crontab.j2", dest: "crontab" }
    - { src: "id_rsa.j2", dest: "id_rsa" }
    - { src: "letsencrypt-lego-run.sh.j2", dest: "letsencrypt-lego-run.sh" }
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  notify:
    - Restart letsencrypt-lego container

- include_tasks: copy-certs.yml
  when:
    - kolla_copy_ca_into_containers | bool
