--- # Upgrade Posgres (Managed Databases only) # * If postgres version is not 12, and not an external postgres instance (when managed_database is yes), # then run this playbook with include_tasks from database_configuration.yml # * Data will be streamed via a pg_dump from the postgres 12 pod to the postgres 13 # pod via a pg_restore. - name: Scale down Deployment for migration include_tasks: scale_down_deployment.yml - name: Delete existing postgres configuration secret k8s: api_version: v1 kind: Secret name: "{{ ansible_operator_meta.name }}-postgres-configuration" namespace: "{{ ansible_operator_meta.namespace }}" state: absent wait: yes - name: Create Database configuration with new -postgres-{{ supported_pg_version }} hostname k8s: apply: true definition: "{{ lookup('template', 'secrets/postgres_upgrade_secret.yaml.j2') }}" no_log: "{{ no_log }}" - name: Set new database var to be used when configuring app credentials (resources_configuration.yml) set_fact: awx_postgres_host: "{{ ansible_operator_meta.name }}-postgres-{{ supported_pg_version }}" no_log: "{{ no_log }}" - name: Create Database if no database is specified k8s: apply: true definition: "{{ lookup('template', 'statefulsets/postgres.yaml.j2') }}" wait: true register: create_statefulset_result - name: Set postgres label if not defined by user set_fact: postgres_label_selector: "app.kubernetes.io/instance=postgres-{{ supported_pg_version }}-{{ ansible_operator_meta.name }}" when: postgres_label_selector is not defined - name: Get new postgres pod information k8s_info: kind: Pod namespace: "{{ ansible_operator_meta.namespace }}" label_selectors: - "{{ postgres_label_selector }}" field_selectors: - status.phase=Running register: postgres_pod until: - "postgres_pod['resources'] | length" - "postgres_pod['resources'][0]['status']['phase'] == 'Running'" - "postgres_pod['resources'][0]['status']['containerStatuses'][0]['ready'] == true" delay: 5 retries: 60 - name: Set the resource pod name as a variable. set_fact: postgres_pod_name: "{{ postgres_pod['resources'][0]['metadata']['name'] }}" - name: Set full resolvable host name for postgres pod set_fact: resolvable_db_host: "{{ ansible_operator_meta.name }}-postgres.{{ ansible_operator_meta.namespace }}.svc" # yamllint disable-line rule:line-length no_log: "{{ no_log }}" - name: Set pg_dump command set_fact: pgdump: >- pg_dump -h {{ resolvable_db_host }} -U {{ awx_postgres_user }} -d {{ awx_postgres_database }} -p {{ awx_postgres_port }} -F custom no_log: "{{ no_log }}" - name: Set pg_restore command set_fact: pg_restore: >- pg_restore -U {{ awx_postgres_user }} -d {{ awx_postgres_database }} no_log: "{{ no_log }}" - name: Stream backup from pg_dump to the new postgresql container k8s_exec: namespace: "{{ ansible_operator_meta.namespace }}" pod: "{{ postgres_pod_name }}" command: | bash -c """ set -e -o pipefail PGPASSWORD='{{ awx_postgres_pass }}' {{ pgdump }} | PGPASSWORD='{{ awx_postgres_pass }}' {{ pg_restore }} echo 'Successful' """ no_log: "{{ no_log }}" register: data_migration failed_when: "'Successful' not in data_migration.stdout" - name: Set flag signifying that this instance has been migrated set_fact: upgraded_postgres_version: '13' # Cleanup old Postgres resources - name: Remove old Postgres StatefulSet k8s: kind: StatefulSet api_version: v1 namespace: "{{ ansible_operator_meta.namespace }}" name: "{{ ansible_operator_meta.name }}-postgres" state: absent wait: true - name: Remove old Postgres Service k8s: kind: Service api_version: v1 namespace: "{{ ansible_operator_meta.namespace }}" name: "{{ ansible_operator_meta.name }}-postgres" state: absent - name: Remove old persistent volume claim k8s: kind: PersistentVolumeClaim api_version: v1 namespace: "{{ ansible_operator_meta.namespace }}" name: "postgres-{{ ansible_operator_meta.name }}-postgres-0" state: absent when: postgres_keep_pvc_after_upgrade