DEV Community

Cover image for Installing Redis 7.4 with Ansible & a Custom `redis.conf`
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Installing Redis 7.4 with Ansible & a Custom `redis.conf`

Hi there! I'm Maneshwar. Right now, I’m building LiveAPI, a first-of-its-kind tool that helps you automatically index API endpoints across all your repositories. LiveAPI makes it easier to discover, understand, and interact with APIs in large infrastructures.


Redis is a high-performance in-memory key-value store.

If you're deploying it with Ansible and want full control over the version and configuration (including your own redis.conf), here's a clean and repeatable setup to automate the whole thing.

We’ll go from a blank Ansible structure to a fully provisioned Redis instance running version 7.4.1, with your custom config baked in.

Project Structure

We're using a standard Ansible role layout:

ansible/
├── ansible.cfg
├── db.yml
├── hosts.ini
└── roles/
    └── db/
        ├── tasks/
        │   ├── main.yml
        │   └── install_redis.yml
        ├── handlers/
        │   └── main.yml
        ├── templates/
        │   └── redis.conf.j2
        └── ...
Enter fullscreen mode Exit fullscreen mode

Step-by-Step

1. ansible.cfg

Make sure you have a minimal config:

[defaults]
# Use YAML for cleaner stdout/stderr formatting
stdout_callback = yaml
stderr_callback = yaml

# Path to inventory file
inventory = hosts.ini

# Skip SSH host key checking (useful for automation)
host_key_checking = False

# SSH connection timeout in seconds
timeout = 30

# Number of parallel hosts to configure at once
forks = 10

# Speed up Ansible by reusing SSH connections and avoiding sudo temp files
pipelining = True


[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
pipelining = True
Enter fullscreen mode Exit fullscreen mode

2. hosts.ini

Define your target host:

[db]
your-redis-host ansible_user=ubuntu
Enter fullscreen mode Exit fullscreen mode

Replace your-redis-host with the IP or hostname.

3. db.yml — Entry Playbook

---
- name: Install Redis on DB hosts
  hosts: db
  become: yes
  roles:
    - db
Enter fullscreen mode Exit fullscreen mode

4. roles/db/tasks/main.yml

---
- import_tasks: install_redis.yml
Enter fullscreen mode Exit fullscreen mode

5. roles/db/tasks/install_redis.yml

This handles installing Redis 7.4.1 via Redis’s official APT repo, plus pushing your custom config:

---
- name: Install required packages
  apt:
    name: ['lsb-release', 'curl', 'gpg']
    state: present
    update_cache: yes

- name: Add Redis GPG key
  shell: |
    curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
  args:
    creates: /usr/share/keyrings/redis-archive-keyring.gpg

- name: Set permissions on GPG key
  file:
    path: /usr/share/keyrings/redis-archive-keyring.gpg
    mode: '0644'

- name: Add Redis APT repo
  copy:
    dest: /etc/apt/sources.list.d/redis.list
    content: "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb {{ ansible_lsb.codename }} main"

- name: Update APT
  apt:
    update_cache: yes

- name: Install Redis
  apt:
    name: redis
    state: present

- name: Copy custom redis.conf
  template:
    src: redis.conf.j2
    dest: /etc/redis/redis.conf
    owner: redis
    group: redis
    mode: '0644'
  notify: Restart Redis

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

- name: Start Redis
  systemd:
    name: redis-server
    state: started
Enter fullscreen mode Exit fullscreen mode

6. roles/db/handlers/main.yml

---
- name: Restart Redis
  systemd:
    name: redis-server
    state: restarted
Enter fullscreen mode Exit fullscreen mode

7. roles/db/templates/redis.conf.j2

This is your custom redis.conf. You can render variables in it using Jinja2, like:

bind 0.0.0.0
port 6379
requirepass {{ redis_password | default('changeme') }}
maxmemory 256mb
maxmemory-policy allkeys-lru
Enter fullscreen mode Exit fullscreen mode

You can move sensitive stuff (like redis_password) to defaults/main.yml or vault.

Running the Playbook

ansible-playbook db.yml
Enter fullscreen mode Exit fullscreen mode

Verifying

SSH into your target host:

systemctl status redis-server
redis-cli -a changeme ping
Enter fullscreen mode Exit fullscreen mode

You should get:

PONG
Enter fullscreen mode Exit fullscreen mode

Bonus Tips

  • Pin exact Redis version by installing a .deb package or using apt-mark hold redis.
  • Add Redis log/config location to your monitoring tools.
  • Use Ansible Vault to store secrets (like the password).
  • Write a test playbook under roles/db/tests/test.yml to verify installation.

Final Thoughts

This setup gives you:

  • Precise version control with Redis’s upstream APT repo
  • Full override of redis.conf
  • Reusable, modular Ansible role

No surprises. No apt install redis-server with some random system default version. You’re in charge.


LiveAPI helps you get all your backend APIs documented in a few minutes.

With LiveAPI, you can generate interactive API docs that allow users to search and execute endpoints directly from the browser.

LiveAPI Demo

If you're tired of updating Swagger manually or syncing Postman collections, give it a shot.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.