pgbouncer/tasks/main.yaml
2024-11-03 17:11:51 +03:00

157 lines
4.9 KiB
YAML

---
- name: Make sure handlers are flushed immediately
meta: flush_handlers
- name: Gather packages
package_facts:
manager: auto
- name: Load a variable file based on the OS type
include_vars: "{{ lookup('first_found', params) }}"
vars:
params:
files:
- "{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_major_version'] }}.yaml"
- "{{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_version'] }}.yaml"
- "{{ ansible_facts['os_family'] }}-{{ ansible_facts['distribution_major_version'] }}.yaml"
- "{{ ansible_facts['os_family'] }}-{{ ansible_facts['distribution_version'] }}.yaml"
- "{{ ansible_facts['distribution'] }}.yaml"
- "{{ ansible_facts['os_family'] }}.yaml"
paths:
- "vars"
tags: pgbouncer_vars
- name: "Set fact about PostgreSQL package"
set_fact:
pgbouncer_postgresql_package_name: "{{ ansible_facts.packages | list | select('match', pgbouncer_postgresql_package_name_regex) | first }}"
- name: "Get PostgreSQL major version"
set_fact:
pgbouncer_postgresql_major_version: "{{ ansible_facts.packages[pgbouncer_postgresql_package_name][0]['version'] | split('.') | first }}"
pgbouncer_postgresql_minor_version: "{{ ansible_facts.packages[pgbouncer_postgresql_package_name][0]['version'] | split('.') | last }}"
- name: Mask pgbouncer before install
systemd:
name: pgbouncer
masked: yes
when:
- ansible_os_family == "Debian"
- "'pgbouncer' not in ansible_facts.packages"
tags: pgbouncer_mask, pgbouncer
- name: Install pgbouncer package
package:
name: pgbouncer
environment: "{{ proxy_env | default({}) }}"
tags: pgbouncer_install, pgbouncer
- name: Ensure config directory "{{ pgbouncer_conf_dir }}" exist
file:
path: "{{ pgbouncer_conf_dir }}"
state: directory
owner: "{{ pgbouncer_systemd_user }}"
group: "{{ pgbouncer_systemd_group }}"
mode: 0750
tags: pgbouncer_conf, pgbouncer
- name: Add pgbouncer systemd unit user and group override
block:
- name: Ensure override dir for pgbouncer exists
file:
path: /etc/systemd/system/pgbouncer.service.d
state: directory
- name: Set user and group for pgbouncer
copy:
dest: /etc/systemd/system/pgbouncer.service.d/override.conf
content: |
[Service]
User=
User={{ pgbouncer_systemd_user }}
Group=
Group={{ pgbouncer_systemd_group }}
LimitNOFILE=1048576
Restart=on-failure
- name: Enable log rotation with logrotate
copy:
content: |
"{{ pgbouncer_log_dir }}/{{ pgbouncer_log_name }}" {
daily
rotate 7
copytruncate
delaycompress
compress
notifempty
missingok
su root root
}
dest: /etc/logrotate.d/pgbouncer
tags: pgbouncer_logrotate, pgbouncer
- name: Configure pgbouncer.ini
template:
src: templates/pgbouncer.ini.j2
dest: "{{ pgbouncer_conf_dir }}/{{ pgbouncer_conf_name }}"
owner: "{{ pgbouncer_systemd_user }}"
group: "{{ pgbouncer_systemd_group }}"
mode: 0640
backup: yes
notify: "restart pgbouncer"
when: existing_pgcluster is not defined or not existing_pgcluster|bool
tags: pgbouncer_conf, pgbouncer
- name: Ensure user and group applied to all files and dirs
file:
path: "{{ item }}"
owner: "{{ pgbouncer_systemd_user }}"
group: "{{ pgbouncer_systemd_group }}"
recurse: yes
state: directory
loop:
- "{{ pgbouncer_conf_dir }}"
- "{{ pgbouncer_log_dir }}"
- name: Ping PostgreSQL server
community.postgresql.postgresql_ping:
db: "{{ pgbouncer_postgresql_db }}"
login_host: "{{ pgbouncer_postgresql_host }}"
login_user: "{{ pgbouncer_postgresql_superuser_username }}"
login_password: "{{ pgbouncer_postgresql_superuser_password }}"
register: postgresql_ping
until: postgresql_ping.is_available
retries: 60
delay: 20
- name: Get auth user {{ pgbouncer_auth_user }} password hash
community.postgresql.postgresql_query:
db: "{{ pgbouncer_postgresql_db }}"
login_host: "{{ pgbouncer_postgresql_host }}"
login_user: "{{ pgbouncer_postgresql_superuser_username }}"
login_password: "{{ pgbouncer_postgresql_superuser_password }}"
query: "{{ pgbouncer_postgresql_auth_query }}"
positional_args: "{{ pgbouncer_auth_user }}"
register: pgbouncer_postgresql_superuser_password_hash
- name: Create auth_file
template:
src: templates/userlist.txt.j2
dest: "{{ pgbouncer_auth_file }}"
owner: "{{ pgbouncer_systemd_user }}"
group: "{{ pgbouncer_systemd_group }}"
mode: 0640
backup: yes
when: existing_pgcluster is not defined or not existing_pgcluster|bool
tags: pgbouncer
- name: Ensure pgbouncer enabled and started
systemd:
daemon_reload: true
name: pgbouncer
enabled: true
state: started
masked: no
register: pgbouncer_enable_and_start
tags: pgbouncer, pgbouncer_start