- name: Deploy Application
  hosts: web
  remote_user: root
  pre_tasks:
    - name: "Assert project_name is provided and not empty"
      assert:
        that:
          - project_name is not undefined and project_name != ""
  tasks:

  ## Login 
  - name: Login to registry
    docker_login:
      registry_url: "{{ registry_url }}"
      username: "{{ registry_username }}"
      password: "{{ registry_password }}"
      reauthorize: true

# Deploy image
  - name: Create config directory
    file:
      path: "{{ config_dir }}" # /etc/app-x-y
      state: directory
      recurse: yes

  - name: Create .env file
    file:
      state: touch
      path: "{{ config_dir }}/.env"
      mode: '0600'

  - name: Create .env.local file
    file:
      state: touch
      path: "{{ config_dir }}/.env.local"
      mode: '0600'  

  - name: Execute docker-compose
    shell: cd "{{ config_dir }}" && docker-compose down
    ignore_errors: true

  - name: "Upload docker-compose template" # fills the tempalte with variables from ansile
    template:
      src: "templates/docker-compose-web.yml"
      dest: "{{ config_dir }}/docker-compose.yml"

  - name: Execute docker-compose
    shell: cd "{{ config_dir }}" && docker-compose up -d

  - name: Create log file
    file:
      path: "/var/log/{{ project_name }}"
      state: directory
      recurse: yes    
