Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support postgres v9.6 in docker #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ postgresql_ctype_parts:
postgresql_ctype: "{{ postgresql_ctype_parts | join('.') }}"

postgresql_admin_user: "postgres"
# Pwd only needed in docker mode, which uses md5 authentication
postgresql_admin_pwd: ""
postgresql_default_auth_method: "trust"
postgresql_login_host: "localhost"
postgresql_login_port: 5432

# The user/group that will run postgresql process or service
postgresql_service_user: "{{ postgresql_admin_user }}"
Expand Down Expand Up @@ -59,6 +63,7 @@ postgresql_pg_hba_passwd_hosts: []
postgresql_pg_hba_trust_hosts: []
postgresql_pg_hba_custom: []

postgresql_docker_mode: no

# postgresql.conf

Expand All @@ -79,6 +84,9 @@ postgresql_pid_directory: "/var/run/postgresql"
# If external_pid_file is not explicitly set, on extra PID file is written
postgresql_external_pid_file: "{{ postgresql_pid_directory }}/{{postgresql_version}}-{{postgresql_cluster_name}}.pid"

# Path on the host where the docker data directory is mounted (only for docker mode)
postgresql_data_directory_docker_mount: ""

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -671,6 +679,12 @@ postgresql_exit_on_error: off
# Reinitialize after backend crash?
postgresql_restart_after_crash: on

#------------------------------------------------------------------------------
# OTHER OPTIONS
#------------------------------------------------------------------------------

# Additional options in the form of list of dictionaries (key, value)
postgresql_custom_options: []

#------------------------------------------------------------------------------
# PGTUNE
Expand Down
22 changes: 22 additions & 0 deletions tasks/configure-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# file: postgresql/tasks/configure-docker.yml

- name: PostgreSQL | Update configuration - pt. 1 (pg_hba.conf)
template:
src: pg_hba.conf.j2
dest: "{{postgresql_data_directory_docker_mount}}/pg_hba.conf"
mode: 0640
register: postgresql_configuration_pt1

- name: PostgreSQL | Update configuration - pt. 2 (postgresql.conf)
template:
src: "postgresql.conf-{{ postgresql_version }}.j2"
# if using pgtune, save the template to ".untuned"
dest: "{{postgresql_data_directory_docker_mount}}/postgresql.conf{% if postgresql_pgtune %}.untuned{% endif %}"
mode: 0640
register: postgresql_configuration_pt2

- name: PostgreSQL | Create folder for additional configuration files
file:
name: "{{postgresql_data_directory_docker_mount}}/conf.d"
state: directory
mode: 0755
95 changes: 95 additions & 0 deletions tasks/databases-common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---

- name: PostgreSQL | Make sure the PostgreSQL databases are present
postgresql_db:
name: "{{item.name}}"
owner: "{{ item.owner | default(postgresql_database_owner) }}"
encoding: "{{ item.encoding | default(postgresql_encoding) }}"
lc_collate: "{{ item.lc_collate | default(postgresql_locale) }}"
lc_ctype: "{{ item.lc_ctype | default(postgresql_ctype) }}"
port: "{{ (postgresql_docker_mode) | ternary(postgresql_login_port, postgresql_port) }}"
template: "template0"
state: present
login_user: "{{postgresql_admin_user}}"
login_password: "{{ (postgresql_docker_mode) | ternary(postgresql_admin_pwd, omit) }}"
login_host: "{{ (postgresql_docker_mode) | ternary(postgresql_login_host, omit) }}"
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_admin_user) }}"
with_items: "{{postgresql_databases}}"
when: postgresql_databases|length > 0

- name: PostgreSQL | Add extensions to the databases
shell: "psql {{item.0.db}} --username {{postgresql_admin_user}} {{ postgres_login_host_switch }} -c 'CREATE EXTENSION IF NOT EXISTS {{ item.1 }};'"
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_service_user) }}"
with_subelements:
- "{{postgresql_database_extensions}}"
- extensions
register: result
changed_when: "'NOTICE' not in result.stderr"
environment: "{{ postgres_env_switch }}"

