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.

Related Content:

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

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:

1
2
3
4
5
- name: Update apt repo and cache on all Debian/Ubuntu boxes
  apt:
    update_cache: yes
    force_apt_get: yes
    cache_valid_time: 3600

Where:

  • update_cache: yes - Run the equivalent of apt-get update command on all servers
  • force_apt_get: yes - Do not use the aptitude command, instead use the apt-get command on Debian/Ubuntu boxes
  • 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

1
2
3
4
- name: Upgrade all packages on servers
  apt:
    upgrade: dist
    force_apt_get: yes

Where:

  • 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.
  • force_apt_get: yes - Use apt-get instead of aptitude.

Installing some common packages

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

1
2
3
4
5
6
- name: Install required packages
  apt:
    name:
      - gnupg
      - vim
    state: latest

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:

1
2
3
4
- 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    

Install Redis 6 server

Now update the cache repo and install redis server

1
2
3
4
5
6
- 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.

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

1
2
3
4
5
6
7
- 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.

1
2
3
4
5
6
7
8
- 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

1
2
3
4
5
6
7
8
- 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

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
- 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

Creating the hosts file

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

1
2
3
4
5
all:
  hosts:
    ubuntusrv:
      ansible_ssh_host: 10.2.11.10
      ansible_ssh_user: ubuntu

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:

1
pip install ansible

To run the playbook use this command:

1
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
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy