Skip to content

Commit

Permalink
Modified vars
Browse files Browse the repository at this point in the history
  • Loading branch information
atsikham committed May 24, 2021
1 parent e70b123 commit da57e0c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- name: Check if database is running on node0 database server
become: true
become_user: postgres
command: "{{ repmgr.pg_bindir[ansible_os_family] }}/pg_isready"
command: "{{ pg.bindir[ansible_os_family] }}/pg_isready"
register: node0
ignore_errors: True
when: groups['postgresql'][0] == inventory_hostname
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
pg:
bindir:
RedHat: /usr/pgsql-13/bin
Debian: /usr/lib/postgresql/13/bin
service_name:
RedHat: postgresql-13
Debian: postgresql
Expand All @@ -20,9 +23,6 @@ repmgr:
bindir:
RedHat: /usr/pgsql-13/bin
Debian: /usr/bin
pg_bindir:
RedHat: /usr/pgsql-13/bin
Debian: /usr/lib/postgresql/13/bin
service_name:
RedHat: repmgr13
Debian: repmgrd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
== Requirements

* Internet connection
* General molecule link:https://molecule.readthedocs.io/en/latest/installation.html[dependencies]
* Molecule link:https://molecule.readthedocs.io/en/latest/installation.html[dependencies]
* Docker
* ansible-lint

Expand All @@ -25,6 +25,7 @@ molecule test -s <scenario-name>
----

=== Separate steps

[source:bash]
----
molecule lint -s <scenario-name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
path: /etc/pgbouncer/userlist.txt
line: '"postgres" "*"'
create: true
mode: u=rw,g=,o=
backup: true

- name: Extensions | PgBouncer | Create pgbouncer logrotate configuration file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: Extensions | Repmgr | RedHat | Create repmgr symlink
- name: Extensions | Repmgr | RedHat | Create Repmgr symlink
file:
src: "{{ repmgr.pg_bindir['RedHat'] }}/repmgr"
src: "{{ pg.bindir['RedHat'] }}/repmgr"
dest: "/usr/bin/repmgr"
state: link
when:
Expand Down Expand Up @@ -46,22 +46,15 @@
group: postgres
mode: u=rw,g=,o=

# --- Configure repmgr ---
# --- Configure Repmgr ---

- name: Extensions | Repmgr | Create database for repmgr
- name: Extensions | Repmgr | Create database for Repmgr
become_user: postgres
postgresql_db:
name: "{{ specification.extensions.replication.repmgr_database }}"
owner: "{{ specification.extensions.replication.replication_user_name }}"

# Node id is a constant value independent on order in Ansible host group
- name: Extensions | Repmgr | Set node id
set_fact:
pg_repmgr_node_id: >-
{{ pg_repmgr_node_id | default(groups.postgresql.index(inventory_hostname) + 1) }}
cacheable: true

- name: Extensions | Repmgr | Create repmgr config file
- name: Extensions | Repmgr | Create Repmgr config file
template:
src: repmgr.conf.j2
dest: "{{ repmgr.config_dir[ansible_os_family] }}/repmgr.conf"
Expand All @@ -70,10 +63,10 @@
mode: u=rw,g=,o=
register: template_repmgr_conf
vars:
node_id: "{{ pg_repmgr_node_id }}"
node_id: "{{ pg_node_id }}"

# On Ubuntu config file location is not set by package (see https://repmgr.org/docs/5.2/packages-debian-ubuntu.html).
# Create symlink to allow using repmgr commands without specifying config file location (which is custom).
# Create symlink to allow using Repmgr commands without specifying config file location (which is custom).
# See https://repmgr.org/docs/5.2/configuration-file.html
- name: Extensions | Repmgr | Debian specific tasks
when: ansible_os_family == 'Debian'
Expand All @@ -87,7 +80,7 @@
group: postgres
when: template_repmgr_conf.dest != '/etc/repmgr.conf'

