
---
- name: (redis-server.yml) Set var '_redis_conf'
  ansible.builtin.set_fact:
    _redis_conf: "{{ '/etc/redis.conf' if ansible_facts['distribution'] == 'CentOS' else '/etc/redis/redis.conf' }}"

- name: (redis-server.yml) update
  ansible.builtin.apt:
    update_cache: true
    cache_valid_time: "{{ 0 if apt_config_updated is defined and apt_config_updated.changed else apt_update_cache_valid_time }}"
  when:
    - ansible_facts['distribution'] == "Debian"
    - apt_update|bool
  tags:
    - redis-server

- name: (redis-server.yml) Configure any half-installed packages 'dpkg --configure -a'
  ansible.builtin.command: dpkg --configure -a
  register: _dpkg_configure
  changed_when: (_dpkg_configure.stdout | default('')) | length > 0
  failed_when: _dpkg_configure.rc != 0
  when:
    - ansible_facts['distribution'] == "Debian"
    - apt_dpkg_configure|bool
  tags:
    - redis-server

- name: (redis-server.yml) upgrade
  ansible.builtin.apt:
    upgrade: "{{ apt_upgrade_type }}"
    update_cache: true
    dpkg_options: "{{ apt_upgrade_dpkg_options | join(',') }}"
  when:
    - ansible_facts['distribution'] == "Debian"
    - apt_upgrade|bool
  tags:
    - redis-server

- name: (redis-server.yml) Install redis-server packages (debian system)
  ansible.builtin.apt:
    name: redis-server
    state: present
  when:
    - ansible_facts['distribution'] == "Debian"
  tags:
    - redis-server

- name: (redis-server.yml) Install redis packages (centos / fedora system)
  ansible.builtin.dnf:
    name: redis
    state: latest
    update_cache: true
  when:
    - ansible_facts["os_family"] == "RedHat"
    - ansible_distribution == "CentOS" or ansible_distribution == "Fedora"
  tags:
    - redis-server

- name: (redis-server.yml) Determine available users
  ansible.builtin.getent:
    database: passwd
  tags:
    - redis-server

- name: (redis-server.yml) Determine available groups
  ansible.builtin.getent:
    database: group
  tags:
    - redis-server

- name: (redis-server.yml) Add user 'www-data' to group 'redis'
  ansible.builtin.user:
    name: www-data
    groups: redis
    append: true
  when:
    - "'www-data' in my_users"
    - "'redis' in my_groups"
  vars:
    my_users: "{{ ansible_facts.getent_passwd.keys() | list }}"
    my_groups: "{{ ansible_facts.getent_group.keys() | list }}"
  tags:
    - redis-server

- name: (redis-server.yml) Add user 'webadmin' to group 'redis'
  ansible.builtin.user:
    name: webadmin
    groups: redis
    append: true
  when:
    - "'webadmin' in my_users"
    - "'redis' in my_groups"
  vars:
    my_users: "{{ ansible_facts.getent_passwd.keys() | list }}"
    my_groups: "{{ ansible_facts.getent_group.keys() | list }}"
  tags:
    - redis-server

- name: (redis-server.yml)  Check if redis configuration file exists
  ansible.builtin.stat:
    path: "{{ _redis_conf }}.ORIG"
  register: redis_conf_exists
  tags:
    - redis-server

- name: (redis-server.yml) Backup existing redis configuration file.
  ansible.builtin.command: cp -a  "{{ _redis_conf }}" "{{ _redis_conf }}".ORIG
  when:
    - redis_conf_exists.stat.exists == False
  tags:
    - redis-server

- name: (redis-server.yml) adjust redis configuration
  ansible.builtin.lineinfile:
    dest: "{{ _redis_conf }}"
    regexp: "{{ item.regexp }}"
    insertafter: "{{ item.insertafter }}"
    line: "{{ item.key }} {{ item.val }}"
    state: present
  loop:
    - { regexp: "^bind\\s+", key: "bind", val: "127.0.0.1 ::1", insertafter: "^#\\s*bind\\s+" }
    - { regexp: "^port\\s+", key: "port", val: "6379", insertafter: "^#\\s*port\\s+" }
    - { regexp: "^unixsocket\\s+", key: "unixsocket", val: "/run/redis/redis-server.sock", insertafter: "^#\\s*unixsocketperm" }
    - { regexp: "^unixsocketperm", key: "unixsocketperm", val: "770", insertafter: "^unixsocket\\s+" }
    - { regexp: "^logfile", key: "logfile", val: "/var/log/redis/redis-server.log", insertafter: "^#\\s+logfile\\s+" }
  notify: "Restart redis-server"
  tags:
    - redis-server
