---

### tags:
###   update_certs
###   update_config
###   update_deployment

- name: 'Delete {{ current_base_path }}/{{ current_destination }}'
  file:
    state: absent
    path: "{{ current_base_path }}/{{ current_destination }}"
  when: cleanup_destination is defined and cleanup_destination == "true"
  tags:
    - update_certs
    - update_config
    - update_deployment

- name: 'Ensures {{ current_base_path }}/{{ current_destination }} directory exists'
  file:
    state: directory
    path: '{{ current_base_path }}/{{ current_destination }}'
    mode: '0755'
  tags:
    - update_certs
    - update_config
    - update_deployment

- name: 'Ensure directory structure for {{ current_config }} exists'
  file:
    path: "{{ current_base_path }}/{{ current_destination }}/{{ item.path }}"
    state: directory
    owner: "{{ current_owner }}"
    group: "{{ current_group }}"
    mode: 0755
  with_filetree: "templates/{{ current_config }}"
  when: item.state == "directory"
  tags:
    - update_certs
    - update_config
    - update_deployment

- name: Ensure config template files are populated from templates/{{ current_config }}
  template:
    src: "{{ item.src }}"
    dest: "{{ current_base_path }}/{{ current_destination }}/{{ item.path | regex_replace('\\.j2$', '') }}"
    owner: "{{ current_owner }}"
    group: "{{ current_group }}"
    mode: 0644
  with_filetree: "templates/{{ current_config }}"
  when: item.state == 'file' and item.src is match('.*\.j2$')
  tags:
    - update_certs
    - update_config
    - update_deployment

- name: Ensure config files are populated from from templates/{{ current_config }}
  copy:
    src: "{{ item.src }}"
    dest: "{{ current_base_path }}/{{ current_destination }}/{{ item.path }}"
    owner: "{{ current_owner }}"
    group: "{{ current_group }}"
    mode: 0644
  with_filetree: "templates/{{ current_config }}"
  when: item.state == 'file' and item.src is not match('.*\.j2$')
  tags:
    - update_certs
    - update_config
    - update_deployment
