84 lines
2.6 KiB
YAML
Executable File
84 lines
2.6 KiB
YAML
Executable File
---
|
|
- name: install requirements for postgresql
|
|
apt:
|
|
update_cache: yes
|
|
state: "{% if cloud_update | bool %}latest{% else %}present{% endif %}"
|
|
install_recommends: yes
|
|
pkg:
|
|
- apt-transport-https
|
|
- ca-certificates
|
|
- gnupg2
|
|
- software-properties-common
|
|
- curl
|
|
|
|
- name: setup PostgreSQL repository
|
|
copy:
|
|
content: "deb http://apt.postgresql.org/pub/repos/apt {{ ansible_lsb.codename }}-pgdg main"
|
|
dest: "/etc/apt/sources.list.d/pgdg.list"
|
|
|
|
- name: setup PostgreSQL repository signing key
|
|
apt_key:
|
|
url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
|
|
state: present
|
|
|
|
- name: install PostgreSQL packages
|
|
apt:
|
|
name: "{{ pkg }}"
|
|
state: "{% if cloud_update | bool %}latest{% else %}present{% endif %}"
|
|
install_recommends: yes
|
|
update_cache: yes
|
|
loop:
|
|
- "{{ postgresql_version }}"
|
|
loop_control:
|
|
loop_var: pkg
|
|
label: "{{ pkg }}"
|
|
|
|
- name: adjust postgresql configuration
|
|
lineinfile:
|
|
path: "{{ postgresql_config_path }}/postgresql.conf"
|
|
regexp: '^#?\s*{{ item.opt }}\s*='
|
|
line: "{{ item.opt }} = '{{ item.val }}'"
|
|
backrefs: yes
|
|
loop:
|
|
- opt: data_directory
|
|
val: "{{ postgresql_data_directory }}"
|
|
- opt: listen_addresses
|
|
val: "{% if postgresql_listen_addr is not string and postgresql_listen_addr is iterable %}{{ postgresql_listen_addr | join(',') }}{% else %}{{ postgresql_listen_addr }}{% endif %}"
|
|
loop_control:
|
|
label: "{{ item.opt}} = '{{ item.val }}'"
|
|
register: postgres_config_change
|
|
|
|
- name: adjust pg_hba.conf
|
|
blockinfile:
|
|
path: "{{ postgresql_config_path }}/pg_hba.conf"
|
|
block: "{{ postgresql_pghba_network }}"
|
|
register: postgres_config_change_pg_hba
|
|
when: postgresql_pghba_network | length > 0
|
|
|
|
- name: setup datalocation
|
|
file:
|
|
state: directory
|
|
path: "{{ postgresql_data_directory }}"
|
|
owner: postgres
|
|
group: postgres
|
|
mode: 0750
|
|
register: postgres_db_location
|
|
|
|
- name: setup new database cluster
|
|
command:
|
|
cmd: "pg_createcluster -d {{ postgresql_data_directory }} {{ postgresql_version_major }} {{ postgresql_cluster_name }}"
|
|
creates: "{{ postgresql_data_directory }}/base"
|
|
when: postgres_db_location.changed and postgres_config_change.changed
|
|
|
|
- name: restart postgresql
|
|
systemd:
|
|
name: postgresql
|
|
state: restarted
|
|
when: postgres_config_change.changed or postgres_config_change_pg_hba.changed
|
|
|
|
- name: Setup databases based on PostgreSQL conf
|
|
include_tasks: setup_db.yml
|
|
loop: "{{ db_configs | json_query('[?type==`pgsql`]') }}"
|
|
loop_control:
|
|
loop_var: db
|
|
label: "{% if 'dbname' in db %}{{ db.dbname }}{% elif 'dbuser' in db %}{{ db.dbuser }}{% else %}::pass_redacted::{% endif %}" |