Skip to content

Commit

Permalink
feat(postgresql): prevent major version updates
Browse files Browse the repository at this point in the history
By default the role will now update PostgreSQL to the latest available minor
version, when only a major version is given, or the major version of
postgresql_container_version does not match the major version of the currently
deployed container.
  • Loading branch information
sebastian-de committed Feb 26, 2024
1 parent e78876d commit a4523ca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion roles/postgresql/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ postgresql_socket_path: "{{ postgresql_base_path }}/sockets"
postgresql_config_path: "{{ postgresql_base_path }}/config"
postgresql_connect_socket: true

postgresql_container_version: "13.11"
postgresql_container_version: "13"
postgresql_container_distro: "alpine"

postgresql_container_image_reference: >-
Expand Down
59 changes: 51 additions & 8 deletions roles/postgresql/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
register: "postgresql_user_res"
tags: ["prepare", "prepare-postgresql", "deploy", "deploy-postgresql"]

- name: "Ensure PostgreSQL container image is pulled"
community.docker.docker_image:
name: "{{ postgresql_container_image_reference }}"
force_source: "{{ postgresql_container_pull }}"
source: "pull"
state: "present"
tags: ["prepare", "prepare-postgresql"]

- name: "Check if postgresql_data_path exists"
stat:
path: "{{ postgresql_data_path }}"
Expand All @@ -29,6 +21,57 @@
register: "find_postgresql_data_path"
tags: ["prepare", "prepare-postgresql"]

- name: "Check if container {{ postgresql_container_name }} already exists on host"
community.docker.docker_container_info:
name: "{{ postgresql_container_name }}"
register: "postgresql_active_container"
tags: ["prepare", "prepare-postgresql"]

- name: "Set postgresql_active_container_major_version"
ansible.builtin.set_fact:
postgresql_active_container_major_version: >-2
{{
postgresql_active_container.container.Config.Env
| select('search', '^PG_MAJOR=')
| first
| split('=')
| last
}}
when: "postgresql_active_container.exists"
tags: ["prepare", "prepare-postgresql"]

- name: "Prevent major version update, use major version of existing container instead"
ansible.builtin.set_fact:
postgresql_container_version: "{{ postgresql_active_container_major_version }}"
when:
- "postgresql_active_container.exists"
- "(postgresql_container_version | int) != (postgresql_active_container_major_version | int)"
tags: ["prepare", "prepare-postgresql"]

- name: "Ensure image {{ postgresql_container_image_reference }} is pulled"
community.docker.docker_image:
name: "{{ postgresql_container_image_reference }}"
force_source: "{{ postgresql_container_pull }}"
source: "pull"
state: "present"
register: "postgresql_image_pulled"
tags: ["prepare", "prepare-postgresql"]

- name: "Set container version label to exact version of pulled image"
ansible.builtin.set_fact:
postgresql_container_labels_base:
version: >-2
{{
postgresql_image_pulled.image.Config.Env
| select('search', '^PG_VERSION=')
| first
| split('=')
| last
| string
}}
when: "postgresql_image_pulled.image.Config is defined"
tags: ["prepare", "prepare-postgresql"]

- name: "Initialize"
include_tasks: "initialize.yml"
when: >-
Expand Down

0 comments on commit a4523ca

Please sign in to comment.