- name: PostgreSQL | Add hstore to the databases with the requirement
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_service_user) }}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} {{ postgres_login_host_switch }} -c 'CREATE EXTENSION IF NOT EXISTS hstore;'"
with_items: "{{postgresql_databases}}"
register: hstore_ext_result
failed_when: hstore_ext_result.rc != 0 and ("already exists, skipping" not in hstore_ext_result.stderr)
changed_when: hstore_ext_result.rc == 0 and ("already exists, skipping" not in hstore_ext_result.stderr)
when: item.hstore is defined and item.hstore
environment: "{{ postgres_env_switch }}"

- name: PostgreSQL | Add uuid-ossp to the database with the requirement
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_service_user) }}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} {{ postgres_login_host_switch }} -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'"
with_items: "{{postgresql_databases}}"
register: uuid_ext_result
failed_when: uuid_ext_result.rc != 0 and ("already exists, skipping" not in uuid_ext_result.stderr)
changed_when: uuid_ext_result.rc == 0 and ("already exists, skipping" not in uuid_ext_result.stderr)
when: item.uuid_ossp is defined and item.uuid_ossp
environment: "{{ postgres_env_switch }}"

- name: PostgreSQL | Add postgis to the databases with the requirement
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_service_user) }}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} {{ postgres_login_host_switch }} -c 'CREATE EXTENSION IF NOT EXISTS postgis;'&&psql {{item.name}} --username {{postgresql_admin_user}} {{ postgres_login_host_switch }} -c 'CREATE EXTENSION IF NOT EXISTS postgis_topology;'"
with_items: "{{postgresql_databases}}"
when: item.gis is defined and item.gis
environment: "{{ postgres_env_switch }}"

- name: PostgreSQL | add cube to the database with the requirement
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_service_user) }}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} {{ postgres_login_host_switch }} -c 'create extension if not exists cube;'"
with_items: "{{postgresql_databases}}"
when: item.cube is defined and item.cube
environment: "{{ postgres_env_switch }}"

- name: PostgreSQL | Add plpgsql to the database with the requirement
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_service_user) }}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} {{ postgres_login_host_switch }} -c 'CREATE EXTENSION IF NOT EXISTS plpgsql;'"
with_items: "{{postgresql_databases}}"
when: item.plpgsql is defined and item.plpgsql
environment: "{{ postgres_env_switch }}"

- name: PostgreSQL | add earthdistance to the database with the requirement
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_service_user) }}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} {{ postgres_login_host_switch }} -c 'create extension if not exists earthdistance;'"
with_items: "{{postgresql_databases}}"
when: item.earthdistance is defined and item.earthdistance
environment: "{{ postgres_env_switch }}"

- name: PostgreSQL | Add citext to the database with the requirement
become: yes
become_user: "{{ (postgresql_docker_mode) | ternary('root', postgresql_service_user) }}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} {{ postgres_login_host_switch }} -c 'CREATE EXTENSION IF NOT EXISTS citext;'"
with_items: "{{postgresql_databases}}"
register: citext_ext_result
failed_when: citext_ext_result.rc != 0 and ("already exists, skipping" not in citext_ext_result.stderr)
changed_when: citext_ext_result.rc == 0 and ("already exists, skipping" not in citext_ext_result.stderr)
when: item.citext is defined and item.citext
environment: "{{ postgres_env_switch }}"
9 changes: 9 additions & 0 deletions tasks/databases-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# file: postgresql/tasks/databases-docker.yml

# Prepare facts for the docker configuration
- set_fact:
postgres_login_host_switch: "--host {{postgresql_login_host}} --port {{postgresql_login_port}}"
postgres_env_switch:
PGPASSWORD: "{{postgresql_admin_pwd}}"

- include: databases-common.yml
87 changes: 5 additions & 82 deletions tasks/databases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,9 @@
name: "{{ postgresql_service_name }}"
state: started

