---

- name: ensure the frontend package is installed
  apt:
    name: zabbix-frontend-php
    state: present
  notify:
    - restart zabbix frontend
  tags:
    - zabbix

- name: ensure php5-pgsql is available
  apt:
    name: php5-pgsql
    state: present
  notify:
    - restart zabbix frontend
  tags:
    - zabbix

- name: ensure a user executing the app exists
  user:
    name: zabbix
    system: true
    home: /usr/share/zabbix
    shell: /usr/sbin/nologin
    createhome: false
    group: zabbix
    state: present
  notify:
    - restart zabbix frontend
  tags:
    - zabbix

- name: ensure the database server listens on the external interface
  replace:
    dest: /etc/postgresql/9.6/main/postgresql.conf
    regexp: "#listen_addresses = 'localhost'"
    replace: "listen_addresses = '*'"
  notify:
    - restart postgres
  delegate_to: "{{ zabbix_db_host }}"
  tags:
    - database
    - zabbix

- name: ensure the database server allows connections from the server
  lineinfile:
    dest: /etc/postgresql/9.6/main/pg_hba.conf
    # yamllint disable-line rule:line-length
    insertafter: "host    all             all             127.0.0.1/32            md5"
    # yamllint disable-line rule:line-length
    line: "host    {{ zabbix_db_name }}          {{ zabbix_db_user }}          {{ ansible_fqdn }} md5"
    state: present
  notify:
    - restart postgres
  delegate_to: "{{ zabbix_db_host }}"
  tags:
    - database
    - zabbix

- meta: flush_handlers
