Git initial commit

This commit is contained in:
Lars Hahn 2023-09-17 10:56:20 +02:00
commit 69dcd70d68
8 changed files with 185 additions and 0 deletions

9
LICENSE Executable file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3
README.md Executable file
View File

@ -0,0 +1,3 @@
# cloud-postgresql
Ansible role to provide PostgreSQL for tools where needed.

29
defaults/main.yml Executable file
View File

@ -0,0 +1,29 @@
---
cloud_apps: /opt
cloud_storage: /opt/storage
cloud_stage: prod
cloud_update: false
psql_user: postgres
db_configs:
- type: pgsql
name: db_name
user: db_user
pass: db_user_password
priv: all
postgresql_version_major: 15
postgresql_version: "postgresql-{{ postgresql_version_major }}"
postgresql_cluster_name: main
postgresql_data_directory: "/var/lib/postgresql/{{ postgresql_version_major }}/main"
postgresql_listen_addr: "localhost"
# - ip1
# - ip2
# or even "'*'"
postgresql_pghba_network: ""
# |
#

1
handlers/main.yml Executable file
View File

@ -0,0 +1 @@
---

16
meta/main.yml Executable file
View File

@ -0,0 +1,16 @@
---
galaxy_info:
role_name: postgresql
namespace: hahn-cloud
author: Lars Hahn
company: OpenDevChain
license: MIT
description: Role to setup PostgreSQL, that can be used in multiple apps like Modoboa, Redmine or WordPress.
min_ansible_version: 2.7
platforms:
- name: Debian
versions:
- 10
galaxy_tags:
- postgresql
dependencies: []

84
tasks/main.yml Executable file
View File

@ -0,0 +1,84 @@
---
- 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 %}"

41
tasks/setup_db.yml Executable file
View File

@ -0,0 +1,41 @@
---
- name: set DB related config options
set_fact:
local_db_name: "{{ db.name if 'name' in db else 'noentry' }}"
local_db_user: "{{ db.user if 'user' in db else 'noentry'}}"
local_db_pass: "{{ db.pass if 'pass' in db else 'noentry'}}"
local_db_user_privs: "{{ db.priv if 'priv' in db else 'all'}}"
- name: "create {{ local_db_user }} user"
postgresql_user:
state: present
name: "{{ local_db_user }}"
password: "{{ local_db_pass }}"
become: yes
become_user: "{{ psql_user }}"
when:
- local_db_user != 'noentry'
- local_db_pass != 'noentry'
- name: "setup {{ local_db_name }} database"
postgresql_db:
state: present
name: "{{ local_db_name }}"
owner: "{{ local_db_user }}"
become: yes
become_user: "{{ psql_user }}"
when:
- local_db_name != 'noentry'
- name: "Grant {{ local_db_user }} user access to {{ local_db_name }} database"
postgresql_privs:
type: database
database: "{{ local_db_name }}"
roles: "{{ local_db_user }}"
grant_option: yes
privs: "{{ local_db_user_privs }}"
become: yes
become_user: "{{ psql_user }}"
when:
- local_db_name != 'noentry'
- local_db_user != 'noentry'

2
vars/main.yml Executable file
View File

@ -0,0 +1,2 @@
---
postgresql_config_path: "/etc/postgresql/{{ postgresql_version_major }}/main"