Skip to content

Commit

Permalink
[NEW added postgresql installation ansible playbook
Browse files Browse the repository at this point in the history
  • Loading branch information
ismoilovdevml committed Sep 9, 2024
1 parent d3126b9 commit 0d36923
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Ansible/postgresql/inventory.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[postgresql_servers]
server1 ansible_host=34.69.104.233 ansible_user=ismoilovdev
server2 ansible_host=34.27.32.115 ansible_user=ismoilovdev
server3 ansible_host=34.170.180.55 ansible_user=ismoilovdev
5 changes: 5 additions & 0 deletions Ansible/postgresql/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
postgresql_version: 16
postgresql_password: "oasjofdjqpwe"
listen_address: "*" # Postgresql.conf'da kiritiladi
expose_address: "0.0.0.0/0" # pg_hba.conf'da kiritiladi
22 changes: 22 additions & 0 deletions Ansible/postgresql/postgresql_install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
- name: Install and configure PostgreSQL
hosts: postgresql_servers
become: yes
vars_files:
- main.yml

tasks:
- name: Detect operating system family
set_fact:
os_family: "{{ ansible_os_family }}"

- name: Include Debian-based tasks if OS family is Debian
include_tasks: tasks/install_postgresql_debian.yml
when: ansible_os_family == "Debian"

- name: Include Red Hat-based tasks if OS family is RedHat
include_tasks: tasks/install_postgresql_redhat.yml
when: ansible_os_family == "RedHat"

- name: Configure PostgreSQL after installation
include_tasks: tasks/configure_postgresql.yml
22 changes: 22 additions & 0 deletions Ansible/postgresql/tasks/configure_postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
- name: Set PostgreSQL user password using psql as root
become: yes
shell: su - postgres -c "psql -c \"ALTER USER postgres PASSWORD '{{ postgresql_password }}';\""

- name: Configure PostgreSQL to listen on all interfaces
lineinfile:
path: "/etc/postgresql/{{ postgresql_version }}/main/postgresql.conf"
regexp: "^#listen_addresses ="
line: "listen_addresses = '{{ listen_address }}'"
state: present

- name: Add pg_hba.conf rules
lineinfile:
path: "/etc/postgresql/{{ postgresql_version }}/main/pg_hba.conf"
line: "host all all {{ expose_address }} md5"
state: present

- name: Restart PostgreSQL service
systemd:
name: "postgresql"
state: restarted
38 changes: 38 additions & 0 deletions Ansible/postgresql/tasks/install_postgresql_debian.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
- name: Install prerequisites on Debian-based systems
apt:
name:
- curl
- ca-certificates
- postgresql-common
state: present
update_cache: yes

# - name: Run PostgreSQL repository setup script
# command: /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

- name: Create directory for PostgreSQL keys
file:
path: /usr/share/postgresql-common/pgdg
state: directory
mode: '0755'

- name: Download PostgreSQL GPG key
command: >
curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
- name: Add PostgreSQL repository
shell: |
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
- name: Update apt and install PostgreSQL (latest version)
apt:
name: postgresql
state: present
update_cache: yes

- name: Enable and start PostgreSQL service
systemd:
name: postgresql
enabled: yes
state: started
32 changes: 32 additions & 0 deletions Ansible/postgresql/tasks/install_postgresql_redhat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- name: Install PostgreSQL repository on Red Hat-based systems
yum:
name: "https://download.postgresql.org/pub/repos/yum/reporpms/EL-{{ ansible_distribution_major_version }}-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
state: present

- name: Disable default PostgreSQL module on Red Hat 8 or higher
when: ansible_distribution_major_version | int >= 8
shell: dnf -qy module disable postgresql

- name: Install PostgreSQL server on Red Hat-based systems
yum:
name: "postgresql{{ postgresql_version }}-server"
state: present

- name: Initialize PostgreSQL database on Red Hat-based systems
command: >
/usr/pgsql-{{ postgresql_version }}/bin/postgresql-{{ postgresql_version }}-setup initdb
when: ansible_distribution_major_version | int >= 7

- name: Enable and start PostgreSQL service on Red Hat-based systems
systemd:
name: "postgresql-{{ postgresql_version }}"
enabled: yes
state: started

- name: Enable PostgreSQL service for RHEL/CentOS 6
when: ansible_distribution_major_version | int == 6
service:
name: "postgresql-{{ postgresql_version }}"
enabled: yes
state: started

0 comments on commit 0d36923

Please sign in to comment.