Instances and replicas
PostgreSQL instances are declared in pgcli_instances, normally in one
deployment-mode group’s group_vars/<mode>/main.yml. apply.yaml --tags instance (run with serial: 1) reconciles each host’s entries whose host:
matches that host.
The three mode examples
Each example file is small; copy the one matching the mode you exercise.
group_vars/single/main.yml.example
One stand-alone primary:
group_vars/samehost/main.yml.example
Primary and a read-only replica on the same host:
A same-host replica inherits its primary’s namespace; give it the same
namespace only if you set one explicitly.
group_vars/crosshost/main.yml.example
The replica lives on another host and names both the primary instance and its host:
The primary’s host must come first in [crosshost]: the play is serial, and
the replica needs its primary running. The role prepares the primary side
(replication slot + pg_hba) before creating the replica with --primary-dsn,
reading the primary password from that host’s pg.yaml.
pgcli_instances fields
| Field | Applies to | Meaning |
|---|---|---|
name |
all | Required instance name |
host |
all | Inventory host that owns the instance |
namespace |
all | Name from pgcli_namespaces; omit for the host default |
state |
all | present, started (default), stopped, absent |
autostart |
all | Enable the instance at boot (pg autostart enable) |
backup_target |
primary | local (default) or s3; fixed at creation time |
password_length |
primary | Generated admin password length (default 16) |
extensions |
primary | Extension list (default pg_stat_statements); setting it replaces the default list |
clean_data |
all | Only with state: absent; also deletes data/WAL dirs and the stanza |
replica_of |
replica | Name of the primary |
replica_of_host |
replica | Inventory host of a cross-host primary |
Replica entries accept only name, replica_of, replica_of_host,
namespace, state, autostart and clean_data. Password, database, image,
extension and PITR settings are inherited from the primary.
Defaults come from pgcli_instance_defaults in group_vars/all/main.yml:
State semantics
presentcreates the instance without changing its run state.startedcreates (if needed) and starts it.stoppedcreates (if needed) and stops it.absentremoves the container and the config entry. The data and WAL directories and the pgBackRest stanza are kept unlessclean_data: true. A full destroy therefore needs both:state: absentandclean_data: true;clean_dataalone does nothing.
Namespaces
pgcli_namespaces maps a selector name to a directory holding pg.yaml:
Instances select one with namespace: <name>; omitted means the host default
(pgcli_namespace_path / pgcli_config_path). Extra entries with their own
base_dir are initialized automatically by the pgcli role; entries without
base_dir need a manual pg config init -o.
Run it
Re-running is idempotent; instances already in the desired state report ok.