This Ansible playbook sets up a three-node Redis Sentinel cluster on Ubuntu servers for high availability using the official Redis APT repository.
- 3 Redis instances (1 master, 2 slaves)
- 3 Sentinel instances (one on each server)
- Automatic failover capability
- Uses official Redis APT packages
- 3 Ubuntu servers (18.04 or later)
- SSH access to all servers
- Python 3 installed on target servers
- Ansible installed on control machine
-
Update the inventory file with your server IPs:
cp inventory/hosts.example inventory/hosts # Edit inventory/hosts with your server details -
Configure variables in
group_vars/all.ymlif needed -
(Optional) Check available Redis versions:
ansible-playbook -i inventory/hosts check-redis-versions.yml
-
Run the playbook:
ansible-playbook -i inventory/hosts site.yml
- Redis configuration:
templates/redis.conf.j2 - Sentinel configuration:
templates/sentinel.conf.j2 - Variables:
group_vars/all.yml
By default, the latest available Redis version will be installed. To install a specific version:
- Check available versions:
ansible-playbook -i inventory/hosts check-redis-versions.yml - Set
redis_versioningroup_vars/all.yml(e.g.,redis_version: "6:7.4.2-1rl1~jammy1")
This playbook follows the official Redis documentation for APT installation:
- Adds the official Redis APT repository
- Installs Redis using APT package manager
- Supports version specification
- Uses systemd for service management
After deployment, you can test the cluster:
# Connect to Redis master
redis-cli -h <master-ip> -p 6379
# Check sentinel status
redis-cli -h <any-server-ip> -p 26379 sentinel mastersIf you encounter permission errors (e.g., for /etc/redis/sentinel.conf):
-
Run the debug playbook to check permissions:
ansible-playbook -i inventory/hosts debug-permissions.yml
-
Check if the redis user exists and has proper permissions:
ansible -i inventory/hosts redis_cluster -m command -a "id redis" --become
-
Verify directory permissions:
ansible -i inventory/hosts redis_cluster -m command -a "ls -la /etc/redis" --become
If redis-sentinel package is not found:
-
Check available packages:
ansible-playbook -i inventory/hosts check-sentinel-package.yml
-
Verify Redis repository is properly added:
ansible -i inventory/hosts redis_cluster -m command -a "apt-cache policy redis" --become
If /etc/redis cannot be created, the playbook will automatically fall back to /opt/redis/conf.
- Consider setting up firewall rules
- Use Redis AUTH for production
- Configure SSL/TLS if needed