From 4a40f18499127dd1d9991206d583090fb4595d32 Mon Sep 17 00:00:00 2001 From: martinb Date: Fri, 29 May 2020 15:58:04 +0200 Subject: [PATCH] add postgres-cluster-databases --- .../defaults/main.yml | 67 +++++++++++++++++++ .../postgres-cluster-databases/tasks/main.yml | 27 ++++++++ 2 files changed, 94 insertions(+) create mode 100644 roles/postgres-cluster-databases/defaults/main.yml create mode 100644 roles/postgres-cluster-databases/tasks/main.yml diff --git a/roles/postgres-cluster-databases/defaults/main.yml b/roles/postgres-cluster-databases/defaults/main.yml new file mode 100644 index 0000000..0e2d503 --- /dev/null +++ b/roles/postgres-cluster-databases/defaults/main.yml @@ -0,0 +1,67 @@ +--- +# RHEL/CentOS only. Set a repository to use for PostgreSQL installation. +postgresql_enablerepo: "" + +# Set postgresql state when configuration changes are made. Recommended values: +# `restarted` or `reloaded` +postgresql_restarted_state: "restarted" + +postgresql_python_library: python-psycopg2 +postgresql_user: postgres +postgresql_group: postgres + +postgresql_unix_socket_directories: + - /var/run/postgresql + +postgresql_service_state: started +postgresql_service_enabled: true + +# Global configuration options that will be set in postgresql.conf. +postgresql_global_config_options: + - option: unix_socket_directories + value: '{{ postgresql_unix_socket_directories | join(",") }}' + +# Host based authentication (hba) entries to be added to the pg_hba.conf. This +# variable's defaults reflect the defaults that come with a fresh installation. +postgresql_hba_entries: + - {type: local, database: all, user: postgres, auth_method: peer} + - {type: local, database: all, user: all, auth_method: peer} + - {type: host, database: all, user: all, address: '127.0.0.1/32', auth_method: md5} + - {type: host, database: all, user: all, address: '::1/128', auth_method: md5} + +# Debian only. Used to generate the locales used by PostgreSQL databases. +postgresql_locales: + - 'en_US.UTF-8' + +# Databases to ensure exist. +postgresql_databases: [] +# - name: exampledb # required; the rest are optional +# lc_collate: # defaults to 'en_US.UTF-8' +# lc_ctype: # defaults to 'en_US.UTF-8' +# encoding: # defaults to 'UTF-8' +# template: # defaults to 'template0' +# login_host: # defaults to 'localhost' +# login_password: # defaults to not set +# login_user: # defaults to '{{ postgresql_user }}' +# login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories +# port: # defaults to not set +# owner: # defaults to postgresql_user +# state: # defaults to 'present' + +# Users to ensure exist. +postgresql_users: [] +# - name: jdoe #required; the rest are optional +# password: # defaults to not set +# encrypted: # defaults to not set +# priv: # defaults to not set +# role_attr_flags: # defaults to not set +# db: # defaults to not set +# login_host: # defaults to 'localhost' +# login_password: # defaults to not set +# login_user: # defaults to '{{ postgresql_user }}' +# login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories +# port: # defaults to not set +# state: # defaults to 'present' + +# Whether to output user data when managing users. +postgres_users_no_log: true diff --git a/roles/postgres-cluster-databases/tasks/main.yml b/roles/postgres-cluster-databases/tasks/main.yml new file mode 100644 index 0000000..6dd096d --- /dev/null +++ b/roles/postgres-cluster-databases/tasks/main.yml @@ -0,0 +1,27 @@ +--- +- name: Install psycopg2 + pip: + name: psycopg2 + when: not (postgresql_cluster_is_monitor | default('False') | bool) + +- name: Ensure PostgreSQL databases are present. + postgresql_db: + name: "{{ item.name }}" + lc_collate: "{{ item.lc_collate | default('en_US.UTF-8') }}" + lc_ctype: "{{ item.lc_ctype | default('en_US.UTF-8') }}" + encoding: "{{ item.encoding | default('UTF-8') }}" + template: "{{ item.template | default('template0') }}" + login_host: "{{ item.login_host | default('localhost') }}" + login_password: "{{ item.login_password | default(omit) }}" + login_user: "{{ item.login_user | default(postgresql_user) }}" + login_unix_socket: "{{ item.login_unix_socket | default(postgresql_unix_socket_directories[0]) }}" + port: "{{ item.port | default(postgresql_cluster_port) }}" + owner: "{{ item.owner | default(postgresql_user) }}" + state: "{{ item.state | default('present') }}" + with_items: "{{ postgresql_databases }}" + become: true + become_user: "{{ postgresql_user }}" + # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509 + vars: + ansible_ssh_pipelining: true + when: not (postgresql_cluster_is_monitor | default('False') | bool) \ No newline at end of file