---
# file: roles/redis/tasks/main.yml

- name: "install redis-server"
  ansible.builtin.apt:
    name:
      - redis-server
  tags: redis

- name: "add include ansible.conf line in /etc/redis/redis.conf"
  ansible.builtin.lineinfile:
    path: /etc/redis/redis.conf
    line: "include /etc/redis/ansible.conf"
  when: redis_config is defined
  tags: redis

- name: "add include aclfile line in /etc/redis/redis.conf"
  ansible.builtin.lineinfile:
    path: /etc/redis/redis.conf
    line: "aclfile /etc/redis/users.acl"
  tags: redis

- name: "/etc/redis/ansible.conf"
  ansible.builtin.template:
    src: redis.conf.j2
    dest: /etc/redis/ansible.conf
    mode: "0640"
    owner: redis
    group: redis
    backup: true
  when: redis_config is defined
  notify: restart redis
  tags: redis

- name: "handle users password generation"
  ansible.builtin.include_tasks: users.yml
  loop: "{{ [{'name': 'default', 'password': redis_default_user_password }] + redis_acls | default([]) }}"
  loop_control:
    loop_var: user
  tags: redis

- name: "/etc/redis/users.acl"
  ansible.builtin.template:
    src: users.acl.j2
    dest: /etc/redis/users.acl
    mode: "0640"
    owner: redis
    group: redis
    backup: true
  notify: restart redis
  tags: redis

- name: "make sure redis is started and enable"
  ansible.builtin.systemd:
    name: redis-server
    state: started
    enabled: true
  tags: redis
