Skip to content

Commit

Permalink
Add role to spin up a basic external postgresql database
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed May 12, 2022
1 parent 4a941c0 commit 40b09fd
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions playbooks/postgresql_server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- hosts: all
gather_facts: true
become: true
roles:
- role: update_os_packages
- role: postgresql_scl
when: ansible_distribution_major_version == '7'
- role: external_database
91 changes: 91 additions & 0 deletions roles/external_database/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
- name: "Load OS variables"
include_vars: "{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"

- name: 'Install PostgreSQL packages'
package:
name: "{{ external_database_packages }}"
state: installed

- name: 'Init PostgreSQL database'
command: postgresql-setup initdb
args:
creates: "{{ external_database_postgresql_conf_path }}"

- name: 'Deploy pg_hba.conf'
copy:
dest: "{{ external_database_pg_hba_conf_path }}"
content: |
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all ident
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv4 remote connections:
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 md5
force: yes
mode: 0600
owner: postgres
group: postgres

- name: Set listen addresses to *
lineinfile:
dest: "{{ external_database_postgresql_conf_path }}"
regexp: "^listen_addresses"
line: "listen_addresses = '*'"
state: present
backup: yes

- name: 'Ensure PostgreSQL is running'
service:
name: postgresql
state: restarted
enabled: yes

- name: 'Add database user'
become_user: postgres
postgresql_user:
state: present
name: "foreman"
password: "foreman"

- name: 'Create Foreman database'
become_user: postgres
postgresql_db:
state: present
name: "foreman"
owner: "foreman"

- name: 'Add candlepin database user'
become_user: postgres
postgresql_user:
state: present
name: "candlepin"
password: "candlepin"

- name: 'Create Candlepin database'
become_user: postgres
postgresql_db:
state: present
name: "candlepin"
owner: "candlepin"

- name: 'Add pulp database user'
become_user: postgres
postgresql_user:
state: present
name: "pulp"
password: "pulp"

- name: 'Create Pulp database'
become_user: postgres
postgresql_db:
state: present
name: "pulp"
owner: "pulp"
7 changes: 7 additions & 0 deletions roles/external_database/vars/RedHat-7.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
external_database_packages:
- rh-postgresql12-syspaths
- rh-postgresql12-postgresql-server
- python-psycopg2
external_database_postgresql_conf_path: /var/opt/rh/rh-postgresql12/lib/pgsql/data/postgresql.conf
external_database_pg_hba_conf_path: /var/opt/rh/rh-postgresql12/lib/pgsql/data/pg_hba.conf
6 changes: 6 additions & 0 deletions roles/external_database/vars/RedHat-8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
external_database_packages:
- postgresql-server
- python3-psycopg2
external_database_postgresql_conf_path: /var/lib/pgsql/data/postgresql.conf
external_database_pg_hba_conf_path: /var/lib/pgsql/data/pg_hba.conf

0 comments on commit 40b09fd

Please sign in to comment.