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:
- How to install and configure Redis 6 on Ubuntu 22.04
- Using Ansible to install and configure Redis 6 on Debian 11
- How to install and configure Redis 6 on Ubuntu 20.04
- How to install and configure Redis 6 on FreeBSD 13
- How to install & configure Redis 6 on OpenSUSE Leap 15.3
- How to install and configure Redis 6 on Fedora 34
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:
|
|
Where:
update_cache: yes
- Run the equivalent of apt-get update command on all serversforce_apt_get: yes
- Do not use the aptitude command, instead use the apt-get command on Debian/Ubuntu boxescache_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
|
|
Where:
upgrade: dist
- Run the equivalent ofapt-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.
|
|
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:
|
|
Install Redis 6 server
Now update the cache repo and install redis server
|
|
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
|
|
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.
|
|
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
|
|
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.
|
|
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
|
|
Creating the hosts file
Create a hosts.yaml
file with this content. Ensure that you can connect to the server added here.
|
|
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:
|
|
To run the playbook use this command:
|
|
Conclusion
In this guide, we learnt how to use ansible to install and configure Redis 6 in Ubuntu 20.04.