---

- hosts: all
  become: yes
  become_user: root
  become_method: sudo
  gather_facts: yes

  vars:
    redis_ports:
      - {"port": 19901, "start-hslot": 0, "end-hslot": 1365}
      - {"port": 19902, "start-hslot": 1366, "end-hslot": 2730}
      - {"port": 19903, "start-hslot": 2731, "end-hslot": 4096}
      - {"port": 19904, "start-hslot": 4097, "end-hslot": 5460}
      - {"port": 19905, "start-hslot": 5461, "end-hslot": 6826}
      - {"port": 19906, "start-hslot": 6827, "end-hslot": 8191}
      - {"port": 19907, "start-hslot": 8192, "end-hslot": 9557}
      - {"port": 19908, "start-hslot": 9558, "end-hslot": 10921}
      - {"port": 19909, "start-hslot": 10922, "end-hslot": 12287}
      - {"port": 19910, "start-hslot": 12288, "end-hslot": 13651}
      - {"port": 19911, "start-hslot": 13652, "end-hslot": 15017}
      - {"port": 19912, "start-hslot": 15018, "end-hslot": 16383}

  handlers:
    - name: reload_redis_services
      command: systemctl daemon-reload
    - name: restart_redis_services
      systemd:
        name: "redis-{{ item['port'] }}"
        state: restarted
      with_items: "{{ redis_ports }}"

  pre_tasks:

    - name: Get the Redisraft package
      copy:
        src: redisraft_0.0.1_x86_64.rpm
        dest: /tmp/redisraft_0.0.1_x86_64.rpm

    - name: Install the Redisraft package
      yum:
        name: /tmp/redisraft_0.0.1_x86_64.rpm
        state: present

  tasks:

    - name: Redis Systemd
      template:
        src: systemd_redis.j2
        dest: /lib/systemd/system/redis-{{ item['port'] }}.service
        owner: root
        group: root
        mode: 0644
      with_items: "{{ redis_ports }}"
      notify:
        - reload_redis_services
        - restart_redis_services

