# file: postgresql/tasks/check_pg_version_mismatch.yml

# Check binary version
- name: PostgreSQL | Check binary version
  shell: >
    psql --version | sed 's/^psql (//' | sed 's/)//' | awk '{print $1, $2}'
  become: yes
  become_user: "{{ postgresql_service_user }}"
  changed_when: false
  failed_when: postgresql_binary_version.stdout == ""
  register: postgresql_binary_version

# Check database version
- name: PostgreSQL | Check database version
  shell: >
    psql --quiet --tuples-only --command="select substring(version(),'[^\s]+\s+[^\s]+');" | sed 's/^ //'
  become: yes
  become_user: "{{ postgresql_service_user }}"
  changed_when: false
  failed_when: postgresql_database_version.stdout == ""
  register: postgresql_database_version

# If versions do not match, then restart PostgreSQL
- name: PostgreSQL | Verify binary and database versions match
  debug:
    msg: "WARNING: Binary ({{ postgresql_binary_version.stdout }}) and Database ({{ postgresql_database_version.stdout }}) version mismatch. Restart required!"
  when: postgresql_binary_version.stdout != postgresql_database_version.stdout
  changed_when: postgresql_binary_version.stdout != postgresql_database_version.stdout
  notify: restart postgresql

# file: postgresql/tasks/check_pg_version_mismatch.yml

# Check binary version
- name: PostgreSQL | Check binary version
  shell: >
    psql --version | sed 's/^psql (//' | sed 's/)//' | awk '{print $1, $2}'
  become: yes
  become_user: "{{ postgresql_service_user }}"
  changed_when: false
  failed_when: postgresql_binary_version.stdout == ""
  register: postgresql_binary_version
  check_mode: no

# Check database version
- name: PostgreSQL | Check database version
  shell: >
    psql --quiet --tuples-only --command="select substring(version(),'[^\s]+\s+[^\s]+');" | sed 's/^ //'
  become: yes
  become_user: "{{ postgresql_service_user }}"
  changed_when: false
  failed_when: postgresql_database_version.stdout == ""
  register: postgresql_database_version
  check_mode: no

# If versions do not match, then restart PostgreSQL
- name: PostgreSQL | Verify binary and database versions match
  debug:
    msg: "WARNING: Binary ({{ postgresql_binary_version.stdout }}) and Database ({{ postgresql_database_version.stdout }}) version mismatch. Restart required!"
  when: postgresql_binary_version.stdout != postgresql_database_version.stdout
  changed_when: postgresql_binary_version.stdout != postgresql_database_version.stdout
  notify: restart postgresql

Change version tests to allow "Check Mode" ansible runs
Change version tests to allow "Check Mode" ansible runs
