---

- name: Install perl lib-redis (needed by check_redis)
  apt:
    name: libredis-perl
    state: present
  tags:
    - redis
    - nrpe

- name: install check_redis on Jessie
  copy:
    src: check_redis.pl
    dest: /usr/local/lib/nagios/plugins/check_redis
    force: yes
    mode: "0755"
    owner: root
    group: root
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('9', '<')
  tags:
    - redis
    - nrpe

- name: set the path of check_redis on Jessie
  set_fact:
    redis_check_redis_path: /usr/local/lib/nagios/plugins/check_redis
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('9', '<')
  tags:
    - redis
    - nrpe

- name: set the path of check_redis on Stretch and later
  set_fact:
    redis_check_redis_path: /usr/lib/nagios/plugins/check_redis
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('9', '>=')
  tags:
    - redis
    - nrpe

- name: sudo without password for nagios
  lineinfile:
    dest: /etc/sudoers.d/evolinux
    regexp: 'check_redis$'
    line: 'nagios          ALL = NOPASSWD: {{ redis_check_redis_path }}'
    insertafter: '^nagios'
    validate: "visudo -cf %s"
    create: yes
  when: redis_instance_name is defined
  tags:
    - redis
    - nrpe

- name: Use check_redis for NRPE
  replace:
    dest: /etc/nagios/nrpe.d/evolix.cfg
    regexp: '^command\[check_redis\]=.+'
    replace: 'command[check_redis]=sudo {{ redis_check_redis_path }} -H {{ redis_bind_interface }} -p {{ redis_port }}'
  when: redis_instance_name is undefined
  notify: restart nagios-nrpe-server
  tags:
    - redis
    - nrpe

- name: sudo without password for nagios
  lineinfile:
    dest: /etc/sudoers.d/evolinux
    regexp: 'check_redis$'
    line: 'nagios          ALL = NOPASSWD: {{ redis_check_redis_path }}'
    insertafter: '^nagios'
    validate: "visudo -cf %s"
  when: redis_instance_name is undefined
  tags:
    - redis
    - nrpe

- name: install check_redis_instances
  copy:
    src: check_redis_instances.sh
    dest: /usr/local/lib/nagios/plugins/check_redis_instances
    force: yes
    mode: "0755"
    owner: root
    group: root
  when: redis_instance_name is defined
  tags:
    - redis
    - nrpe

- name: Use check_redis_instances for NRPE
  replace:
    dest: /etc/nagios/nrpe.d/evolix.cfg
    regexp: '^command\[check_redis\]=.+'
    replace: 'command[check_redis]=sudo /usr/local/lib/nagios/plugins/check_redis_instances'
  when: redis_instance_name is defined
  notify: restart nagios-nrpe-server
  tags:
    - redis
    - nrpe

- name: sudo without password for nagios
  lineinfile:
    dest: /etc/sudoers.d/evolinux
    regexp: 'check_redis_instances$'
    line: 'nagios          ALL = NOPASSWD: /usr/local/lib/nagios/plugins/check_redis_instances'
    insertafter: '^nagios'
    validate: "visudo -cf %s"
  when: redis_instance_name is defined
  tags:
    - redis
    - nrpe
