From b2b3396563ac2990a46df61f6ce5db53aa882ef0 Mon Sep 17 00:00:00 2001 From: Sudoix Date: Wed, 8 Jan 2025 10:14:38 +0100 Subject: [PATCH] add postgres installation --- deployment/roles/postgres/tasks/main.yml | 56 +++++++++++++++++++ .../templates/postgres-docker-compose.yaml.j2 | 16 ++++++ 2 files changed, 72 insertions(+) create mode 100644 deployment/roles/postgres/tasks/main.yml create mode 100644 deployment/roles/postgres/templates/postgres-docker-compose.yaml.j2 diff --git a/deployment/roles/postgres/tasks/main.yml b/deployment/roles/postgres/tasks/main.yml new file mode 100644 index 00000000..a688329a --- /dev/null +++ b/deployment/roles/postgres/tasks/main.yml @@ -0,0 +1,56 @@ +--- +# tasks file for postgres +- name: Create postgres docker directory if it doesn't exist + file: + path: "{{ postgres_dir }}" + state: directory + +- name: Install pgsql client + apt: + name: + - postgresql-client + update_cache: true + state: latest + when: ansible_os_family == 'Debian' + +- name: Copy postgres docker compose file + template: + src: postgres-docker-compose.yaml.j2 + dest: "{{ postgres_dir }}/docker-compose.yaml" + +- name: Check if {{ postgres_container_name }} exists + community.docker.docker_container_info: + name: "{{ postgres_container_name }}" + register: postgres_running + +- name: Ask for confirmation to remove {{ postgres_container_name }} + pause: + prompt: "We found a running {{ postgres_container_name }} container. Would you like to remove it? (y/n)" + echo: yes + register: confirmation + when: postgres_running.exists + delegate_to: localhost + run_once: true + +- block: + - name: Stop and remove {{ postgres_container_name }} if confirmed + community.docker.docker_container: + name: "{{ postgres_container_name }}" + state: absent + when: postgres_running.exists and confirmation.user_input | lower in ['y', 'yes'] + + - name: Remove {{ postgres_container_name }} if it exists + community.docker.docker_container: + name: "{{ postgres_container_name }}" + state: absent + when: postgres_running.exists and confirmation.user_input | lower in ['y', 'yes'] + + - name: Prune docker containers + shell: sudo docker container prune -f + when: postgres_running.exists and confirmation.user_input | lower in ['y', 'yes'] + + - name: Start {{ postgres_container_name }} with docker compose + community.docker.docker_compose: + project_src: "{{ postgres_dir }}" + files: + - docker-compose.yaml diff --git a/deployment/roles/postgres/templates/postgres-docker-compose.yaml.j2 b/deployment/roles/postgres/templates/postgres-docker-compose.yaml.j2 new file mode 100644 index 00000000..65046dcd --- /dev/null +++ b/deployment/roles/postgres/templates/postgres-docker-compose.yaml.j2 @@ -0,0 +1,16 @@ +version: '{{ compose_version }}' + +services: + postgres: + container_name: {{ postgres_container_name }} + restart: unless-stopped + hostname: {{ postgres_container_name }} + image: {{ postgres_docker_image }} + ports: + - {{ private_ip }}:{{ postgres_port }}:{{ postgres_port }} + volumes: + - {{ postgres_data_dir }}:/var/lib/postgresql/data + environment: + - POSTGRES_USER={{ postgres_user }} + - POSTGRES_PASSWORD={{ postgres_password }} + - POSTGRES_DB={{ postgres_db }}