- name: PostgreSQL | Make sure the PostgreSQL databases are present
postgresql_db:
name: "{{item.name}}"
owner: "{{ item.owner | default(postgresql_database_owner) }}"
encoding: "{{ item.encoding | default(postgresql_encoding) }}"
lc_collate: "{{ item.lc_collate | default(postgresql_locale) }}"
lc_ctype: "{{ item.lc_ctype | default(postgresql_ctype) }}"
port: "{{postgresql_port}}"
template: "template0"
state: present
login_user: "{{postgresql_admin_user}}"
become: yes
become_user: "{{postgresql_admin_user}}"
with_items: "{{postgresql_databases}}"
when: postgresql_databases|length > 0
# Prepare facts for the non-docker configuration
- set_fact:
postgres_login_host_switch: "--port {{postgresql_port}}"
postgres_env_switch: {}

- name: PostgreSQL | Add extensions to the databases
shell: "psql {{item.0.db}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS {{ item.1 }};'"
become: yes
become_user: "{{postgresql_service_user}}"
with_subelements:
- "{{postgresql_database_extensions}}"
- extensions
register: result
changed_when: "'NOTICE' not in result.stderr"

- name: PostgreSQL | Add hstore to the databases with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS hstore;'"
with_items: "{{postgresql_databases}}"
register: hstore_ext_result
failed_when: hstore_ext_result.rc != 0 and ("already exists, skipping" not in hstore_ext_result.stderr)
changed_when: hstore_ext_result.rc == 0 and ("already exists, skipping" not in hstore_ext_result.stderr)
when: item.hstore is defined and item.hstore

- name: PostgreSQL | Add uuid-ossp to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'"
with_items: "{{postgresql_databases}}"
register: uuid_ext_result
failed_when: uuid_ext_result.rc != 0 and ("already exists, skipping" not in uuid_ext_result.stderr)
changed_when: uuid_ext_result.rc == 0 and ("already exists, skipping" not in uuid_ext_result.stderr)
when: item.uuid_ossp is defined and item.uuid_ossp

- name: PostgreSQL | Add postgis to the databases with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS postgis;'&&psql {{item.name}} -c 'CREATE EXTENSION IF NOT EXISTS postgis_topology;'"
with_items: "{{postgresql_databases}}"
when: item.gis is defined and item.gis

- name: PostgreSQL | add cube to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'create extension if not exists cube;'"
with_items: "{{postgresql_databases}}"
when: item.cube is defined and item.cube

- name: PostgreSQL | Add plpgsql to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'CREATE EXTENSION IF NOT EXISTS plpgsql;'"
with_items: "{{postgresql_databases}}"
when: item.plpgsql is defined and item.plpgsql

- name: PostgreSQL | add earthdistance to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{ postgresql_admin_user }} -c 'create extension if not exists earthdistance;'"
with_items: "{{postgresql_databases}}"
when: item.earthdistance is defined and item.earthdistance

- name: PostgreSQL | Add citext to the database with the requirement
become: yes
become_user: "{{postgresql_service_user}}"
shell: "{{ postgresql_bin_directory}}/psql {{item.name}} --username {{postgresql_admin_user}} -c 'CREATE EXTENSION IF NOT EXISTS citext;'"
with_items: "{{postgresql_databases}}"
register: citext_ext_result
failed_when: citext_ext_result.rc != 0 and ("already exists, skipping" not in citext_ext_result.stderr)
changed_when: citext_ext_result.rc == 0 and ("already exists, skipping" not in citext_ext_result.stderr)
when: item.citext is defined and item.citext
- include: databases-common.yml
18 changes: 15 additions & 3 deletions tasks/install.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# file: postgresql/tasks/install.yml

- name: Define packages to be installed
set_fact:
postgres_packages:
- "postgresql-{{postgresql_version}}"
- "postgresql-client-{{postgresql_version}}"
- "postgresql-contrib-{{postgresql_version}}"
when: not postgresql_docker_mode

- name: Define packages to be installed (docker mode)
set_fact:
postgres_packages:
- "postgresql-client-{{postgresql_version}}"
when: postgresql_docker_mode

