Initial commit of PVC Ansible role
This commit is contained in:
128
roles/pvc/tasks/patroni.yml
Normal file
128
roles/pvc/tasks/patroni.yml
Normal file
@ -0,0 +1,128 @@
|
||||
---
|
||||
- name: install patroni packages via apt
|
||||
apt:
|
||||
name:
|
||||
- python-psycopg2
|
||||
- python3-kazoo
|
||||
- patroni
|
||||
- postgresql-11
|
||||
state: latest
|
||||
update-cache: yes
|
||||
|
||||
- name: first run check
|
||||
shell: "echo 'bootstrapped' > /etc/postgresql/pvc"
|
||||
register: newinstance
|
||||
args:
|
||||
creates: /etc/postgresql/pvc
|
||||
|
||||
- name: stop and disable postgresql
|
||||
service:
|
||||
name: "{{ item }}"
|
||||
state: stopped
|
||||
enabled: no
|
||||
with_items:
|
||||
- postgresql
|
||||
- postgresql@11-main
|
||||
when: newinstance.changed
|
||||
|
||||
- name: remove obsolete database directories
|
||||
file:
|
||||
dest: "{{ item }}"
|
||||
state: absent
|
||||
with_items:
|
||||
- /etc/postgresql/11
|
||||
- /var/lib/postgresql/11
|
||||
when: newinstance.changed
|
||||
|
||||
- name: create patroni database directory
|
||||
file:
|
||||
dest: /var/lib/postgresql/patroni/pvc
|
||||
state: directory
|
||||
owner: postgres
|
||||
mode: 0700
|
||||
when: newinstance.changed
|
||||
|
||||
- name: install postgresql customization configuration file
|
||||
template:
|
||||
src: patroni/postgresql.pvc.conf.j2
|
||||
dest: /etc/postgresql/postgresql.pvc.conf
|
||||
owner: postgres
|
||||
group: sudo
|
||||
mode: 0640
|
||||
notify: restart patroni
|
||||
|
||||
- name: install patroni configuration file
|
||||
template:
|
||||
src: patroni/patroni.yml.j2
|
||||
dest: /etc/patroni/config.yml
|
||||
owner: postgres
|
||||
group: postgres
|
||||
mode: 0640
|
||||
notify: restart patroni
|
||||
|
||||
- name: install check_mk agent check
|
||||
copy:
|
||||
src: patroni/postgres
|
||||
dest: /usr/lib/check_mk_agent/plugins/postgres
|
||||
mode: 0755
|
||||
|
||||
- name: ensure patroni services are enabled and started
|
||||
service:
|
||||
name: "{{ item }}.service"
|
||||
state: started
|
||||
enabled: yes
|
||||
with_items:
|
||||
- patroni
|
||||
|
||||
- name: install initial schema files
|
||||
copy:
|
||||
src: "{{ item.src }}"
|
||||
dest: "{{ item.dest }}"
|
||||
owner: postgres
|
||||
group: sudo
|
||||
mode: 0640
|
||||
with_items:
|
||||
- { src: "patroni/powerdns-schema.sql", dest: "/etc/postgresql/powerdns-schema.sql" }
|
||||
|
||||
- name: set up PVC DNS database on first host
|
||||
block:
|
||||
- name: wait 15s for cluster to initialize
|
||||
pause:
|
||||
seconds: 15
|
||||
|
||||
- name: create user for role
|
||||
postgresql_user:
|
||||
name: "{{ pvc_dns_database_user }}"
|
||||
password: "{{ pvc_dns_database_password }}"
|
||||
state: present
|
||||
login_host: /run/postgresql
|
||||
|
||||
- name: create database for role
|
||||
postgresql_db:
|
||||
name: "{{ pvc_dns_database_name }}"
|
||||
owner: "{{ pvc_dns_database_user }}"
|
||||
encoding: utf8
|
||||
state: present
|
||||
login_host: /run/postgresql
|
||||
|
||||
- name: set user privs for role
|
||||
postgresql_user:
|
||||
name: "{{ pvc_dns_database_user }}"
|
||||
db: "{{ pvc_dns_database_name }}"
|
||||
priv: ALL
|
||||
login_host: /run/postgresql
|
||||
|
||||
- name: create extensions
|
||||
postgresql_ext:
|
||||
name: "{{ item }}"
|
||||
db: "{{ pvc_dns_database_name }}"
|
||||
login_host: /run/postgresql
|
||||
with_items: "{{ extensions }}"
|
||||
when: extensions is defined
|
||||
|
||||
- name: import dns database schema
|
||||
command: "psql -U {{ pvc_dns_database_user }} -f /etc/postgresql/powerdns-schema.sql {{ pvc_dns_database_name }}"
|
||||
|
||||
become: yes
|
||||
become_user: postgres
|
||||
when: newinstance.changed and ansible_local.host_id == '1'
|
Reference in New Issue
Block a user