An Ansible role for installing and managing PostgreSQL servers. This role works with both Debian and RedHat based systems, and provides backup scripts for PostgreSQL Continuous Archiving and Point-in-Time Recovery.
On RedHat-based platforms, the PostgreSQL Global Development Group (PGDG) packages packages will be installed. On Debian-based platforms, you can choose from the distribution's packages (from APT) or the PGDG packages.
Changes that require a restart will not be applied unless you manually restart PostgreSQL. This role will reload the server for those configuration changes that can be updated with only a reload because reloading is a non-intrusive operation, but options that require a full restart will not cause the server to restart.
This role requires Ansible 1.8+
-
postgresql_version: PostgreSQL version to install. On Debian-based platforms, the default is whatever version is pointed to by thepostgresqlmetapackage). On RedHat-based platforms, the default is9.4. -
postgresql_flavor: On Debian-based platforms, this specifies whether you want to use PostgreSQL packages from pgdg or the distribution's apt repositories. Possible values:apt,pgdg(default:apt). -
postgresql_conf: A hash (dictionary) ofpostgresql.confoptions and values. These options are not added topostgresql.confdirectly - the role adds aconf.dsubdirectory in the configuration directory and an include statement for that directory topostgresql.conf. Options set inpostgresql_confare then set inconf.d/25ansible_postgresql.conf.Due to YAML parsing, you must take care when defining values in
postgresql_confto ensure they are properly written to the config file. For example:postgresql_conf: max_connections: 250 archive_mode: "off" work_mem: "'8MB'"
Becomes the following in
25ansible_postgresql.conf:max_connections = 250 archive_mode = off work_mem: '8MB' -
postgresql_pg_hba_conf: A list of lines to add topg_hba.conf -
postgresql_pg_hba_local_postgres_user: If set tofalse, this will remove thepostgresuser's entry frompg_hba.confthat is preconfigured on Debian-based PostgreSQL installations. You probably do not want to do this unless you know what you're doing. -
postgresql_pg_hba_local_socket: If set tofalse, this will remove thelocalentry frompg_hba.confthat is preconfigured by the PostgreSQL package. -
postgresql_pg_hba_local_ipv4: If set tofalse, this will remove thehost ... 127.0.0.1/32entry frompg_hba.confthat is preconfigured by the PostgreSQL package. -
postgresql_pg_hba_local_ipv6: If set tofalse, this will remove thehost ... ::1/128entry frompg_hba.confthat is preconfigured by the PostgreSQL package. -
postgresql_pgdata_dir: Only set this if you have changed the$PGDATAdirectory from the package default. Note this does not configure PostgreSQL to actually use a different directory, you will need to do that yourself, it just allows the role to properly locate the directory. -
postgresql_conf_dir: As withpostgresql_pgdata_direxcept for the configuration directory.
-
postgresql_backup_dir: If set, enables PITR backups. Set this to a directory where your database will be backed up (this can be any format supported by rsync, e.g.user@host:/path). The most recent backup will be in a subdirectory namedcurrent. -
postgresql_backup_rotate: Boolean, defaults totrue, which will cause thecurrentdirectory to be renamed prior to creating a new backup. If set tofalse,currentwill be deleted (this is useful if you are using snapshots or some other means to archive previous backups). -
postgresql_backup_local_dir: Filesystem path on the PostgreSQL server where backup scripts will be placed and working WALs will be written prior to a WAL archive. -
postgresql_backup_[hour|minute]: Controls what time the cron job will run to perform a full backup. Defaults to 1:00 AM. -
postgresql_backup_[day|month|weekday]: Additional cron controls for when the full backup is performed (default:*). -
postgresql_backup_mail_recipient: User or address that should receive mail from the backup scripts. -
postgresql_backup_remote_rsync_path: Path torsyncon the remote system. -
postgresql_backup_post_command: Arbitrary command to run after successful completion of a scheduled backup.
None
Standard install: Default postgresql.conf, pg_hba.conf and default version
for the OS:
---
- hosts: dbservers
remote_user: root
roles:
- postgresqlUse the pgdg packages on a Debian-based host:
---
- hosts: dbservers
remote_user: root
vars:
postgresql_flavor: pgdg
roles:
- postgresqlUse the PostgreSQL 9.3 packages and set some postgresql.conf options and
pg_hba.conf entries:
---
- hosts: dbservers
remote_user: root
vars:
postgresql_version: 9.3
postgresql_conf:
listen_addresses: "''" # disable network listening (listen on unix socket only)
max_connections: 50 # decrease connection limit
postgresql_pg_hba_conf:
- host all all 10.0.0.0/8 md5
roles:
- postgresqlEnable backups to /archive
- hosts: all
remote_user: root
vars:
postgresql_backup_dir: /archive
roles:
- postgresql