# The standard ca-certs are needed because without them apt_key will fail to
# validate www.postgresql.org (or probably any other source).
- name: PostgreSQL | Make sure the CA certificates are available
Expand Down Expand Up @@ -43,9 +57,7 @@
cache_valid_time: "{{apt_cache_valid_time | default (3600)}}"
environment: "{{postgresql_env}}"
with_items:
- "postgresql-{{postgresql_version}}"
- "postgresql-client-{{postgresql_version}}"
- "postgresql-contrib-{{postgresql_version}}"
- "{{ postgres_packages }}"

- name: PostgreSQL | PGTune
apt:
Expand Down
18 changes: 15 additions & 3 deletions tasks/install_yum.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# file: postgresql/tasks/install_yum.yml

- name: Define packages to be installed
set_fact:
postgres_packages:
- "postgresql{{ postgresql_version_terse }}-server"
- "postgresql{{ postgresql_version_terse }}"
- "postgresql{{ postgresql_version_terse }}-contrib"
when: not postgresql_docker_mode

- name: Define packages to be installed (docker mode)
set_fact:
postgres_packages:
- "postgresql{{ postgresql_version_terse }}"
when: postgresql_docker_mode

# The standard ca-certs are needed because without them apt_key will fail to
# validate www.postgresql.org (or probably any other source).
- name: PostgreSQL | Make sure the CA certificates are available
Expand All @@ -25,9 +39,7 @@
state: present
environment: "{{ postgresql_env }}"
with_items:
- "postgresql{{ postgresql_version_terse }}-server"
- "postgresql{{ postgresql_version_terse }}"
- "postgresql{{ postgresql_version_terse }}-contrib"
- "{{ postgres_packages }}"

- name: PostgreSQL | PGTune
yum:
Expand Down
23 changes: 22 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,41 @@
tags: [postgresql, postgresql-install]

- include: extensions.yml
when: not postgresql_docker_mode
tags: [postgresql, postgresql-extensions]

- include: configure.yml
when: not postgresql_docker_mode
tags: [postgresql, postgresql-configure]

- include: configure-docker.yml
when: postgresql_docker_mode
tags: [postgresql, postgresql-configure]

- include: users.yml
when: not postgresql_docker_mode
tags: [postgresql, postgresql-users]

- include: users-docker.yml
when: postgresql_docker_mode
tags: [postgresql, postgresql-users]

- include: databases.yml
when: not postgresql_docker_mode
tags: [postgresql, postgresql-databases]

- include: databases-docker.yml
when: postgresql_docker_mode
tags: [postgresql, postgresql-databases]

- include: users_privileges.yml
when: not postgresql_docker_mode
tags: [postgresql, postgresql-users]

- include: users_privileges-docker.yml
when: postgresql_docker_mode
tags: [postgresql, postgresql-users]

- include: monit.yml
when: monit_protection is defined and monit_protection == true
when: monit_protection is defined and monit_protection == true and not postgresql_docker_mode
tags: [postgresql, postgresql-monit]
14 changes: 14 additions & 0 deletions tasks/users-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# file: postgresql/tasks/users-docker.yml

- name: PostgreSQL | Make sure the PostgreSQL users are present
postgresql_user:
name: "{{item.name}}"
password: "{{ item.pass | default(omit) }}"
encrypted: "{{ item.encrypted | default(omit) }}"
port: "{{postgresql_login_port}}"
state: present
login_user: "{{postgresql_admin_user}}"
login_password: "{{postgresql_admin_pwd}}"
login_host: "{{postgresql_login_host}}"
with_items: "{{postgresql_users}}"
when: postgresql_docker_mode and postgresql_users|length > 0
15 changes: 15 additions & 0 deletions tasks/users_privileges-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# file: postgresql/tasks/users_privileges.yml

- name: PostgreSQL | Update the user privileges
postgresql_user:
name: "{{item.name}}"
db: "{{item.db | default(omit)}}"
port: "{{postgresql_login_port}}"
priv: "{{item.priv | default(omit)}}"
state: present
role_attr_flags: "{{item.role_attr_flags | default(omit)}}"
login_user: "{{postgresql_admin_user}}"
login_host: "{{postgresql_login_host}}"
login_password: "{{postgresql_admin_pwd}}"
with_items: "{{postgresql_user_privileges}}"
when: postgresql_users|length > 0
Loading