---

- name: Housekeep pervious temporary directory
  ansible.builtin.file:
    path: "{{ pathTemp }}"
    state: absent

- name: Create a temporary directory on remote nodes
  ansible.builtin.file:
    path: "{{ pathTemp }}"
    state: directory
    owner: root
    group: root
    mode: '0755'

# Deploy stage -------------------------------------------------------------
# -- Target: The first node only.
# -- Objective: Stack startup.

- block:
  - name: Create a directory on remote host if not exists for docker-compose file
    ansible.builtin.file:
      path: "{{ pathCompose }}"
      state: directory
      owner: root
      group: root
      mode: '0755'
      
  - name: Check compose file exists
    ansible.builtin.stat:
      path: "{{ pathCompose }}/{{ fileCompose }}"
    ignore_errors: True
    register: chk_composeFile
       
  - name: Deploy the docker compose file
    ansible.builtin.template:
      src: "{{ role_path }}/files/{{ templateCompose }}"
      dest: "{{ pathCompose }}/{{ fileCompose }}"
      owner: root
      group: root
      mode: "0755"
      force: yes
      backup: yes
    when: updateComposeTpl | bool or not chk_composeFile.stat.exists
  
  - name: Docker login to ECV private repository if not pull from local registry
    community.docker.docker_login:
      registry: "{{ imagePullDomain }}"
      username: "{{ registryUser }}"
      password: "{{ registryPass }}"
      state: present
    when: not localRegistry | bool
  
  - name: Prune docker containers
    community.docker.docker_prune:
      containers: yes

  - name: Deploy the Redis-HA service
    community.docker.docker_stack:
      name: "{{ stackName }}"
      compose:
        - "{{ pathCompose }}/{{ fileCompose }}"
      state: present
      with_registry_auth : yes

