mirror of
https://gitea.0xace.cc/ansible-galaxy/pgbouncer.git
synced 2025-06-28 18:53:08 +00:00
first commit
This commit is contained in:
174
tasks/main.yaml
Normal file
174
tasks/main.yaml
Normal file
@ -0,0 +1,174 @@
|
||||
---
|
||||
# yamllint disable rule:line-length
|
||||
|
||||
- name: Make sure handlers are flushed immediately
|
||||
meta: flush_handlers
|
||||
|
||||
- name: Load a variable file based on the OS type
|
||||
include_vars: "{{ lookup('first_found', params) }}"
|
||||
vars:
|
||||
params:
|
||||
files:
|
||||
- "{{ ansible_facts['distribution'] }}.yaml"
|
||||
- "{{ ansible_facts['os_family'] }}.yaml"
|
||||
paths:
|
||||
- "vars"
|
||||
tags: pgbouncer_vars
|
||||
|
||||
- name: "Gather packages"
|
||||
package_facts:
|
||||
manager: auto
|
||||
|
||||
- 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 and minor versions"
|
||||
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: Install pgbouncer package
|
||||
package:
|
||||
name: pgbouncer
|
||||
environment: "{{ proxy_env | default({}) }}"
|
||||
when: ansible_os_family == "Debian" or
|
||||
(ansible_os_family == "RedHat" and
|
||||
ansible_distribution_major_version == '7')
|
||||
tags: pgbouncer_install, pgbouncer
|
||||
|
||||
# RHEL 8
|
||||
- name: Install pgbouncer package
|
||||
dnf:
|
||||
name: pgbouncer
|
||||
disablerepo: AppStream
|
||||
environment: "{{ proxy_env | default({}) }}"
|
||||
when: ansible_os_family == "RedHat" and
|
||||
ansible_distribution_major_version >= '8'
|
||||
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: Stop and disable standard init script
|
||||
service:
|
||||
name: pgbouncer
|
||||
state: stopped
|
||||
enabled: false
|
||||
when: ansible_os_family == "Debian"
|
||||
tags: pgbouncer_service, 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 }}
|
||||
|
||||
- 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
|
||||
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 }}"
|
||||
- "{{ pgbouncer_pid_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: postgres
|
||||
group: postgres
|
||||
mode: 0640
|
||||
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
|
||||
|
||||
- name: Ensure user and group applied to all files and dirs one more time
|
||||
file:
|
||||
path: "{{ item }}"
|
||||
owner: "{{ pgbouncer_systemd_user }}"
|
||||
group: "{{ pgbouncer_systemd_group }}"
|
||||
recurse: yes
|
||||
state: directory
|
||||
loop:
|
||||
- "{{ pgbouncer_conf_dir }}"
|
||||
- "{{ pgbouncer_log_dir }}"
|
||||
- "{{ pgbouncer_pid_dir }}"
|
Reference in New Issue
Block a user