---
- name: Install munin check dependencies
  apt:
    name: libswitch-perl
    state: present
  tags:
    - redis

- include_role:
    name: evolix/remount-usr
  tags:
    - redis

- name: Create plugin directory
  file:
    name: /usr/local/share/munin/
    state: directory
    mode: "0755"
  tags:
    - redis

- name: Create plugin directory
  file:
    name: /usr/local/share/munin/plugins/
    state: directory
    mode: "0755"
  tags:
    - redis

- name: Copy redis munin plugin
  copy:
    src: munin_redis
    dest: /usr/local/share/munin/plugins/redis_
    mode: "0755"
  notify: restart munin-node
  tags:
    - redis

- name: Enable redis munin plugin
  file:
    src: /usr/local/share/munin/plugins/redis_
    dest: "/etc/munin/plugins/redis_{{item}}"
    state: link
  loop:
    - connected_clients
    - key_ratio
    - keys_per_sec
    - per_sec
    - used_keys
    - used_memory
  notify: restart munin-node
  tags:
    - redis

- name: Count redis condif blocks in munin-node configuration
  command: grep -c "\[redis_"  /etc/munin/plugin-conf.d/munin-node
  register: munin_redis_blocs_in_config
  failed_when: False
  changed_when: False
  check_mode: no

- name: Add redis password for munin (if no more than 1 config block)
  ini_file:
    dest: /etc/munin/plugin-conf.d/munin-node
    section: 'redis_*'
    option: env.password
    value: '{{ redis_password }}'
  notify: restart munin-node
  when:
    - redis_password is not none
    - redis_password | length > 0
    - (munin_redis_blocs_in_config.stdout | int) <= 1
  tags: redis


- name: Warn if multiple instance in munin-plugins configuration
  debug:
    msg: "WARNING - It seems you have multiple redis sections in your munin-node configuration - Munin config NOT changed"
  when:
    - redis_password is not none
    - redis_password | length > 0
    - (munin_redis_blocs_in_config.stdout | int) > 1
