How to Use Ansible to install and configure Redis 6 on Ubuntu 20.04

In this tutorial we are going to learn how to install and configure Redis 6 on Ubuntu 20.04 using Ansible.

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.

Prerequisites

To follow along, ensure that you have:

  • An updated Ubuntu 20.04 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. Installing some common packages
  3. Setting up Redis 6 repo
  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 in Ubuntu 20.04

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: Update apt repo and cache on all Debian/Ubuntu boxes
  apt:
    update_cache: yes
    force_apt_get: yes
    cache_valid_time: 3600

Where:

  1. update_cache: yes – Run the equivalent of apt-get update command on all servers
  2. force_apt_get: yes – Do not use the aptitude command, instead use the apt-get command on Debian/Ubuntu boxes
  3. cache_valid_time: 3600 – Update the apt cache if its older than the cache_valid_time. This option is set in seconds. In this examples, it is set to 3600 seconds.

Doing the upgrade

- name: Upgrade all packages on servers
  apt:
    upgrade: dist
    force_apt_get: yes

Where:

  1. upgrade: dist – Run the equivalent of “apt-get upgrade” command on all Ubuntu or Debian Linux servers. In other words, upgrade all packages to latest version.
  2. force_apt_get: yes – Use apt-get instead of aptitude.

2. Installing some common packages

Use this to install common packages. The gnugpg package is required to set up the repos in Ubuntu.

- name: Install required packages
  apt:
    name:
      - gnupg
      - vim
    state: latest

3. Setting up Redis 6 Repo

You can install the latest stable version (version 6) of Redis from the official packages.redis.io APT repository. Add the repository to the apt index, update it and install:

- name: Set up Redis 6 repo
  shell: |
    curl https://packages.redis.io/gpg | sudo apt-key add -
    echo "deb https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

4. Install Redis 6 server

Now update the cache repo and install redis server

- name: Install redis
  apt:
    name: redis-server
    update_cache: yes
  notify:
    - Enable Redis

Here we are using apt to first update the cache with update_cache: yes then installing redis-server and finally setting up a handler to enable the redis-server on boot.

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.

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

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

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

  - name: Enable Redis
    systemd:
      name: redis-server
      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 in Ubuntu 20.04

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

---
- name: Install and set up Redis 6 in Ubuntu 20.04
  hosts: ubuntusrv
  become: yes
  gather_facts: False
  vars:
    redis_password: j2GfJuLFR8
  tasks:
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600

    - name: Upgrade all packages on servers
      apt:
        upgrade: dist
        force_apt_get: yes

    - name: Set up Redis 6 repo
      shell: |
        curl https://packages.redis.io/gpg | sudo apt-key add -
        echo "deb https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

    - name: Install redis
      apt:
        name: redis
        update_cache: yes
      notify:
        - Enable Redis

    - 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

    - 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

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

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

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

    - name: Enable Redis
      systemd:
        name: redis-server
        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:
    ubuntusrv:
      ansible_ssh_host: 10.2.11.10
      ansible_ssh_user: ubuntu

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-ubuntu.yaml -vv

Conclusion

In this guide, we learnt how to use ansible to install and configure Redis 6 in Ubuntu 20.04.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy