- name: "Create application service group"
  ansible.builtin.group:
    name: "{{ service_group_name }}"
    state: present
    gid: "{{ service_group_id }}"

- name: "Create service user"
  user:
    name: "{{ service_user_name }}" 
    password: "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=16') | string | password_hash('sha512') }}"
    uid: "{{ service_user_id }}"
    update_password: on_create
    shell: /bin/zsh
    comment: Service Account
    group: "{{ service_group_name }}"
    groups: docker

- name: "Create service directory"
  ansible.builtin.file:
    path: "{{ service_directory }}"
    state: directory
    mode: '0770'
    owner: "{{ service_user_name }}"
    group: "{{ service_group_name }}"

- block:
    - name: "Create volume directories"
      ansible.builtin.file:
        path: "{{ service_directory }}/data/{{ item }}"
        state: directory
        mode: '0770'
        owner: "{{ service_user_name }}"
        group: "{{ service_group_name }}"
      loop: '{{ service_volume_dirs }}'

    - name: Copy docker compose config to target
      template:
        src: "../templates/{{ service_name }}/docker-compose.yml.j2"
        dest: "{{ service_directory }}/docker-compose.yml"
        mode: '0644'
    
    - name: Copy .env to target
      tags: always
      template:
        src: "../templates/{{ service_name }}/.env.j2"
        dest: "{{ service_directory }}/.env"
        mode: '0644'   
  become: yes
  become_user: "{{ service_user_name }}"    