---
- name: Set redis_list_servers variable
  block:
    - name: Try group web_servers
      set_fact:
        redis_list_servers: "{{ groups['web_servers'] | list }}"
  rescue:
    - name: Try group test_servers
      set_fact:
        redis_list_servers: "{{ groups['test_servers'] | list }}"

# include os specific tasks
- include_tasks: "setup/{{ ansible_distribution }}.yml"

- name: Ensure Redis configuration dir exists.
  file:
    path: "{{ redis_conf_path | dirname }}"
    state: directory
    mode: 0755

      #- name: Get the IP adress
      #  set_fact:
      #    curip: "{{ ansible_all_ipv4_addresses | select('search', private_network) }}"

- name: Ensure Redis is configured
  template:
    src: "{{ redis_daemon }}.conf.j2"
    dest: "{{ redis_conf_path }}"
    mode: "{{ redis_conf_mode }}"
  notify: start {{ redis_daemon }}

- name: Checking overcommit memory
  shell: 'cat /proc/sys/vm/overcommit_memory'
  register: overcommit_memory
  changed_when: false
  failed_when: false

- name: Overcommit memory
  sysctl:
    name: vm.overcommit_memory
    value: '1'
    state: present
  when: '"1" not in overcommit_memory.stdout'

- name: Increase max connections
  sysctl:
    name: net.core.somaxconn
    value: '511'
    state: present

- name: Disable THP
  shell: echo never > /sys/kernel/mm/transparent_hugepage/enabled

- name: Allow HAProxy to start
  sysctl:
    name: net.ipv4.ip_nonlocal_bind
    value: '1'
    sysctl_set: yes
    state: present
    reload: yes

- name: Ensure Redis is running and enabled on boot.
  service: "name={{ redis_daemon }} state=started enabled=yes"

- name: Ensure Redis Sentinel is configured
  template:
    src: redis_sentinel.conf.j2
    dest: "{{ redis_sentinel_conf_path }}"
    mode: "{{ redis_sentinel_conf_mode }}"
  notify: restart {{ redis_sentinel_daemon }}
  when: need_sentinel

- name: Ensure Redis Sentinel is running and enabled on boot.
  service: "name={{ redis_sentinel_daemon }} state=started enabled=yes"
  when: need_sentinel
