Automation

How to using Ansible to install and configure Redis 6 on Rocky Linux 8

Pinterest LinkedIn Tumblr

In this tutorial we are going to learn how to install and configure Redis 6 on Rocky Linux 8 using Ansible. This guide will also work on other RHEL 8 based servers like Oracle Linux and Alma Linux.

Redis is an in-memory data structure store, used as a distributed, in-memory key–value database, cache and message broker, with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indices.

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Related Content

Prerequisites

To follow along, ensure that you have:

  • An updated Rocky Linux 8 server
  • Access to the Internet
  • Root access to the server or user with sudo access

Table of Content

  1. Ansible tasks to ensure server is up to date
  2. Eanbling the Remi Release repo
  3. Enable the redis version 6 module
  4. Installing Redis 6 server
  5. Configuring Redis 6 Server
  6. Setting up the handlers
  7. The whole playbook to install and set up Redis 6

1. Ansible tasks to ensure the server is up to date

Before proceeding, let us make sure that the server is up to date using these tasks:

- name: Upgrade packages
  dnf:
    name: "*"
    state: latest

2. Enabling the Remi Release repo

The Rocky Linux repos has redis in it’s default repos. But it is not the latest version. We are going to use the Remi repos to get the latest version of redis. Add the remi repo using this task

- name: Enable the remi repo
  shell: |
    dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
  args:
    warn: no

3. Enable the redis version 6 module

Redis is available in different versions. To enable the latest version of redis, we use dnf module enable. This task achieves that.

- name: Enable the redis module for version 6.2
  shell: |
    dnf module enable redis:remi-6.2 -y
  args:
    warn: no

4. Install Redis 6 server

Finally we can install redis.

- name: Install redis
  dnf:
    name: redis
    state: latest

We are using dnf to ensure that we have latest redis installed. Since we enabled the module for redis 6.2, that will be installed.

5. Configuring Redis 6 server

Next we are to configure the server to be production ready.

Add a line to allow the server to write to a pid file

- name: Set redis server pid file
  lineinfile:
    path: /etc/redis/redis.conf
    regexp: "^pidfile"
    line: "pidfile /var/run/redis/redis-server.pid"
  notify:
    - Restart Redis

In the above, we are updating the redis config file to append the line pidfile /var/run/redis/redis-server.pid so it can write a pid file. We are also setting up a handler to restart the service when the playbook is done executing.

Next, let us set up a password for out redis server to boost security.

- name: Secure redis with a password
  lineinfile:
    dest: /etc/redis/redis.conf
    regexp: '^(# )?requirepass \w*$'
    line: 'requirepass {{ redis_password }}'
    state: present
  notify:
    - Restart Redis

In the above, we are updating the redis config file to append the line requirepass {{ redis_password }} so it can set the password to be the value set. We are also setting up a handler to restart the service when the playbook is done executing.

Finally, let us bind the service to 0.0.0.0 so it is accessible externally using bind 0.0.0.0

- name: Bind redis to 0.0.0.0
  lineinfile:
    dest: /etc/redis/redis.conf
    regexp: '^bind \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b$'
    line: 'bind 0.0.0.0'
    state: present
  notify:
    - Restart Redis

6. Setting up the handlers

Let us set up the handlers listed above. Handlers are just like normal tasks in an Ansible playbook but they run only when if the Task contains a “notify” directive. It also indicates that it changed something.

- name: Restart Redis
  systemd:
    name: redis
    state: restarted

- name: Start Redis
  systemd:
    name: redis
    state: started

- name: Stop Redis
  systemd:
    name: redis
    state: stopped

- name: Enable Redis
  systemd:
    name: redis
    enabled: yes

Those handlers will be run after the tasks to restart and enable the service.

7. The whole playbook to install and set up Redis 6

This is the final playbook. I have the file saved as setup-redis-rocky.yaml

---
- name: Install and set up Redis 6 in Rocky Linux 8
  hosts: rockylinuxsrv
  become: yes
  gather_facts: False
  vars:
    redis_password: j2GfJuLFR8
  tasks:
    - name: Upgrade packages
      dnf:
        name: "*"
        state: latest

    - name: Enable the remi repo
      shell: |
        dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
      args:
        warn: no

    - name: Enable the redis module for version 6.2
      shell: |
        dnf module enable redis:remi-6.2 -y
      args:
        warn: no

    - name: Install redis
      dnf:
        name: redis
        state: latest

    - name: Set redis server pid file
      lineinfile:
        path: /etc/redis/redis.conf
        regexp: "^pidfile"
        line: "pidfile /var/run/redis/redis-server.pid"
      notify:
        - Restart Redis

    - name: Secure redis with a password
      lineinfile:
        dest: /etc/redis/redis.conf
        regexp: '^(# )?requirepass \w*$'
        line: 'requirepass {{ redis_password }}'
        state: present
      notify:
        - Restart Redis
        - Enable Redis

    - name: Bind redis to 0.0.0.0
      lineinfile:
        dest: /etc/redis/redis.conf
        regexp: '^bind \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b$'
        line: 'bind 0.0.0.0'
        state: present
      notify:
        - Restart Redis
        - Enable Redis

  handlers:
    - name: Restart Redis
      systemd:
        name: redis
        state: restarted

    - name: Start Redis
      systemd:
        name: redis
        state: started

    - name: Stop Redis
      systemd:
        name: redis
        state: stopped

    - name: Enable Redis
      systemd:
        name: redis
        enabled: yes

8. Creating the hosts file

Create a hosts.yaml file with this content. Ensure that you can connect to the server added here.

all:
  hosts:
    rockylinuxsrv:
      ansible_ssh_host: 10.2.11.10
      ansible_ssh_user: rocky

8. Running the playbook

You need Ansible installed locally. To install Ansible, you can use the OS package manager or using pip. Use this pip command to install ansible:

pip install ansible

To run the playbook use this command:

ansible-playbook -i hosts.yaml setup-redis-rocky.yaml -vv

Conclusion

In this guide, we learnt how to use ansible to install and configure Redis 6 in Rocky Linux 8.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment