---
#--------------------------------------------------------------#
# Install Redis Packages                         [redis_install]
#--------------------------------------------------------------#
- name: install redis binaries
  tags: redis_install
  environment: "{{ proxy_env | default({}) }}"
  package: name={{ item }} state=present
  with_items:
    - redis
    - "{% if os_package|default('rpm') == 'deb' %}redis-exporter{% else %}redis_exporter{% endif %}"

#--------------------------------------------------------------#
# Redis User                                        [redis_user]
#--------------------------------------------------------------#
- name: create os user redis
  tags: redis_user
  ignore_errors: true
  user: name=redis home=/home/redis generate_ssh_key=yes

#--------------------------------------------------------------#
# Redis Directory                                    [redis_dir]
#--------------------------------------------------------------#
- name: create redis directories
  tags: redis_dir
  block:
    - name: create redis directories
      file: path={{ item }} state=directory owner=redis mode=0700
      with_items:
        - /etc/redis
        - /var/log/redis/
        - /var/run/redis/
        - "{{ redis_fs_main }}/redis"

    # make sure the /var/run/redis is created after reboot
    - name: create redis tmpfiles.d entry
      copy:
        dest: /etc/tmpfiles.d/redis.conf
        content: |
          d /var/run/redis 0755 redis redis -
        owner: root
        group: root
        mode: '0644'

#--------------------------------------------------------------#
# Redis SELinux                                  [redis_selinux]
#--------------------------------------------------------------#
- name: setup selinux for redis
  tags: redis_selinux
  ignore_errors: true
  shell: |
    command -v semanage &>/dev/null || exit 0
    # data dir: redis_var_lib_t for /data/redis (rdb, aof, temp files)
    semanage fcontext -m -t redis_var_lib_t "{{ redis_fs_main }}/redis(/.*)?" 2>/dev/null || \
    semanage fcontext -a -t redis_var_lib_t "{{ redis_fs_main }}/redis(/.*)?" 2>/dev/null
    # run dir: redis_var_run_t for /run/redis (pid files)
    semanage fcontext -m -t redis_var_run_t "/run/redis(/.*)?" 2>/dev/null || \
    semanage fcontext -a -t redis_var_run_t "/run/redis(/.*)?" 2>/dev/null
    restorecon -Rv {{ redis_fs_main }}/redis /run/redis 2>/dev/null
    /bin/true
  args: { executable: /bin/bash }
...