Danny
Danny

Reputation: 209

PostgreSQL Port not ready for an ansible task

I'm almost finished with the setup for an PostgreSQL HA Ansible role between the master and slave node, but it keeps failing with one command:

- name: Create base backup
  command: pg_basebackup -X stream -D {{ postgres_home }} -h {{ master_ip }} -U {{ replica_user }}
  become: yes
  become_user: postgres

Error output of Ansible:

"stderr": "pg_basebackup: could not connect to server: could not connect to server: Connection refused\n\tIs the server running on host \"10.44.164.159\" and accepting\n\tTCP/IP connections on port 5432?",
    "stderr_lines": [
        "pg_basebackup: could not connect to server: could not connect to server: Connection refused",
        "\tIs the server running on host \"10.44.164.159\" and accepting",
        "\tTCP/IP connections on port 5432?"

But when I run this command after failing the playbook it runs without any erros and it succeeds.

Why does it works without Ansible and not within a role and playbook?

Thank you!

Upvotes: 0

Views: 2094

Answers (2)

Daniel Argüelles
Daniel Argüelles

Reputation: 2349

In my case, the port was abailable but connection wasn't established, so it fails. Instead of waiting for the port, I run the query and retry if it fails (1):

- name: Create base backup
  command: pg_basebackup -X stream -D {{ postgres_home }} -h {{ master_ip }} -U {{ replica_user }}
  become: yes
  become_user: postgres
  retries: 5
  delay: 5
  register: result
  until: result is not failed

Upvotes: 1

Baptiste Mille-Mathias
Baptiste Mille-Mathias

Reputation: 2169

you need to add a task before this task to wait the postgresql server to be ready.

You can do this way

- name: hold until Postgresql is up and running
  wait_for:
    host "{{ master_ip }}"
    port: 5432

You can also look at the complete documentation here

Upvotes: 3

Related Questions