homelab/roles/postgresql/tasks/main.yml

53 lines
1.4 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
image: postgres:16.4-bookworm
networks:
- name: postgresql
volumes:
- postgresql:/var/lib/postgresql/data
env:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
POSTGRES_PASSWORD: "{{ postgres_password }}"
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 }}"