Stalwart mail server Ansible role https://stalw.art/
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Conrad 254560d5b3
All checks were successful
Lint + Tag / format (push) Successful in 1m17s
Lint + Tag / tag (push) Successful in 59s
fix: fixed reloadsettings
2026-10-28 13:41:55 +01:00
.forgejo/workflows feat: initial version 2026-10-27 10:05:42 +01:00
defaults fix: added stalwart admin login variable 2026-10-28 11:01:34 +01:00
meta feat: initial version 2026-10-27 10:05:42 +01:00
tasks fix: fixed reloadsettings 2026-10-28 13:41:55 +01:00
templates feat: moved to external valkey + postgres 2026-10-27 22:34:34 +01:00
vars feat: initial version 2026-10-27 10:05:42 +01:00
.gitignore feat: initial version 2026-10-27 10:05:42 +01:00
CHANGELOG.md fix: fixed reloadsettings 2026-10-28 13:41:55 +01:00
LICENSE feat: initial version 2026-10-27 10:05:42 +01:00
README.md Prettified Code! 2026-10-28 10:02:32 +00:00

Stalwart

Deploys the Stalwart mail and collaboration server (SMTP, IMAP, JMAP, CalDAV/CardDAV) via Docker Compose, as a single node or as a cluster.

Stalwart 1.x keeps almost all of its configuration in its data store. Only the data store definition lives in config.json. The role therefore configures the server with stalwart-cli apply plans (NDJSON, upsert/update only, so re-runs converge). The setup wizard is never used.

Requirements

  • Docker
  • community.docker Ansible collection

How it works

  1. config.json is written to <data>/etc: RocksDB for a single node, the shared PostgreSQL for a cluster.
  2. First deploy (no <docker>/.bootstrapped): Stalwart starts in recovery mode (management API on 8080 only, no mail services) with a temporary recovery admin. The role applies stores, admin (permanent admin) and settings, then recreates the container without the recovery admin. On this first normal start Stalwart creates its default listeners (25, 465, 993, 995, 4190, 443, 8080).
  3. Later deploys log in as <stalwart_admin_name>@<first domain> and apply stores and settings again. A changed stores plan restarts every node, because stores are read at startup only. A changed settings plan triggers ReloadSettings.

The CLI runs as a short-lived stalwartlabs/cli container on the compose network and talks to http://<instance>-app:8080. Port 8080 is never published on the host. The credentials are passed as environment variables, not on the command line.

Clustering

With stalwart_cluster: true, every host of the play becomes a node of one cluster (docs):

  • Data store: PostgreSQL (stalwart_db). Blobs and the search index are stored there as well (Default blob and search stores).
  • In-memory store: Valkey/Redis (stalwart_valkey), for HA through Sentinel (RedisSentinel): Stalwart asks the Sentinels for the current primary and follows a failover on its own (tested: Valkey primary killed, both nodes reconnected to the new one, coordinator kept working).
  • Coordinator: Default (the Redis of the in-memory store). It distributes cache invalidations, config reloads, IMAP IDLE/push events and IP blocks.
  • Node name: STALWART_HOSTNAME = stalwart_node_name (default: inventory hostname). The node ID lease is tied to it, so it must be unique.
  • Roles: STALWART_ROLE = stalwart_node_role. Define the ClusterRole objects via stalwart_plan_extra.

stalwart_cluster_primary (default: the first play host) bootstraps and applies the plans. The other nodes start only afterwards, so they find the stores and the coordinator already in place.

The role runs only Stalwart: PostgreSQL and Valkey are external services (e.g. the valkey role with Sentinel).

PostgreSQL HA caveat: Stalwart connects to exactly one host and does not support libpq's target_session_attrs (checked in its source). A DNS name with several addresses (primary + standby) is therefore not enough: Stalwart could land on a read-only standby, and its pool does not follow a failover. Point stalwart_db.host at an address that always leads to the current primary.

All nodes publish the same mail ports. Spread the clients across them with DNS or a TCP load balancer. Keep the node clocks in sync (NTP), because record IDs and node leases rely on time.

Variables