# For repmgr installed from Ubuntu package additional configuration is required before repmgrd is started as daemon
# For Repmgr installed from Ubuntu package additional configuration is required before repmgrd is started as daemon
- name: Extensions | Repmgr | Set repmgr.conf file in /etc/default/repmgrd
replace:
path: /etc/default/repmgrd
Expand Down Expand Up @@ -135,9 +128,9 @@
# on system) and [email protected] (service template for clusters). The latter is 'PartOf' the former.
# Ansible seems to not wait for dependent service to be stopped so we need to stop both to avoid error
# from 'repmgr standby clone'.
- name: Extensions | Repmgr | Stop postgresql service
- name: Extensions | Repmgr | Stop PostgreSQL service
systemd:
name: "{{ item }}"
name: "{{ service_name }}"
state: stopped
vars:
_services:
Expand All @@ -146,22 +139,24 @@
- "{{ pg.instantiated_service_name['Debian'] }}"
RedHat:
- "{{ pg.service_name['RedHat'] }}"
loop_control:
loop_var: service_name
loop: "{{ _services[ansible_os_family] }}"

- name: Extensions | Repmgr | Clone standby node with repmgr
- name: Extensions | Repmgr | Clone standby node with Repmgr
become_user: postgres
command: >-
{{ repmgr.bindir[ansible_os_family] }}/repmgr standby clone
-h {{ hostvars[groups.postgresql[0]].ansible_default_ipv4.address }}
-U {{ specification.extensions.replication.privileged_user_name }}
-d {{ specification.extensions.replication.repmgr_database }} -p 5432 --force
- name: Extensions | Repmgr | Start postgresql service
- name: Extensions | Repmgr | Start PostgreSQL service
systemd:
name: "{{ pg.service_name[ansible_os_family] }}"
state: started

- name: Extensions | Repmgr | Register node with repmgr
- name: Extensions | Repmgr | Register node with Repmgr
become_user: postgres
command: >-
{{ repmgr.bindir[ansible_os_family] }}/repmgr standby register --force
Expand All @@ -171,7 +166,7 @@
connect_timeout=2'
--upstream-node-id 1
- name: Extensions | Repmgr | Restart and enable repmgr service
- name: Extensions | Repmgr | Restart and enable Repmgr service
systemd:
name: "{{ repmgr.service_name[ansible_os_family] }}"
state: restarted
Expand All @@ -193,7 +188,7 @@
- name: Extensions | Repmgr | Tasks to run only on standby node
when: pg_node_primary_found.stdout != inventory_hostname
block:
- name: Extensions | Repmgr | Check if node is not attached to repmgr
- name: Extensions | Repmgr | Check if node is not attached to Repmgr
become_user: postgres
command: "{{ repmgr.bindir[ansible_os_family] }}/repmgr cluster show"
changed_when: false
Expand All @@ -202,7 +197,5 @@

- name: Extensions | Repmgr | Attach active standby to current primary node
when: "'not attached to its upstream node' in pg_cluster_status.stdout|lower"
become: true
become_user: postgres
become_method: sudo
command: "{{ repmgr.bindir[ansible_os_family] }}/repmgr standby follow"
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
- name: Set 'enabled_extensions' fact
set_fact:
enabled_extensions: >-
{{ (_defined_extensions | selectattr('config.enabled') | list) + _undefined_extensions }}
{{ _defined_enabled_extensions + _undefined_extensions }}
vars:
_defined_extensions: >-
_defined_enabled_extensions: >-
{{ specification.extensions
| dict2items(key_name='name', value_name='config')
| selectattr('config.enabled', 'defined')
| selectattr('config.enabled')
| map(attribute='name')
| list }}
_undefined_extensions: >-
{{ specification.extensions
| dict2items(key_name='name', value_name='config')
| selectattr('config.enabled', 'undefined')
| map(attribute='name')
| list }}
# Node id is a constant value independent on order in Ansible host group
- name: Set 'pg_node_id' fact
set_fact:
pg_node_id: >-
{{ pg_node_id | default(groups.postgresql.index(inventory_hostname) + 1) }}
cacheable: true

- name: Install OS-specific packages
package:
name: "{{ _packages[ansible_os_family] }}"
Expand All @@ -27,16 +37,16 @@
- postgresql-client-13
- postgresql-13
- postgresql-contrib-13
- python-psycopg2 # required for postgresql ansible management
- python-psycopg2 # required for PostgreSQL ansible management
RedHat:
- postgresql13-server
- python-psycopg2 # required for postgresql ansible management
- python-psycopg2 # required for PostgreSQL ansible management
module_defaults:
yum: { lock_timeout: "{{ yum_lock_timeout }}" }

