first version of ansible role for postgresql

This commit is contained in:
Alexander Gabriel 2022-09-03 18:14:47 +02:00
parent 35b54737ce
commit 7aaab1e4b0
2 changed files with 53 additions and 1 deletions

View File

@ -1,2 +1,12 @@
# ansible-role-postgresql # Ansible Role PostgreSQL
Ansible Role to install postgresql and create database with user
Can be used to be included inside other roles to create databases.
Variables:
* postgresql_database -> Name of the database
* postgresql_password -> Name of the database
* postgresql_username -> Name of the database user
Variables have no defaults (imagine a default pw which would be everywhere!!!) and role will not do anything if not all variables given

42
tasks/main.yaml Normal file
View File

@ -0,0 +1,42 @@
---
- name: Display Message when variables not defined
debug:
msg: "Please define postgresql_database, postgresql_username AND postgresql_password and none of them empty"
when: postgresql_database is not defined or postgresql_database == "" or postgresql_username is not defined or postgresql_username == "" or postgresql_password is not defined or postgresql_password == ""
- name: Check for variables
ansible.builtin.command: /bin/false
when: postgresql_database is not defined or postgresql_database == "" or postgresql_username is not defined or postgresql_username == "" or postgresql_password is not defined or postgresql_password == ""
- name: Install postgresql and ansible-dependencies
apt:
name:
- postgresql
- python3-psycopg2
update_cache: yes
- name: Create database
community.postgresql.postgresql_db:
name: "{{ postgresql_database }}"
encoding: UTF-8
lc_collate: C
lc_ctype: C
template: template0
become_user: postgres
- name: create database user
community.postgresql.postgresql_user:
db: "{{ postgresql_database }}"
name: "{{ postgresql_username }}"
password: "{{ postgresql_password }}"
expires: infinity
become_user: postgres
- name: Grant all privs to user to database
community.postgresql.postgresql_privs:
db: "{{ postgresql_database }}"
privs: ALL
type: database
obj: "{{ postgresql_database }}"
role: "{{ postgresql_username }}"
become_user: postgres