---
# CentOS 8 Specific tasks

- name: Enable  EPEL
  dnf:
    name: epel-release
    state: present

- name: Enable PowerTools
  lineinfile:
    path: /etc/yum.repos.d/CentOS-PowerTools.repo
    regexp: "^enabled=0"
    line: "enabled=1"
  notify:
    - dnf_update

- name: Install Dev Tools
  dnf:
    name: "@Development tools"
    state: present

- name: Install needed Packages
  dnf:
    name: "{{ item }}"
    state: present
  loop:
    - cmake
    - glib2-devel
    - zlib-devel
    - gnutls-devel
    - libuuid-devel
    - libssh-devel
    - libxml2-devel
    - libgcrypt-devel
    - openldap-devel
    - popt-devel
    - redis
    - libical-devel
    - openssl-devel
    - hiredis-devel
    - radcli-devel
    - gpgme-devel
    - libksba-devel
    - doxygen
    - libpcap-devel
    - nodejs
    - python3-polib
    - libmicrohttpd-devel
    - gnutls-utils
    - python3-devel
    - libpq-devel
    - texinfo
    - xmltoman
    - nmap
    - sshpass
    - socat
    - mingw32-gcc
    - ncurses-devel
    - postgresql-server
    - postgresql-contrib
    - postgresql-server-devel
    - gcc
    - libglib2.0-dev
    - libgnutls28-dev
    - libpq-dev
    - pkg-config
    - libical-dev
    - xsltproc

- name: Setup Postgres 10.14 (Current from EPEL)
  block:
    - name: Initialize PGSQL
      command: /usr/bin/postgresql-setup initdb
      ignore_errors: true

    - name: Enable and Start DB
      systemd:
        name: postgresql
        state: started
        enabled: true

    - name: Create GVM Database
      postgresql_db:
        name: "{{ db_name }}"
      become: true
      become_user: postgres
      no_log: true

    - name: Create GVM User
      postgresql_user:
        db: "{{ db_name }}"
        name: "{{ gvmuser }}"
        password: "{{ gvmuserpass }}"
        encrypted: yes
        priv: ALL
        state: present
      become: true
      become_user: postgres
      no_log: true

    - name: Change GVM DB Owner
      postgresql_owner:
        db: "{{ db_name }}"
        new_owner: "{{ gvmuser }}"
        obj_name: "{{ db_name }}"
        obj_type: database
      become: true
      become_user: postgres

    - name: Reduce Mattermost DB Privleges
      postgresql_user:
        name: "{{ gvmuser }}"
        role_attr_flags: NOCREATEDB
      become: true
      become_user: postgres

    - name: Add Extensions
      postgresql_ext:
        name: "{{ item }}"
        db: "{{ db_name }}"
      loop:
        - "uuid-ossp"
        - "pgcrypto"
      notify:
        - Restart PGSQL

- name: Add GVM Library to Systemwide
  command: echo “/opt/gvm/lib” > /etc/ld.so.conf.d/gvm.conf

- name: Run ldconfig
  command: ldconfig

- name: Create local GVM User
  user:
    name: gvm
    comment: “GVM (OpenVAS) User”
    home: /opt/gvm

- name: Make GVM Directory
  file:
    path: /opt/gvm
    state: directory

- name: Make Src Dir
  file:
    path: /opt/gvm/srv
    state: directory

- name: Change Owner of GVM Directory
  file:
    path: /opt/gvm
    recurse: true
    owner: gvm
    group: gvm

- name: Modify System Path
  template:
    src: profile
    dest: /etc/profile

- name: Clone GVMd
  git:
    repo: https://github.com/greenbone/gvmd.git
    clone: true
    dest: /opt/gvm