Variable Default Description
stalwart_instance stalwart Compose project name and container name prefix.
stalwart_data_path /data/stalwart etc/ (config.json), data/ (RocksDB), postgres/ and valkey/ (backend).
stalwart_docker_path /docker/stalwart Compose files, rendered plans and the bootstrap marker.
stalwart_version v1.0.0 stalwartlabs/stalwart tag.
stalwart_cli_version 1.0.14 stalwartlabs/cli tag.
stalwart_hostname ~ Required. Server hostname, e.g. mail.example.com (SystemSettings.defaultHostname).
stalwart_domains [] Required. List of Domain objects (name + any Domain field). The first one is the default domain.
stalwart_admin_name admin Local part of the permanent admin (in the first domain).
stalwart_admin_login "" Login of later deploys if the admin was renamed in the WebUI. Empty: <stalwart_admin_name>@<first domain>.
stalwart_admin_password ~ Required, 8+ characters. Also the temporary recovery admin password during bootstrap.
stalwart_acme_contact ~ When set, Let's Encrypt via TLS-ALPN-01 for every domain without its own certificateManagement.
stalwart_ports [25, 465, 993, 4190] Host ports published 1:1.
stalwart_https_port 443 Host port for HTTPS. ~ = not published (use with Traefik).
stalwart_traefik false Traefik TCP passthrough of stalwart_traefik_sni to Stalwart's 443, with PROXY protocol v2. Traefik's network becomes a trusted proxy network.
stalwart_traefik_entrypoint websecure Traefik entrypoint.
stalwart_traefik_sni [stalwart_hostname] SNI names routed to Stalwart (add autoconfig./mta-sts. names as needed).
stalwart_proxy_trusted_networks [] Extra networks allowed to send PROXY headers (all listeners). Connections from them must send one.
stalwart_plan_extra [] Extra plan operations (one dict per NDJSON line), applied last. Domains are #dom-0, #dom-1, ...
stalwart_cluster false Cluster mode, see above.
stalwart_cluster_primary first play host Node that bootstraps and applies the plans.
stalwart_node_name inventory_hostname STALWART_HOSTNAME, unique per node.
stalwart_node_role ~ STALWART_ROLE.
stalwart_db ~ PostgreSQL: host, password, optional port (5432), database/user (stalwart), tls (true, verified), allow_invalid_certs (false).
stalwart_valkey ~ Valkey/Redis. Sentinel: sentinels (addresses, port 26379 unless host:port), service_name, password, sentinel_password. Single server: host, password, port.
stalwart_uninstall false Remove containers and the compose directory.
stalwart_purge false Also remove stalwart_data_path (mail, config, backend DBs). Irreversible.

Usage

Single node behind Traefik (HTTPS passthrough), mail ports published directly:

stalwart_hostname: mail.example.com
stalwart_domains:
  - name: example.com
stalwart_admin_password: "{{ vault_stalwart_admin_password }}"
stalwart_acme_contact: it@example.com
stalwart_traefik: true
stalwart_https_port: ~

Three-node cluster on external PostgreSQL and Valkey with Sentinel:

# group_vars/mail.yml
stalwart_cluster: true
stalwart_hostname: mail.example.com
stalwart_domains:
  - name: example.com
stalwart_admin_password: "{{ vault_stalwart_admin_password }}"
stalwart_db:
  host: pg-primary.example.internal # always the current primary, see above
  password: "{{ vault_stalwart_db_password }}"
stalwart_valkey:
  sentinels: [10.0.0.21, 10.0.0.22, 10.0.0.23]
  service_name: valkey
  password: "{{ vault_valkey_password }}"
  sentinel_password: "{{ vault_valkey_sentinel_password }}"

Anything the role does not model (DKIM, DNS providers, directories/OIDC, listeners, cluster roles, spam settings) goes into stalwart_plan_extra. The easiest way to find the right shape is to configure it once in the WebUI and export it with stalwart-cli snapshot <Object>.

Notes

  • Admin password changes. The admin is created on the first deploy only, and later deploys log in with stalwart_admin_password. To rotate it, change it in the WebUI first and then in the inventory. If the password is lost, delete <docker>/.bootstrapped and redeploy. The bootstrap then runs again with the recovery admin and resets the admin to the inventory password.
  • PROXY protocol. A trusted network must send a PROXY header on every connection, otherwise the connection is closed. Do not trust networks from which clients connect directly.
  • Upgrades. Bump stalwart_version only after reading the upgrade notes. In a cluster, all nodes are recreated in the same run.