Valkey Ansible Role https://valkey.io/
  • Jinja 50.6%
  • Shell 49.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Conrad 68ebc4dcfb
All checks were successful
Lint + Tag / format (push) Successful in 58s
Lint + Tag / tag (push) Successful in 1m0s
fix: adjusted container permissions
2026-10-28 00:20:51 +01:00
.forgejo/workflows feat: initial commit 2026-10-27 22:32:22 +01:00
defaults feat: initial commit 2026-10-27 22:32:22 +01:00
files feat: initial commit 2026-10-27 22:32:22 +01:00
meta feat: initial commit 2026-10-27 22:32:22 +01:00
tasks fix: adjusted container permissions 2026-10-28 00:20:51 +01:00
templates feat: initial commit 2026-10-27 22:32:22 +01:00
.gitignore feat: initial commit 2026-10-27 22:32:22 +01:00
CHANGELOG.md fix: adjusted container permissions 2026-10-28 00:20:51 +01:00
LICENSE feat: initial commit 2026-10-27 22:32:22 +01:00
README.md Prettified Code! 2026-10-27 21:33:26 +00:00

Valkey

Deploys Valkey with Sentinel via Docker Compose: data nodes (one primary, replicas) plus Sentinels that detect a failed primary and promote a replica automatically. Clients that speak Sentinel (e.g. Stalwart's RedisSentinel) ask the Sentinels for the current primary and follow a failover on their own.

Requirements

  • Docker
  • community.docker Ansible collection

Layout for HA

Three hosts, each with a Sentinel; two of them also run a data node:

Host Data node Sentinel
A yes yes
B yes yes
C – yes (witness)

Any one host can fail: the remaining two Sentinels are a majority (quorum 2 of 3) and fail over to the other data node. Tested: primary killed → replica promoted in about 7 s; witness down and primary killed → failover in about 6 s; the old primary comes back as a replica of the new one and has the writes made in between.

# all three hosts
valkey_servers: [10.0.0.21, 10.0.0.22]
valkey_sentinels: [10.0.0.21, 10.0.0.22, 10.0.0.23]
valkey_password: "{{ vault_valkey_password }}"
valkey_sentinel_password: "{{ vault_valkey_sentinel_password }}"

# host C only
valkey_server: false

How it works

Containers use host networking: the address a node listens on (valkey_address, default ansible_host) is the one it announces. The configs rendered by Ansible are static; who is primary is decided at runtime by the Sentinels, so a re-run of the role never undoes a failover:

  • Data node start (files/valkey-start.sh): asks the Sentinels for the current primary and starts as primary or as its replica. Only on the very first start (empty data directory, no Sentinel knows the service yet) the first entry of valkey_servers becomes primary. A node that already has data never guesses: if a reachable Sentinel refuses (wrong password) it exits, if none is reachable it retries for 60 s and then exits (the container restarts). Starting it as a second primary would split the set.
  • Sentinel start (files/sentinel-start.sh): Sentinel keeps its own state file (<data>/sentinel/sentinel.conf) across restarts. It is regenerated only when the rendered base changes, and then monitors the current primary (asked from the other Sentinels, else the data node that reports role master).

Variables

Variable Default Description
valkey_instance valkey Compose project name and container name prefix.
valkey_docker_path /docker/valkey Compose file, configs, start scripts.
valkey_data_path /data/valkey server/ (AOF/RDB), sentinel/ (Sentinel state).
valkey_version 9.1.3-alpine valkey/valkey tag.
valkey_server true Run a data node on this host.
valkey_sentinel true Run a Sentinel on this host.
valkey_address ansible_host Listen and announce address (must be in valkey_servers/valkey_sentinels for what the host runs).
valkey_port 6379 Data node port.
valkey_sentinel_port 26379 Sentinel port.
valkey_service_name valkey Name of the monitored service (clients ask for it).
valkey_servers [] Required. Data node addresses; the first is the initial primary.
valkey_sentinels [] Required. Sentinel addresses.
valkey_quorum majority Sentinels that must agree a primary is down.
valkey_down_after_ms 5000 Unreachable this long = down.
valkey_failover_timeout_ms 60000 Sentinel failover timeout. A second failover of the same service waits 2× this.
valkey_password ~ Required, 16+ characters, no '. Data nodes (requirepass, replication).
valkey_sentinel_password ~ Required, 16+ characters, no '. Sentinels.
valkey_maxmemory "0" e.g. "512mb"; "0" = none.
valkey_maxmemory_policy noeviction Fail writes when full instead of evicting (store, not cache).
valkey_appendonly true AOF persistence, fsync every second.
valkey_uninstall false Remove containers and the compose directory.
valkey_purge false Also remove valkey_data_path. Irreversible.

Notes

  • Config changes restart the containers. Restarting the primary's data node makes the Sentinels fail over (after valkey_down_after_ms). Run the role with serial: 1 across hosts.
  • Replication is asynchronous. Writes acknowledged by the primary in the last moment before it dies can be lost on failover. Fine for caches, locks and coordination; not a database.
  • No TLS. Traffic and passwords are plain on the network: restrict who can reach the ports (firewall).