---
  - name: Configure the self-hosted server
    hosts: appsmith
    any_errors_fatal: true
    vars:
        analytics_webhook_uri: https://hook.integromat.com/dkwb6i52am93pi30ojeboktvj32iw0fa
        ipify_url: https://api64.ipify.org

    tasks:
        - name: Deploy appsmith on host
          block:
            - name: Get OS
              shell: |
                cat /etc/*-release | awk -F= '$1 == "NAME" { gsub(/"/, ""); print $2; exit }' | tr '[:upper:]' '[:lower:]'
              register: os

            - name: Get app installation id
              uri:
                url: "{{ ipify_url }}"
                return_content: yes
              register: app_installation_id

            - name: Push installation started event to integromat
              uri:
                url: "{{ analytics_webhook_uri }}"
                method: POST
                body: "{\"userId\":\"{{ app_installation_id.content }}\",\"event\":\"Installation Started\",\"data\":{\"os\":\"{{ os.stdout }}\", \"platform\": \"ansible\"}}"
                body_format: json
                return_content: yes

            - name: Setup dependencies
              import_role:
                name: base

            - name: Generate config template
              import_role:
                name: generate_template

            - name: Run App
              import_role:
                name: start_app

            - name: Config SSL
              import_role:
                name: domain_ssl
            - name: Push installation success event to integromat
              uri:
                url: "{{ analytics_webhook_uri }}"
                method: POST
                body: "{\"userId\":\"{{ app_installation_id.content }}\",\"event\":\"Installation Success\",\"data\":{\"os\":\"{{ os.stdout }}\", \"platform\": \"ansible\"}}"
                body_format: json
                return_content: yes
          rescue:
            - name: Push installation failed event to integromat
              uri:
                url: "{{ analytics_webhook_uri }}"
                method: POST
                body: "{\"userId\":\"{{ app_installation_id.content }}\",\"event\":\"Installation Support\",\"data\":{\"os\":\"{{ os.stdout }}\", \"platform\": \"ansible\", \"email\": \"{{ user_email }}\"}}"
                body_format: json
                return_content: yes

