---
- name: Prepare
  hosts: all
  vars:
    nodejs_install_npm_user: "root"
    npm_config_unsafe_perm: "true"
    nodejs_npm_global_packages:
      - name: yarn
    service_url: "hedgedocs"
    nginx_remove_default_vhost: true
    nginx_extra_http_options: |
      map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
      }

    nginx_vhosts:
      - listen: "80 default_server"
        server_name: "_"
        access_log: "/var/log/nginx/{{ service_url }}_access.log"
        error_log: "/var/log/nginx/{{ service_url }}_error.log"
        state: "present"
        template: "{{ nginx_vhost_template }}"
        filename: "{{ service_url }}.http.conf"
        extra_parameters: |
          location / {
                    proxy_pass http://127.0.0.1:3003;
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-Forwarded-Proto $scheme;
                  }

                  location /socket.io/ {
                    proxy_pass http://127.0.0.1:3003;
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-Forwarded-Proto $scheme;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection $connection_upgrade;
                  }
  roles:
    - geerlingguy.nginx
    - geerlingguy.nodejs
    - geerlingguy.postgresql
  pre_tasks:
    - name: Update Debian Package List
      command: apt-get -y -o Acquire::GzipIndexes=false update
  tasks:
    - name: Install needed packages
      ansible.builtin.package:
        name:
          - git
          - curl
          - gnupg2
        state: present
    # We need to install Yarn via Raw because the default yarn package
    # present in the debian repos is out-of-date.
    - name: Install yarn
      raw: >
        echo "deb https://dl.yarnpkg.com/debian/ stable main"
        >> /etc/apt/sources.list.d/yarn.list &&
        curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg |
        sudo apt-key add - &&
        apt-get update
        && apt-get install -y --no-install-recommends yarn
      args:
        executable: /bin/bash
