---
- name: Check for presence of Pritunl server installation indicator
  stat:
    path: /root/.pritunl_installed
  register: pritunl_install_indicator

- name: Run the init script for the Pritunl server (for CentOS 8)
  become: true
  shell:
    cmd: |
      sudo tee /etc/yum.repos.d/mongodb-org-5.0.repo << EOF
      [mongodb-org-5.0]
      name=MongoDB Repository
      baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/5.0/x86_64/
      gpgcheck=1
      enabled=1
      gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
      EOF

      sudo tee /etc/yum.repos.d/pritunl.repo << EOF
      [pritunl]
      name=Pritunl Repository
      baseurl=https://repo.pritunl.com/stable/yum/oraclelinux/8/
      gpgcheck=1
      enabled=1
      EOF

      sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
      gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 7568D9BB55FF9E5287D586017AE645C0CF8E292A
      gpg --armor --export 7568D9BB55FF9E5287D586017AE645C0CF8E292A > key.tmp; sudo rpm --import key.tmp; rm -f key.tmp
      sudo yum -y install pritunl mongodb-org
      sudo systemctl start mongod pritunl
      sudo systemctl enable mongod pritunl
  when: not pritunl_install_indicator.stat.exists
  args:
    executable: /bin/bash

- name: Add indicator that Pritunl was installed to /root/.pritunl_installed
  become: true
  copy:
    content: |
      {{ ansible_date_time.iso8601 }}
    dest: /root/.pritunl_installed