- name: Deploy Application
  hosts: all
  become: yes
  become_method: sudo

  vars_files:
    - vars/main.yml

  pre_tasks:
    - name: Get applist
      block:
        - name: Multiply apps execute on second time
          shell : |
            rm -f /tmp/port.txt
            
        - name: Get appname and dependencies, combine to a applist
          shell : |
            echo "{{appname}}" > applist
            cat ./roles/{{appname}}/meta/main.yml | sed -n '/dependencies/,/galaxy_info/{//!p}'|grep -|awk -F'- ' '{print $2}' >> applist
            cat applist
          register: applist
          delegate_to: 127.0.0.1

        - name: Check if nginx is necessary
          shell : |
            wget -N https://raw.githubusercontent.com/Websoft9/docker-library/main/apps/{{appname}}/.env
            cat .env
          register: env_content
          failed_when: False
          delegate_to: 127.0.0.1
   
        - name: Create credentials Folder for put get_ip.sh
          file:
            path: /credentials
            state: directory
            
        - name: Copy get_ip.sh
          copy: src=../scripts/get_ip.sh  dest=/credentials/get_ip.sh
       
  roles:
    - { role: role_common, tags: "role_common" }
    - { role: role_cloud, tags: "role_cloud" }
    - { role: "{{appname}}", tags: "{{appname}}" }
    - { role: role_nginx, tags: "role_nginx", when: env_content.stdout.find('APP_HTTP_PORT') != -1 }
    - { role: role_init, tags: "role_init" }
    - { role: role_preend, tags: "preend" }
    - { role: role_end, tags: "role_end" }
