homelab/roles/postgresql/tasks/main.yml

65 lines
1.8 KiB
YAML
Raw Normal View History

2024-09-29 18:04:34 +05:00
---
- name: Create docker network
become: true
community.docker.docker_network:
name: postgresql
- name: Create docker volume
become: true
community.docker.docker_volume:
name: postgresql
2024-10-02 00:09:41 +05:00
- name: Copy postgresql config
become: true
ansible.builtin.copy:
src: postgresql.conf
dest: /var/lib/docker/volumes/postgresql/_data/postgresql.conf
mode: '0664'
owner: root
group: root
register: pgconf
2024-09-29 18:04:34 +05:00
- name: Create postgresql container
become: true
community.docker.docker_container:
name: postgresql
2024-10-30 11:08:12 +05:00
image: git.comfycamp.space/lumin/postgres:16.4-bookworm
2024-09-29 18:04:34 +05:00
networks:
- name: postgresql
volumes:
- postgresql:/var/lib/postgresql/data
env:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
POSTGRES_PASSWORD: "{{ users.postgres }}"
2024-09-29 18:04:34 +05:00
restart_policy: unless-stopped
2024-10-02 00:09:41 +05:00
recreate: "{{ pgconf.changed }}"
2024-09-29 18:04:34 +05:00
- name: Create a function to manage users
become: true
community.docker.docker_container_exec:
container: postgresql
argv:
- /bin/bash
- "-c"
- echo "{{ lookup('file', 'create-user.sql') }}" | psql -U postgres
- name: Create roles
become: true
community.docker.docker_container_exec:
container: postgresql
argv:
- /bin/bash
- "-c"
- echo "SELECT create_user_if_not_exists('{{ item.key }}', '{{ item.value }}')" | psql -U postgres
no_log: True
loop:
"{{ users | dict2items }}"
- name: Create databases
become: true
community.docker.docker_container_exec:
container: postgresql
argv: ["sh", "-c", "createdb {{ item.key }} -O {{ item.key }} -U postgres || exit 0"]
no_log: True
loop: "{{ users | dict2items }}"
2024-10-15 21:56:15 +05:00
- name: Grant pg_monitor role to exporter
become: true
community.docker.docker_container_exec:
container: postgresql
argv: ["sh", "-c", "echo 'GRANT pg_monitor TO exporter' | psql -U postgres"]