---

- set_fact:
    redis_restart_handler_name: "{{ redis_restart_if_needed | bool | ternary('restart redis', 'restart redis (noop)') }}"

- name: Redis is installed.
  apt:
    name:
      - redis-server
      - redis-tools
    state: present
  tags:
    - redis
    - packages

- name: Redis Sentinel is installed.
  apt:
    name: "redis-sentinel"
    state: present
  tags:
    - redis
    - packages
  when: redis_sentinel_install | bool

- name: Get Redis version
  shell: "redis-server -v | grep -Eo '(v=\\S+)' | cut -d'=' -f 2 | grep -E '^([0-9]|\\.)+$'"
  changed_when: false
  check_mode: no
  register: _redis_installed_version
  tags:
    - redis

- set_fact:
    redis_installed_version: "{{ _redis_installed_version.stdout }}"
  check_mode: no
  tags:
    - redis

- name: set variables for default mode
  set_fact:
    redis_conf_dir: "{{ redis_conf_dir_prefix }}"
    redis_socket_dir: "{{ redis_socket_dir_prefix }}"
    redis_pid_dir: "{{ redis_pid_dir_prefix }}"
    redis_log_dir: "{{ redis_log_dir_prefix }}"
    redis_data_dir: "{{ redis_data_dir_prefix }}"
  when: redis_instance_name is not defined

- name: set variables for instance mode
  set_fact:
    redis_systemd_name: "redis-server@{{ redis_instance_name }}"
    redis_conf_dir: "{{ redis_conf_dir_prefix }}-{{ redis_instance_name }}"
    redis_socket_dir: "{{ redis_socket_dir_prefix }}-{{ redis_instance_name }}"
    redis_pid_dir: "{{ redis_pid_dir_prefix }}-{{ redis_instance_name }}"
    redis_log_dir: "{{ redis_log_dir_prefix }}-{{ redis_instance_name }}"
    redis_data_dir: "{{ redis_data_dir_prefix }}-{{ redis_instance_name }}"
  when: redis_instance_name is defined

- name: configure Redis for default mode
  include: default-server.yml
  when: redis_instance_name is not defined

- name: configure Redis for instance mode
  include: instance-server.yml
  when: redis_instance_name is defined

- name: Is Munin installed
  stat:
    path: /etc/munin/plugins
  register: _munin_installed
  tags:
    - redis
    - munin

- name: configure Munin for default mode
  include: default-munin.yml
  when:
    - _munin_installed.stat.exists
    - _munin_installed.stat.isdir
    - redis_instance_name is undefined
  tags:
    - redis
    - munin

- name: configure Munin for instance mode
  include: instance-munin.yml
  when:
    - _munin_installed.stat.exists
    - _munin_installed.stat.isdir
    - redis_instance_name is defined
  tags:
    - redis
    - munin

- name: Is log2mail installed
  stat:
    path: /etc/log2mail/config
  register: _log2mail_installed
  tags:
    - redis
    - log2mail

- name: configure log2mail for default mode
  include: default-log2mail.yml
  when:
    - _log2mail_installed.stat.exists
    - _log2mail_installed.stat.isdir
    - redis_instance_name is undefined
  tags:
    - redis
    - log2mail

- name: configure log2mail for instance mode
  include: instance-log2mail.yml
  when:
    - _log2mail_installed.stat.exists
    - _log2mail_installed.stat.isdir
    - redis_instance_name is defined
  tags:
    - redis
    - log2mail

- name: is NRPE present ?
  stat:
    path: /etc/nagios/nrpe.d/evolix.cfg
  check_mode: no
  register: nrpe_evolix_config
  tags:
    - redis
    - nrpe

- include: nrpe.yml
  when: nrpe_evolix_config.stat.exists
  tags:
    - redis
    - nrpe

- name: Force restart redis
  command: /bin/true
  notify: restart redis
  when: redis_restart_force | bool