# Extension packages need to be installed first to be able to process PostgreSQL config
- name: Install extension packages
include_tasks: extensions/{{ extension.name }}/packages.yml
include_tasks: extensions/{{ extension }}/packages.yml
loop_control:
loop_var: extension
loop: "{{ enabled_extensions }}"
Expand All @@ -63,12 +73,15 @@
register: pg_hba_conf_stat

- name: RedHat | Initialize database
become_user: postgres
when: not pg_hba_conf_stat.stat.exists
command: /usr/pgsql-13/bin/postgresql-13-setup initdb {{ pg.service_name[ansible_os_family] }}
command: "{{ pg.bindir[ansible_os_family] }}/initdb"
environment:
PGDATA: "{{ pg.data_dir[ansible_os_family] }}"

- name: Ensure that postgresql service is started
- name: Ensure that PostgreSQL service is started
block:
- name: Ensure that postgresql service is started
- name: Ensure that PostgreSQL service is started
systemd:
name: "{{ pg.service_name[ansible_os_family] }}"
state: started
Expand All @@ -86,7 +99,6 @@
- name: Set runtime_shared_preload_libraries fact
block:
- name: Get PostgreSQL settings
become: true
become_user: postgres
postgresql_info:
filter: settings
Expand Down Expand Up @@ -114,25 +126,25 @@
loop: "{{ _files }}"
register: change_pg_config

- name: Restart postgresql service if configuration changed
- name: Restart PostgreSQL service if configuration changed
block:
- name: Restart postgresql service if configuration changed
- name: Restart PostgreSQL service if configuration changed
systemd:
name: "{{ pg.service_name[ansible_os_family] }}"
state: restarted

- *check-debian-service
when: (change_pg_config.results | map(attribute='changed') | list) is any

- name: Configure postgresql logrotate
- name: Configure PostgreSQL logrotate
block:
- name: Remove logrotate configuration provided by 'postgresql-common' package
file:
path: /etc/logrotate.d/postgresql-common
state: absent
when: ansible_os_family == 'Debian'

- name: Create postgresql logrotate configuration file
- name: Create PostgreSQL logrotate configuration file
template:
src: logrotate-postgresql.conf.j2
dest: /etc/logrotate.d/postgresql
Expand All @@ -141,9 +153,9 @@
mode: u=rw,g=r,o=r

- name: Include extensions
include_tasks: extensions/{{ data.name }}/extension.yml
include_tasks: extensions/{{ extension }}/extension.yml
loop_control:
loop_var: data
loop_var: extension
loop: "{{ enabled_extensions }}"

# PgPool is configured only if such K8s application is enabled
Expand All @@ -169,4 +181,4 @@
| default({}) }}
when:
- pgpool.env is defined
- groups['postgresql'][0] == inventory_hostname
- pg_node_id|int == 1
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ conninfo='host={{ ansible_default_ipv4.address }} user={{ specification.extensio
data_directory='{{ pg.data_dir[ansible_os_family] }}'

failover=automatic
promote_command='{{ repmgr.pg_bindir[ansible_os_family] }}/repmgr standby promote --log-to-file'
follow_command='{{ repmgr.pg_bindir[ansible_os_family] }}/repmgr standby follow --log-to-file --upstream-node-id=%n'
promote_command='{{ pg.bindir[ansible_os_family] }}/repmgr standby promote --log-to-file'
follow_command='{{ pg.bindir[ansible_os_family] }}/repmgr standby follow --log-to-file --upstream-node-id=%n'
monitoring_history=yes
monitor_interval_secs=10

log_file='/var/log/postgresql/repmgr.log'

pg_bindir='{{ repmgr.pg_bindir[ansible_os_family] }}/'
pg_bindir='{{ pg.bindir[ansible_os_family] }}/'
service_start_command='sudo /bin/systemctl start {{ pg.service_name[ansible_os_family] }}'
service_stop_command='sudo /bin/systemctl stop {{ pg.service_name[ansible_os_family] }}'
service_restart_command='sudo /bin/systemctl restart {{ pg.service_name[ansible_os_family] }}'
Expand Down

0 comments on commit da57e0c

Please sign in to comment.