From f41b2d1198d4443dc19fccb371e139ff2a745250 Mon Sep 17 00:00:00 2001 From: transcaffeine Date: Thu, 14 Nov 2024 18:38:38 +0100 Subject: [PATCH] feat(postgresql): add ansible role for postgresql deployment --- README.md | 3 ++ galaxy.yml | 2 ++ roles/postgresql/README.md | 3 ++ roles/postgresql/defaults/main/container.yml | 38 ++++++++++++++++++++ roles/postgresql/defaults/main/main.yml | 6 ++++ roles/postgresql/defaults/main/user.yml | 10 ++++++ roles/postgresql/meta/main.yml | 12 +++++++ roles/postgresql/tasks/deploy-docker.yml | 23 ++++++++++++ roles/postgresql/tasks/main.yml | 28 +++++++++++++++ roles/postgresql/vars/main.yml | 6 ++++ 10 files changed, 131 insertions(+) create mode 100644 roles/postgresql/README.md create mode 100644 roles/postgresql/defaults/main/container.yml create mode 100644 roles/postgresql/defaults/main/main.yml create mode 100644 roles/postgresql/defaults/main/user.yml create mode 100644 roles/postgresql/meta/main.yml create mode 100644 roles/postgresql/tasks/deploy-docker.yml create mode 100644 roles/postgresql/tasks/main.yml create mode 100644 roles/postgresql/vars/main.yml diff --git a/README.md b/README.md index 6195bf7..27876af 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ - [`mariadb`](roles/mariadb/README.md): deploy mariadb in a docker container +- [`postgresql`](roles/postgresql/README.md): deploy postgresql, + the worlds most advances open-source relational database + - [`valkey`](roles/valkey/README.md): deploy and configure valkey, an open source in-memory data store under BSD license, forked from redis. diff --git a/galaxy.yml b/galaxy.yml index 5f215ec..e4c1cf3 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -16,5 +16,7 @@ tags: - elasticsearch - redis - mariadb + - postgresql + - postgres - valkey - docker diff --git a/roles/postgresql/README.md b/roles/postgresql/README.md new file mode 100644 index 0000000..75877a3 --- /dev/null +++ b/roles/postgresql/README.md @@ -0,0 +1,3 @@ +# `finallycoffee.databases.postgresql` ansible role + +TODO! diff --git a/roles/postgresql/defaults/main/container.yml b/roles/postgresql/defaults/main/container.yml new file mode 100644 index 0000000..3ffbc95 --- /dev/null +++ b/roles/postgresql/defaults/main/container.yml @@ -0,0 +1,38 @@ +--- +postgresql_container_image_registry: docker.io +postgresql_container_image_namespace: ~ +postgresql_container_image_name: postgres +postgresql_container_image_tag: ~ +postgresql_container_image_source: pull +postgresql_container_image_force_source: >-2 + {{ postgresql_container_image_tag | default(false, true) | bool }} +postgresql_container_image: >-2 + {{ + ([ + postgresql_container_image_registry | default([], true), + postgresql_container_image_namespace | default([], true), + postgresql_container_image_name + ] | flatten | join('/')) + + ':' + postgresql_container_image_tag | default( + postgresql_version + ( + ((postgresql_container_image_flavour is string) + and (postgresql_container_image_flavour | length > 0)) + | ternary( + '_' + postgresql_container_image_flavour | default('', true), + '', + ) + ) + ) + }} + +postgresql_container_name: postgresql +postgresql_container_env: ~ +postgresql_container_user: >-2 + {{ postgresql_user_id }}:{{ postgresql_user_group_id }} +postgresql_container_ports: ~ +postgresql_container_labels: ~ +postgresql_container_networks: ~ +postgresql_container_recreate: ~ +postgresql_container_restart_policy: "on-failure" +postgresql_container_state: >-2 + {{ (postgresql_state == 'present') | ternary('started', 'absent') }} diff --git a/roles/postgresql/defaults/main/main.yml b/roles/postgresql/defaults/main/main.yml new file mode 100644 index 0000000..59c5b28 --- /dev/null +++ b/roles/postgresql/defaults/main/main.yml @@ -0,0 +1,6 @@ +--- +postgresql_user: postgresql +postgresql_version: "17.0" + +postgresql_state: present +postgresql_deployment_method: docker diff --git a/roles/postgresql/defaults/main/user.yml b/roles/postgresql/defaults/main/user.yml new file mode 100644 index 0000000..d28fa5a --- /dev/null +++ b/roles/postgresql/defaults/main/user.yml @@ -0,0 +1,10 @@ +--- +postgresql_user_system: true +postgresql_user_create_home: false +postgresql_user_groups: ~ +postgresql_user_append: ~ + +postgresql_user_id: >-2 + {{ postgresql_user_info.uid | default(postgresql_user, true) }} +postgresql_user_group_id: >-2 + {{ postgresql_user_info.group | default(postgresql_user, true) }} diff --git a/roles/postgresql/meta/main.yml b/roles/postgresql/meta/main.yml new file mode 100644 index 0000000..2eb937c --- /dev/null +++ b/roles/postgresql/meta/main.yml @@ -0,0 +1,12 @@ +--- +allow_duplicates: true +dependencies: [] +galaxy_info: + role_name: postgresql + description: >-2 + PostgreSQL is the self-proclaimed 'worlds most advanced' open source relational database + galaxy_tags: + - postgresql + - postgres + - database + - docker diff --git a/roles/postgresql/tasks/deploy-docker.yml b/roles/postgresql/tasks/deploy-docker.yml new file mode 100644 index 0000000..41e63fe --- /dev/null +++ b/roles/postgresql/tasks/deploy-docker.yml @@ -0,0 +1,23 @@ +--- +- name: Ensure postgresql container image '{{ postgresql_container_image }}' is {{ postgresql_state }} + community.docker.docker_image: + name: "{{ postgresql_container_image }}" + state: "{{ postgresql_state }}" + source: "{{ postgresql_container_image_source }}" + force_source: "{{ postgresql_container_image_force_source }}" + register: postgresql_container_image_info + until: postgresql_container_image_info is success + retries: 5 + delay: 4 + +- name: Ensure postgresql container '{{ postgresql_container_name }}' is {{ postgresql_container_state }} + community.docker.docker_container: + name: "{{ postgresql_container_name }}" + image: "{{ postgresql_container_image }}" + env: "{{ postgresql_container_env | default(omit, true) }}" + user: "{{ postgresql_container_user | default(omit, true) }}" + ports: "{{ postgresql_container_ports | default(omit, true) }}" + labels: "{{ postgresql_container_labels | default(omit, true) }}" + networks: "{{ postgresql_container_networks | default(omit, true) }}" + restart_policy: "{{ postgresql_container_restart_policy | default(omit, true) }}" + state: "{{ postgresql_container_state }}" diff --git a/roles/postgresql/tasks/main.yml b/roles/postgresql/tasks/main.yml new file mode 100644 index 0000000..5b77b41 --- /dev/null +++ b/roles/postgresql/tasks/main.yml @@ -0,0 +1,28 @@ +--- +- name: Ensure state is valid + fail: + msg: >-2 + Invalid state '{{ postgresql_state }}'! Supported + states are {{ postgresql_states | join(', ') }}. + when: postgresql_state not in postgresql_states + +- name: Ensure deployment method is valid + fail: + msg: >-2 + Unsupported deployment method '{{ postgresql_deployment_method }}! + Supported deployment methods are {{ postgresql_deployment_methods | join(', ') }}. + when: postgresql_deployment_method not in postgresql_deployment_methods + +- name: Ensure postgresql user '{{ postgresql_user }}' is {{ postgresql_state }} + ansible.builtin.user: + name: "{{ postgresql_user }}" + state: "{{ postgresql_state }}" + system: "{{ postgresql_user_system | default(omit, true) }}" + create_home: "{{ postgresql_user_create_home | default(omit, true) }}" + groups: "{{ postgresql_user_groups | default(omit, true) }}" + append: "{{ postgresql_user_append | default(omit, true) }}" + register: postgresql_user_info + +- name: Deploy postgresql using {{ postgresql_deployment_method }} + ansible.builtin.include_tasks: + file: "deploy-{{ postgresql_deployment_method }}.yml" diff --git a/roles/postgresql/vars/main.yml b/roles/postgresql/vars/main.yml new file mode 100644 index 0000000..ed8f50a --- /dev/null +++ b/roles/postgresql/vars/main.yml @@ -0,0 +1,6 @@ +--- +postgresql_states: + - present + - absent +postgresql_deployment_methods: + - docker