From 1a601efca443ae39876c5484c56d322137211b43 Mon Sep 17 00:00:00 2001 From: nihonium Date: Thu, 31 Oct 2024 23:04:36 +0300 Subject: [PATCH] initial commit --- ansible-postgresql/playbook.yml | 5 ++ .../roles/postgresql16/handlers/main.yml | 4 ++ .../roles/postgresql16/tasks/main.yml | 72 +++++++++++++++++++ .../postgresql16/templates/postgresql.conf.j2 | 3 + 4 files changed, 84 insertions(+) create mode 100644 ansible-postgresql/playbook.yml create mode 100644 ansible-postgresql/roles/postgresql16/handlers/main.yml create mode 100644 ansible-postgresql/roles/postgresql16/tasks/main.yml create mode 100644 ansible-postgresql/roles/postgresql16/templates/postgresql.conf.j2 diff --git a/ansible-postgresql/playbook.yml b/ansible-postgresql/playbook.yml new file mode 100644 index 0000000..907daef --- /dev/null +++ b/ansible-postgresql/playbook.yml @@ -0,0 +1,5 @@ +- name: Configure Debian 11, install and launch PostgreSQL 16 + hosts: debian11_servers + become: yes + roles: + - postgresql16 diff --git a/ansible-postgresql/roles/postgresql16/handlers/main.yml b/ansible-postgresql/roles/postgresql16/handlers/main.yml new file mode 100644 index 0000000..15ef8a3 --- /dev/null +++ b/ansible-postgresql/roles/postgresql16/handlers/main.yml @@ -0,0 +1,4 @@ +- name: Restart PostgreSQL 16 + systemd: + name: postgresql + state: restarted diff --git a/ansible-postgresql/roles/postgresql16/tasks/main.yml b/ansible-postgresql/roles/postgresql16/tasks/main.yml new file mode 100644 index 0000000..2def735 --- /dev/null +++ b/ansible-postgresql/roles/postgresql16/tasks/main.yml @@ -0,0 +1,72 @@ +- name: Update apt package index + apt: + update_cache: yes + +- name: Install necessary dependencies + apt: + name: + - wget + - ca-certificates + state: present + +- name: Add PostgreSQL GPG key + apt_key: + url: https://www.postgresql.org/media/keys/ACCC4CF8.asc + state: present + +- name: Add PostgreSQL APT repository + apt_repository: + repo: "deb http://apt.postgresql.org/pub/repos/apt/ {{ ansible_distribution_release }}-pgdg main" + state: present + update_cache: yes + +- name: Install PostgreSQL 16 and its dependencies + apt: + name: + - postgresql-16 + - postgresql-client-16 + - postgresql-contrib + - python3-psycopg2 + state: present + +- name: Install python3-psycopg2 + apt: + name: + - python3-psycopg2 + state: present + +- name: Ensure PostgreSQL is running and enabled on boot + systemd: + name: postgresql + state: started + enabled: yes + +- name: Copy PostgreSQL configuration + template: + src: postgresql.conf.j2 + dest: /etc/postgresql/16/main/postgresql.conf + notify: + - Restart PostgreSQL 16 + +- name: Create a database user + become_user: postgres + postgresql_user: + name: testuser + password: testpassword + state: present + +- name: Create a database + become_user: postgres + postgresql_db: + name: testdb + owner: testuser + state: present + +- name: Grant privileges to the user on the database + become_user: postgres + postgresql_privs: + database: testdb + roles: testuser + objs: ALL_IN_SCHEMA + privs: "ALL" + state: present diff --git a/ansible-postgresql/roles/postgresql16/templates/postgresql.conf.j2 b/ansible-postgresql/roles/postgresql16/templates/postgresql.conf.j2 new file mode 100644 index 0000000..34e9d73 --- /dev/null +++ b/ansible-postgresql/roles/postgresql16/templates/postgresql.conf.j2 @@ -0,0 +1,3 @@ +listen_addresses = '*' +port = 5432 +max_connections = 100