---

- name: Verify Redis port
  ansible.builtin.assert:
    that:
      - redis_port | int != 6379
    msg: "If you want to use port 6379, use the default instance, not a named instance."
  when: not (redis_force_instance_port | bool)

- name: "Instance '{{ redis_instance_name }}' group is present"

  ansible.builtin.group:
    name: "redis-{{ redis_instance_name }}"
    state: present
    system: True
  tags:
    - redis

- name: "Instance '{{ redis_instance_name }}' user is present"
  ansible.builtin.user:
    name: "redis-{{ redis_instance_name }}"
    group: "redis-{{ redis_instance_name }}"
    state: present
    system: True
    shell: '/bin/false'
  tags:
    - redis

- name: "Instance '{{ redis_instance_name }}' config directory is present"
  ansible.builtin.file:
    dest: "{{ redis_conf_dir }}"
    mode: "0750"
    owner: "redis-{{ redis_instance_name }}"
    group: "redis-{{ redis_instance_name }}"
    follow: yes
    state: directory
  tags:
    - redis

- name: "Instance '{{ redis_instance_name }}' config hooks directories are present"
  ansible.builtin.file:
    dest: "{{ _dir }}"
    mode: "0750"
    owner: "redis-{{ redis_instance_name }}"
    group: "redis-{{ redis_instance_name }}"
    follow: yes
    state: directory
  loop:
    - "{{ redis_conf_dir }}/redis-server.pre-up.d"
    - "{{ redis_conf_dir }}/redis-server.post-up.d"
    - "{{ redis_conf_dir }}/redis-server.pre-down.d"
    - "{{ redis_conf_dir }}/redis-server.post-down.d"
  loop_control:
    loop_var: _dir
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('9', '=')
  tags:
    - redis

- name: "Instance '{{ redis_instance_name }}' hooks examples are present"
  ansible.builtin.command:
    cmd: "cp -a /etc/redis/{{ _dir }}/00_example {{ redis_conf_dir }}/{{ _dir }}"
  args:
    creates: "{{ redis_conf_dir }}/{{ _dir }}/00_example"
  loop:
    - "redis-server.pre-up.d"
    - "redis-server.post-up.d"
    - "redis-server.pre-down.d"
    - "redis-server.post-down.d"
  loop_control:
    loop_var: _dir
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('9', '=')
  tags:
    - redis

- name: "Instance '{{ redis_instance_name }}' socket/pid directories are present"
  ansible.builtin.file:
    dest: "{{ _dir }}"
    mode: "0755"
    owner: "redis-{{ redis_instance_name }}"
    group: "redis-{{ redis_instance_name }}"
    follow: yes
    state: directory
  loop:
    - "{{ redis_pid_dir }}"
    - "{{ redis_socket_dir }}"
  loop_control:
    loop_var: _dir
  tags:
    - redis

- name: "Instance '{{ redis_instance_name }}' data/log directories are present"
  ansible.builtin.file:
    dest: "{{ _dir }}"
    mode: "0751"
    owner: "redis-{{ redis_instance_name }}"
    group: "redis-{{ redis_instance_name }}"
    follow: yes
    state: directory
  loop:
    - "{{ redis_data_dir }}"
    - "{{ redis_log_dir }}"
  loop_control:
    loop_var: _dir
  tags:
    - redis

- name: "Instance '{{ redis_instance_name }}' log file are present"
  ansible.builtin.file:
    path: "{{ redis_log_dir }}/redis-server.log"
    mode: "660"
    owner: "redis-{{ redis_instance_name }}"
    group: "adm"
    state: touch
  tags:
    - redis

- name: "Add begin marker if missing"
  ansible.builtin.lineinfile:
    path: "{{ redis_conf_dir }}/redis.conf"
    line: "# BEGIN {{ redis_conf_marker_label }}"
    insertbefore: BOF
    create: yes
  tags:
    - redis

- name: "Add end marker if missing"
  ansible.builtin.lineinfile:
    path: "{{ redis_conf_dir }}/redis.conf"
    line: "# END {{ redis_conf_marker_label }}"
    insertbefore: "# Generated by CONFIG REWRITE"
    create: yes
  tags:
    - redis

- name: "Create config if missing"
  ansible.builtin.blockinfile:
    path: "{{ redis_conf_dir }}/redis.conf"
    marker: "# {mark} {{ redis_conf_marker_label }}"
    block: "{{ lookup('ansible.builtin.template', '../templates/redis.conf.j2') }}"
    mode: "0640"
    owner: redis-{{ redis_instance_name }}
    group: redis-{{ redis_instance_name }}
    create: yes
  notify: "{{ redis_restart_handler_name }}"
  tags:
    - redis

- name: Systemd template for redis instances is installed (Debian 8)
  ansible.builtin.template:
    src: 'redis-server@jessie.service.j2'
    dest: '/etc/systemd/system/redis-server@.service'
    mode: "0644"
    owner: "root"
    group: "root"
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('9', '<')
  tags:
    - redis

- name: Systemd template for redis instances is installed (Debian 9)
  ansible.builtin.template:
    src: 'redis-server@stretch.service.j2'
    dest: '/etc/systemd/system/redis-server@.service'
    mode: "0644"
    owner: "root"
    group: "root"
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('9', '=')
  tags:
    - redis

- name: Systemd template for redis instances is installed (Debian 10 or later)
  ansible.builtin.template:
    src: 'redis-server@buster.service.j2'
    dest: '/etc/systemd/system/redis-server@.service'
    mode: "0644"
    owner: "root"
    group: "root"
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('10', '>=')
  tags:
    - redis

- name: "Instance '{{ redis_instance_name }}' systemd unit is enabled and started"
  ansible.builtin.systemd:
    name: "{{ redis_systemd_name }}"
    enabled: yes
    state: started
    daemon_reload: yes
  tags:
    - redis

- name: Redis SysVinit script is stopped and disabled
  ansible.builtin.service:
    name: "redis-server"
    enabled: no
    state: stopped
  when: redis_default_server_disabled | bool
  tags:
    - redis
