commit 8d92aa976a82dfb435ee7217bc969a5138f24b80 Author: lhahn Date: Sun Aug 20 10:31:29 2023 +0200 Git initial commit diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +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. diff --git a/README.md b/README.md new file mode 100755 index 0000000..d9db307 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# cloud-postgresql + +Ansible role to provide PostgreSQL for tools where needed. \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100755 index 0000000..5af67ed --- /dev/null +++ b/defaults/main.yml @@ -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: "" +# | +# \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100755 index 0000000..73b314f --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1 @@ +--- \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml new file mode 100755 index 0000000..89ec236 --- /dev/null +++ b/meta/main.yml @@ -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: [] diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100755 index 0000000..e9a71a4 --- /dev/null +++ b/tasks/main.yml @@ -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 %}" \ No newline at end of file diff --git a/tasks/setup_db.yml b/tasks/setup_db.yml new file mode 100755 index 0000000..05c5616 --- /dev/null +++ b/tasks/setup_db.yml @@ -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' diff --git a/vars/main.yml b/vars/main.yml new file mode 100755 index 0000000..3f7e7ed --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +postgresql_config_path: "/etc/postgresql/{{ postgresql_version_major }}/main"